NGINX is a powerful, high-performance web server, reverse proxy, and load balancer. It is widely used to distribute incoming traffic across multiple backend servers, ensuring high availability, fault tolerance, and efficient load distribution. This document covers setting up NGINX as a Load Balancer and Reverse Proxy.
A reverse proxy works by accepting client requests and forwarding them to a backend server. NGINX can serve as a reverse proxy to forward requests to multiple backend servers. This setup improves security, load distribution, and caching of content.
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_servers;
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;
}
}
In this example, requests to example.com
are forwarded to a group of backend servers defined as backend_servers
.
NGINX can also distribute incoming traffic across multiple backend servers to ensure better performance and redundancy. It balances the load between servers using various algorithms.
- Round Robin - Distributes requests to backend servers in a sequential, circular order.
- Least Connections - Sends traffic to the server with the fewest active connections.
- IP Hash - Routes requests based on the client’s IP address to ensure consistent routing.
To configure NGINX as a load balancer, you'll define a set of backend servers under the upstream
directive.
http {
upstream backend_servers {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
server {
listen 80;
server_name example.com;
location / {
proxy_pass http://backend_servers;
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;
}
}
}
In this configuration, NGINX will forward requests to one of the backend_servers
.
Round Robin is the default method used by NGINX. Requests are distributed sequentially to each server in the upstream
group.
upstream backend_servers {
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
The least_conn
directive sends requests to the server with the least active connections.
upstream backend_servers {
least_conn;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
The ip_hash
directive ensures that each client is routed to the same backend server based on their IP address.
upstream backend_servers {
ip_hash;
server backend1.example.com;
server backend2.example.com;
server backend3.example.com;
}
-
Start NGINX: After configuring the NGINX configuration file, restart NGINX.
sudo systemctl restart nginx
-
Check the Load Balancing: You can monitor the distribution of requests to the backend servers by checking the logs or using monitoring tools like
htop
ornetstat
on the backend servers. -
Test Failover: You can simulate server failures to test if NGINX properly reroutes traffic to healthy backend servers. Disable a backend server and observe if traffic is redirected to other servers.
Using NGINX as a reverse proxy and load balancer helps improve the scalability, availability, and security of your web applications. By distributing traffic intelligently across multiple servers, NGINX ensures that the load is balanced and the system remains responsive even under heavy traffic.