Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 22Web · Nginx

How to set up Nginx as a reverse proxy

Most apps (Node.js, Python, Java, Go, a container) listen on a local port like 3000 or 8080. In production you put Nginx in front: it takes HTTPS on port 443, passes the requests to the app, adds the right headers, handles big uploads, WebSockets and timeouts, and keeps the app port closed to the internet. This how-to writes a clean proxy config, explains every line, and fixes the errors you will see (502, 504, 413).

  • 5 Lessons
  • 11 Commands

$ run as a normal user# run as root (or with sudo)

22.1

The quick answer

/etc/nginx/conf.d/shop.conf
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;
    }
}
nginx -t
systemctl reload nginx
curl -I http://shop.example.com

22.2

Step by step

  1. 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))
  1. Where the file goes: RHEL: /etc/nginx/conf.d/NAME.conf. Ubuntu: /etc/nginx/sites-available/NAME plus a link:
ln -s /etc/nginx/sites-available/shop /etc/nginx/sites-enabled/shop
  1. Why each header:
LineWhy
proxy_set_header Host $hostThe app sees the real domain (needed for links and virtual hosts).
X-Real-IP, X-Forwarded-ForThe app logs the visitor's IP, not 127.0.0.1.
X-Forwarded-Proto $schemeThe app knows the visitor used HTTPS (no redirect loops).
  1. WebSockets, uploads and timeouts — add what your app needs:
/etc/nginx/conf.d/shop.conf
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;
    }
}
  1. RHEL: allow Nginx to connect to the app (SELinux blocks it by default — you get 502):
setsebool -P httpd_can_network_connect 1
  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 --redirect

22.3

Check that it worked

curl -sI https://shop.example.com | head -1
HTTP/2 200
tail -n 5 /var/log/nginx/access.log
tail -n 5 /var/log/nginx/error.log

From another machine, the app port must be closed: nc -zv shop.example.com 3000 → refused or timed out.

22.4

Common problems

ProblemCauseFix
502 Bad GatewayThe 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-outThe app answers too slowly.Make the app faster, or raise proxy_read_timeout.
413 Request Entity Too LargeUpload bigger than 1 MB (default).client_max_body_size 50m;
Redirect loop after HTTPSThe 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 disconnectsNo Upgrade/Connection headers.Add the map and the two headers, proxy_http_version 1.1.

22.5

Go further

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.

All commands in this chapter