Paj Digital Labs
All articles
Hosting & Server Infrastructure

nginx as a Reverse Proxy: The Config We Actually Deploy

Every nginx tutorial shows you proxy_pass and stops. Here is the full block we ship to production — headers, timeouts, buffering, WebSocket upgrades and the rate limit that saved a launch.

Paj Digital Labs 2 min read
Server racks and network cabling in a data center
Editorial illustration · Hosting & Server Infrastructure

The average nginx reverse proxy tutorial is six lines long and wrong for production in four of them. It proxies the request, drops the headers your app needs, buffers responses your users are waiting on, and falls over the first time someone opens a WebSocket.

This is the block we actually deploy, with the reasons for each line.

The full server block

nginx
upstream app {
    server 127.0.0.1:3000;
    keepalive 32;
}

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

    client_max_body_size 25m;

    location / {
        proxy_pass http://app;
        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_set_header Connection class="text-code-string">"";

        proxy_connect_timeout 5s;
        proxy_read_timeout 60s;
        proxy_send_timeout 60s;

        proxy_buffering on;
        proxy_buffer_size 8k;
        proxy_buffers 16 8k;
    }

    location /ws {
        proxy_pass http://app;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection class="text-code-string">"upgrade";
        proxy_read_timeout 3600s;
    }
}

Why each piece exists

upstream with keepalive 32. Without keepalive, nginx opens a fresh TCP connection to your app for every request. Under load that is thousands of connections in TIME_WAIT and measurable latency on every hit. Keepalive reuses a pool of connections. This one directive is worth more than most "optimizations."

proxy_http_version 1.1 and the empty Connection header. nginx defaults to HTTP/1.0 to upstreams, which disables keepalive and chunked responses. Setting 1.1 and clearing the Connection header is the required pairing — one without the other does nothing.

The forwarding headers. Your app cannot see the real client IP, the original host, or whether the request was HTTPS unless nginx tells it. Every "why does my app generate http:// links" and "why do all my logs show 127.0.0.1" ticket traces back to a missing line here.

Timeouts, set deliberately. proxy_connect_timeout of 5s means a dead upstream fails fast instead of hanging users for the default 60. proxy_read_timeout of 60s kills requests to a wedged app. The WebSocket location gets an hour, because sockets are supposed to be long-lived — the default 60s is why "the socket disconnects every minute."

The rate limit that saved a launch

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

location /api/ {
    limit_req zone=api burst=20 nodelay;
    proxy_pass http://app;
class="text-code-comment">    # ... same headers as above
}

Ten requests per second per IP, with a burst allowance of 20. During one client's launch, a misbehaving integration hammered their API in a retry loop. The rate limit absorbed it; the app never noticed. Without it, that launch would have been an outage.

Test before you reload

bash
nginx -t && systemctl reload nginx

reload is graceful — old workers finish in-flight requests. There is no reason to restart nginx for config changes, and every reason to test the config first. A syntax error caught by nginx -t is a non-event. The same error on restart is downtime.

Paj Digital Labs

The engineering journal of Paj Digital Solutions — hosting, server infrastructure and web engineering, written by the people who run the servers.

Related reading