— ์๋๋ 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} ์์ ์คํ ์ค์
๋๋ค.`);
});