์ฐ์ Nginx ์ค์ ํ์ผ์ ํฌ๊ฒ 2๊ฐ์ง๋ก ๋๋๋ค.
๊ตฌ์กฐ๋ฅผ ๋๋์ผ๋ก์จ ์ด์ ์ํฅ๋๋ฅผ ์ค์ด๊ณ ์ค์ ์ถฉ๋ ๋ฐฉ์ง, ์๋น์ค ์ถ๊ฐ/์ญ์ ๊ฐ ์ฌ์ฐ๋ฉฐ ๋กค๋ฐฑ์ด ์ฝ๋ค.
nginx ์ค์ ๊ตฌ์กฐ
/etc/nginx/
โโโ nginx.conf # ๋ฉ์ธ ์ค์ ํ์ผ
โโโ conf.d/ # ์๋ฒ๋ณ ์ค์ include
โ โโโ default.conf
โ โโโ api.conf
โ โโโ web.conf
์์ ์ ์ํฅ๋ ํ์ธ
- nginx.conf ์์ → nginx ์ ์ฒด ์ํฅ
- conf.d/*.conf ์์ → ํด๋น ์๋น์ค๋ง ์ํฅ
1. ๋ฉ์ธ ์ค์ (Main Config) - /etc/nginx/nginx.conf
-> nginx ์ ์ฒด ํ๋ก์ธ์ค/๋์ ๋ฐฉ์์ ์ ์ํ๋ ๋ฃจํธ ์ค์
1-1. ์ ์ญ(Global) ์ค์
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log warn;
pid /var/run/nginx.pid;
- user : nginx ํ๋ก์ธ์ค ์คํ ๊ณ์
- worker_processes : ์์ปค ํ๋ก์ธ์ค ๊ฐ์
- error_log : ์๋ฌ ๋ก๊ทธ
- pid : PID ํ์ผ
2-2. ์ด๋ฒคํธ ๋ธ๋ก
๋์์ ์ฒ๋ฆฌ ๊ฐ๋ฅํ ์ปค๋ฅ์ ์
events {
worker_connections 1024;
}
2-3. http ๋ธ๋ก
HTTP ์๋ฒ ์ ์ญ ์ค์
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
log_format main '$remote_addr - $remote_user ...';
access_log /var/log/nginx/access.log main;
sendfile on;
keepalive_timeout 65;
include /etc/nginx/conf.d/*.conf; //์๋น์ค ์ค์ ํ์ผ include
map $http_upgrade $connection_upgrade {
default upgrade; # 1์์: ์๋ฌด๊ฒ๋ ๋งค์นญ ์ ๋๋ฉด ์ด๊ฑฐ
'' close; # 2์์: ๋น ๋ฌธ์์ด์ด๋ฉด ์ด๊ฑฐ
}
}
- MIME ํ์
- ๋ก๊ทธ ํฌ๋งท
- gzip
- proxy ๊ด๋ จ ๊ธฐ๋ณธ๊ฐ
- SSL ๊ธฐ๋ณธ๊ฐ
- upstream ์ ์ ๊ฐ๋ฅ
- ์ค์! ์๋น์ค๋ณ ์ค์ ์ ์ง์ ๋ค ๋ฃ๊ธฐ๋ณด๋ค๋ include ๋ก ๋ถ๋ฆฌํ๋ ๊ตฌ์กฐ
ํธ๋ฌ๋ธ์ํ
| ์ผ๋ฐ HTTP | WebSocket | |
| ์ฐ๊ฒฐ | ์์ฒญํ ๋๋ง๋ค ์๋ก ์ฐ๊ฒฐ | ํ ๋ฒ ์ฐ๊ฒฐํ๋ฉด ๊ณ์ ์ ์ง |
| ๋ฐฉํฅ | ํด๋ผ์ด์ธํธ → ์๋ฒ (๋จ๋ฐฉํฅ) | ์๋ฐฉํฅ ์์ ๋กญ๊ฒ |
| ์ฌ์ฉ ์ | ์นํ์ด์ง, REST API, ์ด๋ฏธ์ง ๋ก๋ฉ | ์ฑํ , ์๋ฆผ, ๊ฒ์, ์ค์๊ฐ ๋ฐ์ดํฐ |
๋ง์ฝ์ WebSoket์ ์ฌ์ฉํ๋ ์์คํ ์ด๋ผ๋ฉด ์๋น์ค ์ค์ ํ์ผ ์์ ํด๋น Proxy header๊ฐ ๋ค์ด์์๊ฒ์ด๋ค.
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
๊ทธ๋ ๋ค๋ฉด ๋ฉ์ธ ์ค์ ํ์ผ์ http ๋ธ๋ก ์์ ์๋ ์ฝ๋๊ฐ ๋ค์ด๊ฐ์ผํ๋ฉฐ ๊ทธ๋ ์ง ์์ผ๋ฉด ์ค๋ฅ ๋ฐ์ํ๋ค.
map $http_upgrade $connection_upgrade {
default upgrade; # 1์์: ์๋ฌด๊ฒ๋ ๋งค์นญ ์ ๋๊ฑฐ๋ ๋น ๋ฌธ์์ด์ด ์๋๋ฉด ์ด๊ฑฐ
'' close; # 2์์: ๋น ๋ฌธ์์ด์ด๋ฉด ์ด๊ฑฐ
}
์ผ์ด์ค 1: WebSocket ์์ฒญ
- $http_upgrade = "websocket" (Upgrade ํค๋ ๊ฐ)
- ๋งค์นญ ํ์ธ:
- close ๋งค์นญ โ (๋น ๋ฌธ์์ด ์๋)
- default upgrade ๋งค์นญ โญ
- ๊ฒฐ๊ณผ: $connection_upgrade = "upgrade"
์ผ์ด์ค 2: ์ผ๋ฐ HTTP ์์ฒญ
- $http_upgrade = "" (๋น ๋ฌธ์์ด, Upgrade ํค๋ ์์)
- ๋งค์นญ ํ์ธ:
- close ๋งค์นญ โญ (์ฌ๊ธฐ์ ๋!)
- ๊ฒฐ๊ณผ: $connection_upgrade = "close"
- default๋ ํ์ธ์กฐ์ฐจ ์ ํจ
์ผ์ด์ค 3: ์ด์ํ ๊ฐ์ด ๋ค์ด์จ ๊ฒฝ์ฐ
- $http_upgrade = "something-weird"
- ๋งค์นญ ํ์ธ:
- close ๋งค์นญ โ (๋น ๋ฌธ์์ด ์๋)
- default upgrade ๋งค์นญ โญ
- ๊ฒฐ๊ณผ: $connection_upgrade = "upgrade"
2. ์๋น์ค ์ค์ (Service / Virtual Host) - /etc/nginx/conf.d/*.conf
HTTPS + WebSocket ์์
์๋ ์ฝ๋๋ nginx๋ฅผ proxy server๋ก ์ฌ์ฉํ ์์์ด๋ค.
server {
listen 443 ssl;
server_name chat.example.com;
# SSL ํด๋
์ฒ๋ฆฌ๋ก HTTPS ์ฌ๊ธฐ์ ์ฒ๋ฆฌ!
ssl_certificate /etc/nginx/ssl/cert.pem;
ssl_certificate_key /etc/nginx/ssl/key.pem;
location / {
proxy_pass http://localhost:8080; # proxy server
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade; # HTTP/WebSocket ์ฒ๋ฆฌ
proxy_set_header Connection $connection_upgrade; # HTTP/WebSocket ์ฒ๋ฆฌ
proxy_set_header Host $host;
}
}
1. HTTPS ์์ฒญ (์ผ๋ฐ ์นํ์ด์ง)
๋ธ๋ผ์ฐ์ → https://chat.example.com (443 ํฌํธ)
→ nginx (SSL ํด๋
)
→ http://localhost:8080 (ํ๋ฌธ HTTP๋ก ์ ๋ฌ)
2. WebSocket ์ฐ๊ฒฐ (wss://)
๋ธ๋ผ์ฐ์ → wss://chat.example.com (443 ํฌํธ)
→ nginx (SSL ํด๋
+ WebSocket ์
๊ทธ๋ ์ด๋)
→ ws://localhost:8080 (ํ๋ฌธ WebSocket์ผ๋ก ์ ๋ฌ)
Proxy Server๋ ๋ญ๊น?
-> ์ค๊ฐ ๋ค๋ฆฌ ์ญํ ์ ํด์ฃผ๋ ์๋ฒ์ด๋ค.
-> Apache (with mod_proxy), Caddy, Envoy ๊ฐ์ Proxy Server๊ฐ ์กด์ฌํ๋ค.
์ฌ์ฉ์ ๋ธ๋ผ์ฐ์ → nginx (80ํฌํธ) → Node.js ์ฑ (3000ํฌํธ)
์ฌ์ฉ์๋ 80, 443 ์ธํฐ๋ท ํฌํธ๋ง ์๊ณ ์ ์ํ๋ฉด ์์์ ์ฌ๋ฌ ์๋ฒ์ ํฌํธ๋ก ์ฐ๊ฒฐ์์ผ์ค๋ค.
- ๋ณด์: ์ค์ ์๋ฒ(3000ํฌํธ) ์ง์ ๋ ธ์ถ ์ ํจ
- ๋ก๋๋ฐธ๋ฐ์ฑ: ์ฌ๋ฌ ์๋ฒ์ ํธ๋ํฝ ๋ถ์ฐ
- SSL ์ฒ๋ฆฌ: nginx๊ฐ HTTPS ์ฒ๋ฆฌ, ์ฑ์ HTTP๋ง
- ์ ์ ํ์ผ: ์ด๋ฏธ์ง ๊ฐ์ ๊ฑด nginx๊ฐ ์ง์ ์๋น
์ nginx๊ฐ ์ ์ ํ์ผ ์๋น์ ๋น ๋ฅผ๊น?
-> ์ด๋ฏธ์ง ์์ฒญ → nginx๊ฐ ๋์คํฌ์์ ๋ฐ๋ก ์ฝ์ด์ ์ ์กํ๊ธฐ ๋๋ฌธ!
- ์์ฒญ ๋น ๋ฆ: C๋ก ์์ฑ๋จ, ํ์ผ ์ฝ๊ธฐ์ ์ต์ ํ
- ๋ฐฑ์๋ ๋ถํ ๊ฐ์: Node.js๊ฐ ํ์ผ ์ฒ๋ฆฌ ์ ํด๋ ๋จ ( ์ด๋ฏธ์ง ์์ฒญ → nginx → Node.js → ํ์ผ ์ฝ๊ธฐ → nginx → ๋ธ๋ผ์ฐ์ )
- ํจ์จ์ ๋ฉ๋ชจ๋ฆฌ: ํ์ผ ์บ์ฑ, sendfile ์์คํ ์ฝ ์ฌ์ฉ
'Server > Nginx' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| nginx ๋์ ๋ชจ๋(dynamic module) ๋น๋ํด์ ์ ์ฉํ๊ธฐ (0) | 2026.01.25 |
|---|