Nginx Proxy Headers, WebSockets & HTTP/2
Configure reverse proxy headers (X-Forwarded-For, X-Real-IP), WebSocket proxy upgrades (Socket.io, HMR), and HTTP/2 protocol support in Nginx.
Nginx Proxy Headers, WebSockets & HTTP/2
When proxying traffic, backend applications only see the IP address of Nginx (127.0.0.1) unless you explicitly forward client identity headers. Furthermore, real-time protocols like WebSockets require protocol upgrade headers.
1. Essential Proxy 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;
proxy_set_header X-Forwarded-Host $host;
proxy_set_header X-Forwarded-Port $server_port;| Header | Value | Purpose |
|---|---|---|
Host | $host | Passes the original requested domain name to the application. |
X-Real-IP | $remote_addr | Contains the true IP address of the client visitor. |
X-Forwarded-For | $proxy_add_x_forwarded_for | Comma-separated list of IP proxies traversed by the client. |
X-Forwarded-Proto | $scheme | Tells the backend whether the user connected via http or https. |
2. Proxying WebSockets (Socket.io, HMR, Realtime APIs)
HTTP/1.1 allows upgrading a TCP connection to a full-duplex WebSocket connection.
Step 1: Define Connection Upgrade Map in http context (nginx.conf)
# Add inside the http { ... } block in /etc/nginx/nginx.conf
map $http_upgrade $connection_upgrade {
default upgrade;
'' close;
}Step 2: Configure Server Location Block
server {
listen 443 ssl;
server_name chat.example.com;
# SSL configuration here...
location / {
proxy_pass http://127.0.0.1:4000;
proxy_http_version 1.1;
# WebSocket Upgrade Headers
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection $connection_upgrade;
# Standard 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;
# Prevent socket from dropping during idle periods
proxy_read_timeout 86400s;
proxy_send_timeout 86400s;
}
}3. Enabling HTTP/2 Support
HTTP/2 allows multiplexing multiple concurrent HTTP requests over a single TCP connection, drastically accelerating page load times.
In Modern Nginx (v1.25.1+):
server {
listen 443 ssl;
listen [::]:443 ssl;
http2 on;
server_name example.com;
# SSL certs...
}In Legacy Nginx (v1.9.5 - v1.24):
server {
listen 443 ssl http2;
listen [::]:443 ssl http2;
server_name example.com;
# SSL certs...
}Nginx Reverse Proxy & Upstream Load Balancing
Configure Nginx as a high-performance reverse proxy β proxy_pass syntax, upstream load balancing algorithms (round-robin, least_conn, ip_hash), and keepalive tuning.
Nginx SSL/TLS Hardening & Let's Encrypt Certbot
Harden Nginx SSL/TLS security with modern TLS 1.2/1.3 ciphers, HSTS preloading, OCSP stapling, session caching, and Certbot automation to achieve an A+ SSL Labs rating.