Devops

nginx as a Reverse Proxy

·1 min read

nginx is the workhorse reverse proxy of the internet. It terminates TLS, load-balances across application servers, caches, compresses, rate-limits, and handles a thousand other small responsibilities you'd otherwise build into each backend. This is a working production configuration, annotated.

A complete server block

upstream app {
    server 127.0.0.1:3000 max_fails=3 fail_timeout=30s;
    server 127.0.0.1:3001 max_fails=3 fail_timeout=30s;
    keepalive 32;
}

server {
    listen 443 ssl http2;
    listen [::]:443 ssl http2;
    server_name example.com www.example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_prefer_server_ciphers off;

    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;
    add_header X-Content-Type-Options nosniff always;
    add_header X-Frame-Options SAMEORIGIN always;
    add_header Referrer-Policy strict-origin-when-cross-origin always;

    client_max_body_size 25m;
    gzip on;
    gzip_types text/plain text/css application/json application/javascript text/xml application/xml;

    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 "";   # required for keepalive to upstream

        proxy_read_timeout 60s;
        proxy_connect_timeout 5s;
    }

    location /static/ {
        alias /var/www/example.com/static/;
        expires 30d;
        access_log off;
    }
}

server {
    listen 80;
    listen [::]:80;
    server_name example.com www.example.com;
    return 301 https://$host$request_uri;
}

WebSocket upgrades

WebSockets need the Upgrade and Connection headers passed through. The classic snippet:

map $http_upgrade $connection_upgrade {
    default upgrade;
    ''      close;
}

location /ws {
    proxy_pass http://app;
    proxy_http_version 1.1;
    proxy_set_header Upgrade    $http_upgrade;
    proxy_set_header Connection $connection_upgrade;
    proxy_read_timeout 3600s;     # long-lived
}

Rate limiting

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;
}

Caching upstream responses

proxy_cache_path /var/cache/nginx levels=1:2 keys_zone=cache:50m max_size=2g inactive=24h;

location /api/public/ {
    proxy_cache cache;
    proxy_cache_valid 200 5m;
    proxy_cache_use_stale error timeout updating;
    add_header X-Cache-Status $upstream_cache_status;
    proxy_pass http://app;
}

Reload, don't restart

nginx -t          # test config
systemctl reload nginx     # graceful — workers finish in-flight requests

Related