Nginx Reverse Proxy Configuration
Nginx is the most common reverse proxy in front of Node.js, Python, and Go application servers. This configuration proxies requests to an upstream application, adds real IP forwarding headers, configures appropriate timeouts, and supports WebSocket upgrades for real-time applications. The Nginx config formatter normalizes indentation and validates directive syntax before deployment. Test the configuration with nginx -t and monitor the error log after applying changes.
Example
upstream app {
server 127.0.0.1:3000;
server 127.0.0.1:3001;
keepalive 32;
}
server {
listen 80;
server_name app.example.com;
location / {
proxy_pass http://app;
proxy_http_version 1.1;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
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_connect_timeout 10s;
proxy_read_timeout 60s;
}
}FAQ
- Why should I set X-Forwarded-For in Nginx?
- Without this header, your application sees only the Nginx server IP as the client address. X-Forwarded-For passes the original client IP so you can use it for logging, rate limiting, and geographic restrictions.
- How do I enable WebSocket support in Nginx?
- Set proxy_http_version 1.1 and pass the Upgrade and Connection headers. This upgrades the HTTP connection to the WebSocket protocol that your backend expects.
- What load balancing algorithms does Nginx support?
- The default round-robin, least_conn (fewest active connections), ip_hash (sticky sessions by client IP), and with Nginx Plus: least_time and random. Specify the algorithm before the server directives in the upstream block.
Related Examples
Nginx SSL and Security Configuration
A secure Nginx configuration enables TLS 1.2 and 1.3 only, disables weak ciphers...
Dockerfile for a Node.js ApplicationA well-structured Node.js Dockerfile uses a multi-stage build to keep the final ...
Parse CORS HTTP Response HeadersCORS misconfigurations are responsible for a large share of frontend API integra...