$ run as a normal user# run as root (or with sudo)
The quick answer
server {
listen 80;
server_name shop.example.com;
location / {
proxy_pass http://127.0.0.1:3000;
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;
}
}Step by step
- Make sure the app answers locally first — and listens only on
127.0.0.1:
curl -sI http://127.0.0.1:3000 | head -1
HTTP/1.1 200 OK
ss -ltnp | grep 3000
LISTEN 0 511 127.0.0.1:3000 0.0.0.0:* users:(("node",pid=2210,fd=19))- Where the file goes: RHEL:
/etc/nginx/conf.d/NAME.conf. Ubuntu:/etc/nginx/sites-available/NAMEplus a link:
ln -s /etc/nginx/sites-available/shop /etc/nginx/sites-enabled/shop- Why each header:
| Line | Why |
|---|---|
proxy_set_header Host $host | The app sees the real domain (needed for links and virtual hosts). |
X-Real-IP, X-Forwarded-For | The app logs the visitor's IP, not 127.0.0.1. |
X-Forwarded-Proto $scheme | The app knows the visitor used HTTPS (no redirect loops). |
- WebSockets, uploads and timeouts — add what your app needs:
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}
server {
listen 80;
server_name shop.example.com;
client_max_body_size 50m;
location / {
proxy_pass http://127.0.0.1:3000;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $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;
proxy_read_timeout 120s;
}
}- RHEL: allow Nginx to connect to the app (SELinux blocks it by default — you get 502):
setsebool -P httpd_can_network_connect 1- Add HTTPS with Certbot (it edits this server block for you), and open the firewall:
firewall-cmd --permanent --add-service={http,https} && firewall-cmd --reload
certbot --nginx -d shop.example.com --redirectCheck that it worked
Common problems
| Problem | Cause | Fix |
|---|---|---|
502 Bad Gateway | The app is down, or on another port. | curl 127.0.0.1:3000; start the app; check proxy_pass. |
502 and Permission denied in error.log (RHEL) | SELinux blocks the connection. | setsebool -P httpd_can_network_connect 1. |
504 Gateway Time-out | The app answers too slowly. | Make the app faster, or raise proxy_read_timeout. |
413 Request Entity Too Large | Upload bigger than 1 MB (default). | client_max_body_size 50m; |
| Redirect loop after HTTPS | The app does not know the visitor uses HTTPS. | X-Forwarded-Proto $scheme and "trust proxy" in the app. |
Wrong paths (/api/api/…) | A path in proxy_pass changes the URI. | proxy_pass http://127.0.0.1:3000; (no path) keeps the URI as it is. |
| WebSocket disconnects | No Upgrade/Connection headers. | Add the map and the two headers, proxy_http_version 1.1. |
Go further
- Command pages: nginx, curl, certbot, ss.
- Next how-tos: Get a free TLS certificate, Run a container and publish a port, Open a port in the firewall, Find slow requests in Nginx logs, Add security headers in Nginx, Host two websites on one server.
- Full guides: Install Nginx, HAProxy, Traefik.
- At work: Nginx in daily use. Learn SELinux: RHCSA — Booleans.
- Practise: Lab: put Nginx in front of an app.
1Why should the app listen on 127.0.0.1 when Nginx is in front?
Only Nginx can reach it; the internet can not skip Nginx (and its TLS, limits and logs).
2What is the usual cause of a 502 from Nginx on RHEL when the app runs fine?
SELinux: set httpd_can_network_connect to on.