Back to All Diagnostic Tools
HIGH-PERFORMANCE INFRASTRUCTURE BUILDER • LIVE V2.4

NGINX Reverse Proxy Config Generator

Generate battle-tested, production-ready NGINX reverse proxy configuration blocks tailored for HLS/MPEG-TS video streaming, WebSocket microservices, and high-concurrency Node.js web applications. Tuned to completely eliminate 502 Bad Gateway buffer overflows and origin timeouts.

Anti-502 Header Buffer Tuning
Hardened TLS 1.3 & Cloudflare Real-IP
Zero-Disk Buffer HLS Manifest Routing

Server Directives & Workload

/etc/nginx/conf.d/stream.example.com.conf
nginx -t validator
UTF-8 • UNIX
# ==============================================================================
# BufferFixer Hardened NGINX Reverse Proxy Configuration
# Target Domain: stream.example.com
# Workload Profile: HLS/MPEG-TS Live Video Streaming (Zero-Disk Lag)
# Generated: 2026-12-04T20:34:05.081Z
# ==============================================================================

upstream backend_cluster {
    server 127.0.0.1:8080 max_fails=3 fail_timeout=10s;
    keepalive 64;
}

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

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

    # TLS 1.3 & Modern Hardened Ciphers
    ssl_certificate /etc/letsencrypt/live/stream.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/stream.example.com/privkey.pem;
    ssl_protocols TLSv1.2 TLSv1.3;
    ssl_ciphers 'ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384';
    ssl_prefer_server_ciphers off;
    ssl_session_cache shared:SSL:10m;
    ssl_session_timeout 1d;
    ssl_session_tickets off;

    # Cloudflare Real-IP Restoration
    set_real_ip_from 173.245.48.0/20;
    set_real_ip_from 103.21.244.0/22;
    set_real_ip_from 103.22.200.0/22;
    set_real_ip_from 103.31.4.0/22;
    set_real_ip_from 141.101.64.0/18;
    set_real_ip_from 108.162.192.0/18;
    set_real_ip_from 190.93.240.0/20;
    set_real_ip_from 188.114.96.0/20;
    set_real_ip_from 197.234.240.0/22;
    set_real_ip_from 198.41.128.0/17;
    set_real_ip_from 162.158.0.0/15;
    set_real_ip_from 104.16.0.0/13;
    set_real_ip_from 104.24.0.0/14;
    set_real_ip_from 172.64.0.0/13;
    set_real_ip_from 131.0.72.0/22;
    set_real_ip_from 2400:cb00::/32;
    set_real_ip_from 2606:4700::/32;
    set_real_ip_from 2803:f800::/32;
    set_real_ip_from 2405:b500::/32;
    set_real_ip_from 2405:8100::/32;
    set_real_ip_from 2a06:98c0::/29;
    set_real_ip_from 2c0f:f248::/32;
    real_ip_header CF-Connecting-IP;

    # Hardened Security & Performance Headers
    add_header X-Content-Type-Options "nosniff" always;
    add_header X-Frame-Options "SAMEORIGIN" always;
    add_header X-XSS-Protection "1; mode=block" always;
    add_header Strict-Transport-Security "max-age=31536000; includeSubDomains" always;

    # HLS Live Manifests (.m3u8): Zero-Disk Lag & Real-Time Sync
    location ~* \.m3u8$ {
        proxy_pass http://backend_cluster;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_buffering off;
        proxy_cache_bypass 1;
        proxy_no_cache 1;
        expires -1;
        add_header Cache-Control "no-cache, no-store, must-revalidate";
        add_header Access-Control-Allow-Origin "*" always;
        add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
    }

    # HLS/MPEG-TS Video Chunks (.ts, .m4s): Memory Buffers & Micro-Cache
    location ~* \.(ts|m4s|mp4)$ {
        proxy_pass http://backend_cluster;
        proxy_http_version 1.1;
        proxy_set_header Connection "";
        proxy_buffers 16 512k;
        proxy_buffer_size 256k;
        proxy_busy_buffers_size 512k;
        proxy_read_timeout 300s;
        expires 12h;
        add_header Cache-Control "public, max-age=43200, immutable";
        add_header Access-Control-Allow-Origin "*" always;
        add_header Access-Control-Allow-Methods "GET, OPTIONS" always;
    }

    # Primary Reverse Proxy Location
    location / {
        proxy_pass http://backend_cluster;
        proxy_http_version 1.1;

        # Upstream Host & Client Identity Headers
        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;

        # WebSocket & Long-Polling Connection Upgrade
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection $connection_upgrade;

        # Anti-502/504 Upstream Buffer & Timeout Tuning
        proxy_buffer_size 128k;
        proxy_buffers 8 256k;
        proxy_busy_buffers_size 256k;
        proxy_connect_timeout 60s;
        proxy_send_timeout 120s;
        proxy_read_timeout 300s;
        proxy_next_upstream error timeout http_502 http_503 http_504;

    }
}
Deploy & Reload Commands (Ubuntu / Debian / CentOS):
sudo nginx -t && sudo systemctl reload nginx

Reverse Proxy Engineering: Eliminating 502 Bad Gateway & Video Stalls

When NGINX operates as a reverse proxy for video transcoders or web applications, the default configuration allocates minimal memory buffers (proxy_buffer_size 4k). When an upstream application sends large response headers (such as authentication tokens or streaming segment metadata), NGINX aborts the socket connection and outputs an upstream sent too big header while reading response header from upstream error, manifesting to users as HTTP 502 Bad Gateway.

Why HLS Manifests (.m3u8) Must Bypass Disk Buffering

In live linear streaming, manifest playlists (.m3u8) update every 2 to 6 seconds with new chunk sequences. If NGINX buffers or caches the playlist on disk, video players receive stale chunk numbers, causing player buffer under-runs, loopback replay, and severe synchronization lag. Setting proxy_buffering off for manifests streams the index directly from the origin in real time.

Tuning Buffer Windows for Video Chunks (.ts / .m4s)

Unlike playlists, video segments are immutable and weigh between 1 MB and 8 MB. Allocating dedicated memory buffers (proxy_buffers 16 512k; proxy_buffer_size 256k;) allows NGINX to absorb high-speed bursts from the upstream server without writing intermediate temporary files to disk, saving NVMe I/O cycles and preventing chunk delivery latency spikes.

Frequently Asked Technical Questions

How do I fix NGINX "upstream sent too big header" 502 errors?

Increase your header buffer allocations in the location / block: set proxy_buffer_size 128k; and proxy_buffers 8 256k;. For full troubleshooting logs, see our in-depth blueprint on How to Resolve NGINX 502 Bad Gateway.

Why does Cloudflare show Error 522 Connection Timed Out with NGINX?

Cloudflare Error 522 indicates the Cloudflare Anycast edge was unable to complete a 3-way TCP handshake with your origin NGINX server within 15 seconds. This is typically caused by dropped SYN packets or firewall blocking. Follow our guide on How to Fix Cloudflare Error 522 Connection Timed Out.

What does proxy_next_upstream do?

proxy_next_upstream error timeout http_502 http_503; instructs NGINX to automatically retry a failed request on another server in your upstream cluster before returning an error to the user, providing transparent failover.

Need Custom NGINX Architecture or Cluster Tuning?

Our systems engineers can deploy HAProxy/NGINX load balancing, calibrate keepalive pools, and optimize stream transcoding buffers.

Consult Systems Engineer