Configuring Nginx as a Reverse Proxy

2 min read

Authors
banner

Configuring Reverse Proxy

Complete Production Setup

server {
    listen 80;
    server_name api.example.com;

    location / {
        proxy_pass http://backend:3000;

        # Forward original 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;

        # Timeouts
        proxy_connect_timeout 60s;
        proxy_send_timeout 60s;
        proxy_read_timeout 60s;

        # Buffering
        proxy_buffering on;
        proxy_buffer_size 128k;
        proxy_buffers 4 256k;
    }
}

Key Directives

  • proxy_pass - Where to forward the request
  • proxy_set_header - Add or modify headers
  • proxy_connect_timeout - Connection establishment timeout
  • proxy_read_timeout - Time to read response
  • proxy_buffering - Buffer backend responses

Important Headers to Forward

HeaderPurpose
HostOriginal host
X-Real-IPReal client IP
X-Forwarded-ForAll IPs in chain
X-Forwarded-ProtoOriginal protocol (http/https)

Production-ready!

© 2025 Vijay Rajendran