GrepBoard/Manuals
47 manuals ·

Put Nginx in front of an app as a reverse proxy

·Ubuntu 24.04.5 LTS · nginx 1.24.0 · Python 3 http.server (test app)·4 steps

Send one host name to an app on a local port, pass the client address along, and tell a proxy 502 apart from an app error.

Forward requests for one host name to an app listening on 127.0.0.1:8080. This assumes Nginx is installed as in part 1.

[1] Create a site file for the app.

/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] Enable the site, test, and reload.

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] Request the host name. With nothing listening on 8080 yet you get 502: Nginx is fine, but nobody answers on the port behind it.

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] Start the app and request again. Here a throwaway web server was started in a work folder.

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

Analyze your Nginx log in the browser ↗ — nothing is uploaded.

#nginx#proxy#ubuntu