Skip to content
Nginx

Reverse Proxy

Forward requests to upstream HTTP services.

By EZ4Code Team
proxyupstreamwebsocket

Code

server {
    listen 80;
    server_name api.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;

        # WebSocket support
        proxy_set_header Upgrade    $http_upgrade;
        proxy_set_header Connection "upgrade";

        proxy_read_timeout 60s;
        proxy_buffering off;     # streaming responses
    }

    # Health check endpoint
    location = /healthz {
        proxy_pass http://127.0.0.1:3000/health;
    }
}

Explanation

proxy_pass forwards requests to a backend service, with proxy_set_header preserving client information through the proxy. The Upgrade/Connection headers enable WebSocket connections to pass through. proxy_buffering off streams responses immediately, useful for Server-Sent Events.

More Nginx Snippets