— 아래는 nginx를 웹서버로 사용하고 nodejs https 프록시 Andmedia server 의 https프록시하는 설정입니다.
Below is the configuration for using nginx as a web server and nodejs https proxy Andmedia server’s https proxy.
https://host.domain.com/nodejs/ → Node.js 서버 (/nodejs/)를 통해 접속했습니다. / You are accessing the Node.js server (/nodejs/).
https://host.domain.com/nodejs/login → Node.js 서버 (/nodejs/login)를 통해 접속했습니다. / You connected via the Node.js server (/nodejs/login).
https://host.domain.com/web/ → 정적 파일(index.html) / Static file(index.html)
https://host.domain.com/LiveApp/ → Ant Media Server
-Nginx의 루트 디렉토리 처리가 Nodejs보다 먼저 실행되므로 설정이 중복되면 nodejs ‘/’ 는실행이 안됩니다.
Since Nginx’s root directory processing is executed before Nodejs, if the settings are duplicated, nodejs ‘/’ will not be executed.
-nodejs구성시 /nodejs/처럼 라우터에 접두어를 다 붙여서 구성하는 것을 추천합니다.(루트로 실행시 nginx처리가 우선이므로 실행 되지 않을 수 있음)
When configuring nodejs, it is recommended to configure it by adding a prefix to the router, such as /nodejs/. (When running as root, nginx processing takes priority, so it may not run.)
-요청 처리 설명 / Request Processing Description
요청 경로 request path | 처리주체 Processing entity | 설명 explanation |
---|---|---|
/ | Nginx | 정적 파일 /var/www/html/index.html 제공Static file /var/www/html/index.html |
/nodejs/ | Node.js | Nginx가 프록시로 전달 Nginx proxying |
/LiveApp/ | Ant Media | Nginx가 프록시로 전달 Nginx proxying |
기타 etc | Nginx | try_files 로 정적 파일 확인 후 없으면 404Check static files with try_files and output 404 |
— Nginx location /nodejs/ + proxy_pass http://localhost:3000/nodejs/;
Nginx는 요청 경로에서 location /nodejs/ 이후의 경로(즉, /admin)를 잘라서 proxy_pass 뒤에 붙입니다.
Nginx will cut off the path after location /nodejs/ ( /admin ) from the request path and append it after proxy_pass.
하지만 proxy_pass에 슬래시로 끝나는 경로가 있으면, 잘린 경로를 그대로 붙여서 전달합니다.
However, if proxy_pass contains a path that ends with a slash, the truncated path is passed as is
요청: https://host.damain.com/nodejs/admin location: /nodejs/ proxy_pass: http://localhost:3000/nodejs/ → 전달: http://localhost:3000/nodejs/admin Node.js는 그냥 정상적인 /nodejs/admin 경로를 받게 됩니다. Nginx(/etc/nginx/sites-available/default) 설정
— 그런데 만약 proxy_pass가 슬래시 없이 끝난다면?
But what if proxy_pass ends without a slash?
proxy_pass http://localhost:3000/nodejs; # 슬래시 없음 / no slash
이럴 경우 Nginx는 요청 경로 전체를 proxy_pass의 뒤에 붙이지 않고, 그냥 덮어씌웁니다.
In this case, Nginx will not append the entire request path to proxy_pass, but will simply overwrite it.
https://…/nodejs/admin → http://localhost:3000/nodejsadmin
— 결론은 nginx에서 경로 설정을 올바로 하면 프록시된 결과값(http://localhost:3000/nodejs/admin)을 가지고 nodejs에서 결과값을 처리합니다.
The bottom line is that if you set the path correctly in nginx, the proxied result (http://localhost:3000/nodejs/admin) will be used to process the result in nodejs.
-마지막으로 정리하자면…
Finally, to summarize
1.nginx의 설정이 nodejs보다 우선순위가 높다. 그러므로nodejs를 루트(/)라우터를 사용하면 오류가 발생 할 수 있다. 2.nginx 설정의 proxy_pass http://localhost:3000/nodejs/;에서 마지막 / 가 중요하다. 3.nginx설정이 올바르다면 nodejs는 nginx를 모르니 프록시 처리된 부분의 라우터 부분만 처리한다.
2.설정 및 소스코드
— Nginx(/etc/nginx/sites-available/default) Setting
server {
listen 443 ssl;
server_name host.domain.com;
ssl_certificate /etc/letsencrypt/live/media.freelifemakers.org/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/media.freelifemakers.org/privkey.pem;
include /etc/letsencrypt/options-ssl-nginx.conf;
ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
# 정적 웹 파일 / static web files (/web/)
location /web/ {
alias /var/www/html/web/;
index index.html;
try_files $uri $uri/ =404;
}
# Ant Media Server 연동 / antmedia server integration (/LiveApp/)
location /LiveApp/ {
proxy_pass http://localhost:5080/LiveApp/;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
# Node.js 앱 프록시 / Node.js app proxy (/nodejs/)
location /nodejs/ {
proxy_pass http://localhost:3000/nodejs/; # <=== 중요: /nodejs/로 프록시
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
proxy_set_header Host $host;
proxy_cache_bypass $http_upgrade;
}
# 루트 정적 파일 / root static file
location / {
root /var/www/html/;
index index.html;
try_files $uri $uri/ =404;
}
}
# HTTP 접근 시 HTTPS로 리디렉션 /
Redirect to HTTPS when accessing HTTP
server {
listen 80;
server_name host.domain.com;
return 301 https://$host$request_uri;
}
Nodejs test code
const express = require('express');
const app = express();
const port = 3000;
// 루트 경로 (프록시를 통해 직접 접근하는 경우 사용 안됨)
app.get("/", (req, res) => {
res.send("Node.js 서버 (/)를 통해 접속했습니다.");
});
// 프록시된 경로
app.get("/nodejs/", (req, res) => {
res.send("Node.js 서버 (/nodejs/)를 통해 접속했습니다.");
});
app.get("/nodejs/admin", (req, res) => {
res.send("Node.js 서버 (/nodejs/admin)를 통해 접속했습니다.");
});
app.get("/nodejs/video", (req, res) => {
res.send("Node.js 서버 (/nodejs/video)를 통해 접속했습니다.");
});
app.get("/nodejs/login", (req, res) => {
res.send("Node.js 서버 (/nodejs/login)를 통해 접속했습니다.");
});
// 확인용
app.get("/liveapp", (req, res) => {
res.send("Ant Media Server (/liveapp)으로 직접 접속했습니다. (Nginx 프록시 설정 확인용)");
});
app.listen(port, () => {
console.log(`Node.js 서버가 http://localhost:${port} 에서 실행 중입니다.`);
});