GrepBoard/매뉴얼
47 편 ·

Nginx 를 앱 앞의 리버스 프록시로 두기

·Ubuntu 24.04.5 LTS · nginx 1.24.0 · Python 3 http.server(시험용 앱)·4 단계

호스트 이름 하나로 들어온 요청을 로컬 포트의 앱으로 넘기고, 클라이언트 주소를 함께 전달하고, 프록시의 502 와 앱 자체 오류를 가려내는 방법입니다.

호스트 이름 하나로 들어온 요청을 127.0.0.1:8080 에서 듣는 앱으로 넘깁니다. 1편에서 Nginx 를 설치했다고 봅니다.

[1] 앱용 사이트 파일을 만듭니다.

/etc/nginx/sites-available/app
server {    listen 80;    server_name app.example.com;     location / {        proxy_pass http://127.0.0.1:8080;        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;    }}

[2] 사이트를 켜고, 검사하고, 다시 읽게 합니다.

console
# ln -s /etc/nginx/sites-available/app /etc/nginx/sites-enabled/# nginx -tnginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successful# systemctl reload nginx

[3] 호스트 이름을 붙여 요청해 봅니다. 아직 8080 에서 듣는 앱이 없으면 502 입니다. 이는 Nginx 는 정상인데 뒤쪽 포트에서 아무도 답하지 않는다는 뜻입니다.

console
$ curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: app.example.com' http://127.0.0.1/502# ss -tlnp 'sport = :8080'State Recv-Q Send-Q Local Address:Port Peer Address:Port Process
console
# tail -n 1 /var/log/nginx/error.log2026/10/19 09:33:31 [error] 34879#34879: *15 connect() failed (111: Connection refused) while connecting to upstream, client: 127.0.0.1, server: app.example.com, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "app.example.com"

[4] 앱을 띄운 뒤 다시 요청합니다. 여기서는 작업 폴더에서 시험용 웹 서버를 잠깐 띄웠습니다.

console
$ python3 -m http.server 8080 --bind 127.0.0.1 &# ss -tlnp 'sport = :8080'State  Recv-Q Send-Q Local Address:Port Peer Address:Port ProcessLISTEN 0      5          127.0.0.1:8080      0.0.0.0:*    users:(("python3",pid=34948,fd=3))$ curl -s -o /dev/null -w '%{http_code}\n' -H 'Host: app.example.com' http://127.0.0.1/200

Nginx 로그를 브라우저에서 바로 분석 ↗ — 아무것도 올라가지 않습니다.

#nginx#proxy#ubuntu