How to Set Up Nginx as a Reverse Proxy

Put Nginx in front of a Node, Python, Go or container app: proxy config, forwarding headers, WebSockets, HTTPS, caching, and several apps on one server.

Published
Reading time
3 min

An application server — Node on port 3000, a Python app on 8000, a container on 8080 — should not face the internet directly. Nginx in front of it handles TLS, static files, compression, caching and rate limiting, and lets several apps share ports 80 and 443 on one server. This is the standard setup, with the details that usually get missed.

The basic proxy

Assuming Nginx is installed (see the LEMP guide for that) and the app listens on 127.0.0.1:3000:

nginx
# /etc/nginx/sites-available/app.example.com
server {
    listen 80;
    listen [::]:80;
    server_name app.example.com;

    location / {
        proxy_pass http://127.0.0.1:3000;
        proxy_http_version 1.1;
        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 60s;
    }
}
bash
sudo ln -s /etc/nginx/sites-available/app.example.com /etc/nginx/sites-enabled/
sudo nginx -t && sudo systemctl reload nginx

The proxy_set_header lines are not optional. Without them the app sees every request as coming from 127.0.0.1 over HTTP — logs are useless, rate limiting hits everyone at once, and frameworks generate http:// links on an https:// site. Configure the app to trust the proxy (Express: app.set('trust proxy', 1); Laravel: TrustProxies; Django: SECURE_PROXY_SSL_HEADER).

The app itself should listen on 127.0.0.1 only, so nothing can bypass Nginx.

HTTPS

bash
sudo certbot --nginx -d app.example.com

Certbot rewrites the block with a 443 listener, the certificate paths and an HTTP→HTTPS redirect, and schedules renewal. Nothing about the app changes; TLS ends at Nginx.

WebSockets

A chat, a live dashboard, Socket.io, Next.js dev — anything using WebSockets needs the upgrade headers passed through:

nginx
    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_read_timeout 3600s;    # keep long-lived connections open
    }

Static files direct from Nginx

If the app has a public/ or build/ directory, let Nginx serve it and keep the app for dynamic routes:

nginx
    location /static/ {
        alias /var/www/app/public/static/;
        expires 30d;
        access_log off;
    }

Several apps on one server

One server block per hostname, each proxying to its own port:

nginx
server { server_name app.example.com;  location / { proxy_pass http://127.0.0.1:3000; ... } }
server { server_name api.example.com;  location / { proxy_pass http://127.0.0.1:4000; ... } }
server { server_name docs.example.com; root /var/www/docs; }

Or by path on one hostname — location /api/ { proxy_pass http://127.0.0.1:4000/; } — noting that a trailing slash on proxy_pass strips the /api/ prefix before forwarding, and no trailing slash keeps it.

Upstreams and load balancing

Name the backend, and add more instances later without touching the location block:

nginx
upstream app_backend {
    server 127.0.0.1:3000;
    server 127.0.0.1:3001;
    keepalive 32;
}
server {
    ...
    location / { proxy_pass http://app_backend; ... }
}

Nginx round-robins between them and skips one that stops answering.

Caching responses

For an API or a page that is the same for everyone for a minute:

nginx
proxy_cache_path /var/cache/nginx/app levels=1:2 keys_zone=app:10m max_size=500m inactive=10m;

    location /api/products {
        proxy_cache app;
        proxy_cache_valid 200 60s;
        proxy_cache_use_stale error timeout updating;
        add_header X-Cache-Status $upstream_cache_status;
        proxy_pass http://app_backend;
    }

X-Cache-Status: HIT in the response headers confirms it. Never cache anything that varies per user without a proxy_cache_key that includes the session.

Limits and protection

nginx
limit_req_zone $binary_remote_addr zone=perip:10m rate=10r/s;

    location /api/ {
        limit_req zone=perip burst=20 nodelay;
        client_max_body_size 20m;
        proxy_pass http://app_backend;
    }

Ten requests a second per IP with a burst of twenty is generous for humans and fatal for scrapers. client_max_body_size raises the 1 MB default for uploads.

Docker

A container publishing -p 127.0.0.1:8080:80 is just another backend at 127.0.0.1:8080. Publish to 127.0.0.1, not 0.0.0.0, or Docker's own iptables rules will expose the container on all interfaces regardless of your firewall. When Docker is the right tool at all is discussed in Docker on a VPS.

Debugging

502 Bad Gateway — the app is not listening on the port (ss -tulpn | grep 3000), or crashed; check its logs and systemctl status. 504 Gateway Timeout — the app took longer than proxy_read_timeout. 413 — raise client_max_body_size. Nginx's error log names the upstream and the reason.

On a VPSPioneer managed VPS, a Node, Python or containerised app is set up exactly like this at handover — systemd unit, Nginx in front, TLS, and the app bound to localhost — and adding another app is a ticket rather than a config file.

#nginx#reverse-proxy#node#docker#servers

Keep reading

More from Servers

All guides

Servers

How to Configure PHP-FPM Pools for Performance

Size pm.max_children from real memory numbers, choose dynamic or ondemand, set timeouts and the slow log, run one pool per site, and stop 502 and 504 errors.

4 min read →

Servers

Apache vs Nginx: Which Web Server Should You Run?

How Apache and Nginx differ in architecture, performance and .htaccess support, why many hosts run both, and which to choose for PHP, an API or static files.

3 min read →

Servers

How to Install Nginx, PHP-FPM and MariaDB (LEMP) on Ubuntu

A complete LEMP setup on Ubuntu 24.04 — Nginx, PHP 8.3-FPM, MariaDB — with a working server block, a secure database, sensible PHP settings and HTTPS.

3 min read →