Nginx Reverse Proxy: From First Config to Production

A nginx reverse proxy is the front door of most small production setups. Users talk to Nginx, and Nginx talks to your app. The app never faces the internet directly. That one layer gives you TLS termination, request routing, static file serving, and a place to add rate limits later, all without changing your application code.

This nginx reverse proxy setup guide starts from zero: one server, one app running on a local port, and a domain pointing at the server. By the end you will have a production-ready config with HTTPS, sane headers, and a checklist for the errors you will actually hit. Follow this reverse proxy tutorial in order and you will end up with a server you can trust.

What a Reverse Proxy Does

A reverse proxy receives requests from clients and forwards them to backend servers. The client never sees the backend. That sounds simple, and the basic config is, but the details matter. Headers carry the real client IP, the original scheme, and the host name. If you get them wrong, your app logs the proxy’s IP for every request, generates http links on an https site, or breaks redirects.

Three jobs make a reverse proxy worth running. First, routing: one Nginx server can send /api/ to a Node app, /blog/ to WordPress, and everything else to a static site. Second, TLS: Nginx handles certificates so your app only speaks plain HTTP on localhost. Third, protection: the app binds to 127.0.0.1, so nothing on the internet can reach it directly. A Cloudflare setup in front of your VPS adds another layer on top, but the reverse proxy is the layer you control.

Nginx Reverse Proxy Setup: The First Working Config

Assume your app listens on port 3000. Install Nginx, then create a server block. On Ubuntu the sites live in /etc/nginx/sites-available/ and are enabled with a symlink into sites-enabled/.

server {
    listen 80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header 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_cache_bypass $http_upgrade;
    }
}

Test and reload:

sudo nginx -t
sudo systemctl reload nginx

Every line in that block earns its place. proxy_http_version 1.1 with the Upgrade headers keeps WebSocket connections alive, which matters for chat apps and live dashboards. proxy_set_header Host $host passes the real hostname so your app can build correct URLs. X-Real-IP and X-Forwarded-For preserve the client IP for logging and rate limiting. Skip them and every visitor looks like 127.0.0.1 in your logs.

One common mistake: proxy_pass http://127.0.0.1:3000; with a trailing slash behaves differently from the same line without one. A trailing slash strips the matched location prefix from the forwarded URI. In a location / block the difference is invisible, but in location /api/ it changes the path your app receives. If your app gets 404s for routes that work locally, check the trailing slash first.

Routing Multiple Apps From One Server

A single Nginx instance can serve several apps on one domain by path, or several domains with separate server blocks. Path routing looks like this:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        include proxy_params;
    }

    location /api/ {
        proxy_pass http://127.0.0.1:4000;
        include proxy_params;
    }

    location /static/ {
        alias /var/www/static/;
        expires 30d;
        add_header Cache-Control "public, immutable";
    }
}

The proxy_params include file ships with Nginx on Debian and Ubuntu and sets the standard forwarded headers. Using it keeps your blocks short and consistent. Static files bypass the app entirely, which is the point: Nginx serves a file from disk far faster than your app can, and the expires directive lets browsers cache it for a month.

For a full walkthrough of the app side of this setup, see the guide to deploying a Node.js app to a VPS with PM2 and Nginx. PM2 keeps your app running after you log out, and Nginx in front of it is the standard pairing.

Adding HTTPS With Let’s Encrypt

Never run a production reverse proxy over plain HTTP. Certbot handles the certificate and rewrites your server block for you:

sudo apt install certbot python3-certbot-nginx
sudo certbot --nginx -d app.example.com

Certbot adds a listen 443 ssl block and a redirect from port 80. After it runs, check the generated config and make sure your proxy_set_header X-Forwarded-Proto $scheme; line is still there. Your app needs that header to know the request arrived over HTTPS, otherwise it may generate http links or refuse secure cookies.

Certificates renew automatically through a systemd timer. Verify it works before you forget about it:

sudo certbot renew --dry-run

The full process, including common renewal failures, is covered in the Let’s Encrypt on Nginx guide. If your site serves WordPress behind the proxy, also read the PHP-FPM tuning guide, because WordPress needs X-Forwarded-Proto handled correctly or its admin area will loop redirects.

Production Hardening: Timeouts, Buffers, and Limits

The default Nginx settings work until they do not. Four settings cause most production surprises.

  • Timeouts. proxy_read_timeout defaults to 60 seconds. An endpoint that takes longer, such as a large report or a file export, will hit a 504. Raise it for specific locations, not globally: proxy_read_timeout 300s; inside the location that needs it.
  • Body size. client_max_body_size defaults to 1 MB. File uploads larger than that fail with a 413 error. Set it where uploads happen: client_max_body_size 50m;.
  • Buffering. Nginx buffers responses by default, which is good for slow clients. For streaming responses and server-sent events, turn it off in that location: proxy_buffering off;.
  • Rate limiting. A simple limit zone stops a single IP from hammering your login page. Add it in the http context, then apply it per location.
limit_req_zone $binary_remote_addr zone=login:10m rate=5r/s;

server {
    listen 443 ssl;
    server_name app.example.com;

    location /login {
        limit_req zone=login burst=10 nodelay;
        proxy_pass http://127.0.0.1:3000;
        include proxy_params;
    }
}

Watch your error log after every change. sudo tail -f /var/log/nginx/error.log tells you what Nginx actually did, which is often more useful than guessing from the browser’s error page.

When It Breaks: 502, 504, and Redirect Loops

A 502 Bad Gateway means Nginx could not reach your app. The app is down, it crashed, or it listens on a different port than your proxy_pass points to. Check the app first with curl http://127.0.0.1:3000 from the server itself. If that fails, the problem is the app, not the nginx proxy_pass config.

A 504 Gateway Timeout means the app answered too slowly. Check the app logs for a stuck query or a long-running task, then look at the timeout settings above. Do not raise timeouts blindly: a timeout that keeps growing hides a real performance problem.

A redirect loop usually means a scheme mismatch. Nginx terminates TLS and talks HTTP to the app, while the app thinks every request is insecure and redirects to https, which Nginx passes back, forever. The fix is X-Forwarded-Proto plus your framework’s trust-proxy setting. In Express that is app.set('trust proxy', true). The 502 troubleshooting guide walks through the full diagnostic sequence for the most common case.

A Short Pre-Launch Checklist

  • nginx -t passes and the config reloads cleanly
  • HTTPS works and the certificate renews with a dry run
  • Real client IPs appear in app logs, not 127.0.0.1
  • Uploads larger than 1 MB succeed where they should
  • The app binds to localhost only, not 0.0.0.0
  • Error logs are quiet under normal traffic

Run through that list after every config change, not just at launch. A reverse proxy is a small piece of infrastructure, but it sits in front of everything. Get the headers, the timeouts, and the TLS right once, and it will quietly do its job for years. Get them wrong and every app behind it inherits the problem.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.