Nginx Reverse Proxy Configuration Guide

Production-ready configurations for HTTP, WebSocket, and HTTPS.

Back to Guides ยท Home

Prerequisite: Nginx is installed and running.

Configuration location: /etc/nginx/conf.d/your-domain.conf

1. Basic HTTP Reverse Proxy

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_pass http://127.0.0.1:8080;
        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;
    }
}

2. WebSocket Proxy (for ws:// or socket.io)

location /ws/ {
    proxy_pass http://127.0.0.1:8080;
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "upgrade";
    proxy_set_header Host $host;
}

3. Common Timeout and Request Body Settings

client_max_body_size 20m;
proxy_connect_timeout 60s;
proxy_read_timeout 60s;
proxy_send_timeout 60s;

4. Test and Reload Configuration

sudo nginx -t
sudo systemctl reload nginx

5. HTTPS Deployment

To enable HTTPS, first obtain an SSL certificate, then configure the certificate paths in your server block.

server {
    listen 443 ssl;
    server_name example.com;

    ssl_certificate     /etc/letsencrypt/live/example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:8080;
        # ... proxy settings above
    }
}

For certificate issuance, see the SSL Certificate Guide below.