Nginx as a Reverse Proxy and Load Balancer
1. Basic Reverse Proxy Configuration
Example: Proxying Requests to a Backend Server
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_app;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}
upstream backend_app {
server 127.0.0.1:5000;
}2. Proxying Dynamic Content to Application Servers
Example: Proxying Requests to a Node.js Application
Example: Proxying Requests to a Spring Boot Application
3. Load Balancing Algorithms
1. Round Robin (Default)
2. Least Connections
3. IP Hash (Sticky Sessions)
4. Health Checks & Failover Strategies
Example: Setting Up Health Checks
5. Handling WebSockets with Nginx
Example: Reverse Proxy WebSockets
6. Handling CORS with Nginx
Example: Allowing CORS for APIs
7. Conclusion
Last updated