Nginx as a Reverse Proxy and Load Balancer
Nginx is a high-performance web server that is widely used as a reverse proxy and load balancer for handling traffic to backend application servers. It efficiently manages request routing, distributes load, and improves fault tolerance.
1. Basic Reverse Proxy Configuration
A reverse proxy forwards client requests to backend servers, which helps in load balancing, caching, and security.
Example: Proxying Requests to a Backend Server
In this example:
Requests to
example.com
are forwarded to the backend application at127.0.0.1:5000
.Headers are preserved to ensure correct request information.
2. Proxying Dynamic Content to Application Servers
Modern applications use various backend technologies. Nginx can proxy requests to Node.js, Python, Java (Spring Boot), PHP, etc.
Example: Proxying Requests to a Node.js Application
Here, requests are forwarded to a Node.js app running on port 3000
.
Example: Proxying Requests to a Spring Boot Application
This proxies API calls to a Spring Boot backend running on 8080
.
3. Load Balancing Algorithms
Nginx supports multiple load balancing strategies to distribute traffic across multiple backend servers.
1. Round Robin (Default)
Requests are evenly distributed among available servers.
2. Least Connections
Traffic is routed to the server with the fewest active connections.
3. IP Hash (Sticky Sessions)
Requests from the same client IP always go to the same backend server.
4. Health Checks & Failover Strategies
Nginx can detect failing servers and redirect traffic accordingly.
Example: Setting Up Health Checks
If
app1.example.com
fails 3 times within 30 seconds, it is marked as down.Traffic is automatically rerouted to
app2.example.com
.
5. Handling WebSockets with Nginx
WebSockets require special handling in Nginx to support persistent connections.
Example: Reverse Proxy WebSockets
WebSockets use the
Upgrade
header for persistent connections.The
proxy_http_version 1.1;
directive ensures WebSocket compatibility.
6. Handling CORS with Nginx
CORS (Cross-Origin Resource Sharing) must be configured when serving APIs to prevent security issues.
Example: Allowing CORS for APIs
This configuration allows cross-origin requests and preflight OPTIONS
requests for APIs.
7. Conclusion
Nginx is a powerful tool for proxying requests, balancing traffic, and optimizing application performance. By using the right configurations, you can:
Distribute load across multiple servers efficiently.
Ensure high availability with failover strategies.
Handle WebSockets and CORS seamlessly.
Last updated