◷ Reading Time: 5 minutes
Load balancing across multiple application instances is a commonly used technique for optimizing resource utilization, maximizing throughput, reducing latency, and ensuring fault‑tolerant configurations.
Watch the NGINX Plus for Load Balancing and Scaling webinar on demand for a deep dive on techniques that NGINX users employ to build large‑scale, highly available web services.
NGINX and NGINX Plus can be used in different deployment scenarios as a very efficient HTTP load balancer. The basic version is free, and Nginx+ needs subscription and that includes Connection Limiting, session persistence, active health features.
HTTP Load Balancing
When load balancing HTTP traffic, NGINX Plus terminates each HTTP connection and processes each request individually. You can strip out SSL/TLS encryption, inspect and manipulate the request, queue the request using rate limits, and then select the load‑balancing policy.
To improve performance, NGINX Plus can automatically apply a wide range of optimizations to an HTTP transaction, including HTTP protocol upgrades, keep alive optimization, and transformations such as content compression and response caching.
Load balancing HTTP traffic is easy with NGINX:
http {
upstream my_upstream {
server server1.example.com;
server server2.example.com;
}
server {
listen 80;
location / {
proxy_set_header Host $host;
proxy_pass http://my_upstream;
}
}
}TCP and UDP Load Balancing
NGINX Plus can also load balance TCP applications such as MySQL, and UDP applications such as DNS and RADIUS. For TCP applications, NGINX Plus terminates the TCP connections and creates new connections to the backend.
The configuration is similar to HTTP: specify a virtual server with the server directive, listen for traffic on a port, and proxy_pass the request to an upstream group.
# Define a stream context for TCP load balancing
stream {
log_format mysql_log '$remote_addr [$time_local] '
'$protocol $status $bytes_sent $bytes_received '
'$session_time';
access_log /var/log/nginx/mysql_access.log mysql_log;
# Define an upstream group of MySQL servers
upstream mysql_backend {
# Define your MySQL servers here
server mysql1.example.com:3306;
server mysql2.example.com:3306;
server mysql3.example.com:3306;
# Optional: Specify load balancing method (default is round-robin)
# least_conn; # Uncomment to use least connections method
# ip_hash; # Uncomment to use IP hash method
}
# Define a server block to listen for MySQL connections
server {
listen 3306;
proxy_pass mysql_backend;
# Optional: Enable proxy protocol
# proxy_protocol on;
# Health checks to ensure only healthy servers receive traffic
health_check interval=10s timeout=2s fall=3 rise=2;
}
}
Load Balancing Methods
NGINX Plus supports a number of application load‑balancing methods for HTTP, TCP, and UDP load balancing. All methods take into account the weight you can optionally assign to each upstream server.
- Round Robin (the default) – Distributes requests across the upstream servers in order.
- Least Connections – Forwards requests to the server with the lowest number of active connections.
- Least Time – Forwards requests to the least‑loaded server, based on a calculation that combines response time and number of active connections. Exclusive to NGINX Plus.
- Hash – Distributes requests based on a specified key, for example client IP address or request URL. NGINX Plus can optionally apply a consistent hash to minimize redistribution of loads if the set of upstream servers changes.
- IP Hash (HTTP only) – Distributes requests based on the first three octets of the client IP address.
- Random with Two Choices – Picks two servers at random and forwards the request to the one with the lower number of active connections (the Least Connections method). With NGINX Plus, the Least Time method can also be used.
How to use NginX with FlexRule
The best practice For FlexRule environment is to Load balance traffic between Master Management nodes, Master Execution nodes, and Master Workbench nodes separately.
- Download Nginx, based on the machine OS.
> unzip nginx-1.25.2.zip
2. Extract it and edit the config file (on Windows its located in conf/nginx.conf).
this is a sample to load balance 2 Master execution nodes as below:
server1: 192.168.1.111:9001
server2: 192.168.1.112:9002
http {
log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"';
access_log /var/log/nginx/access.log main;
error_log /var/log/nginx/error.log warn;
upstream master_execution_servers {
server 192.168.1.111:9001;
server 192.168.1.112:9002;
}
server {
listen 80;
location / {
proxy_pass http://master_execution_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;
}
}
}3. Start the Nginx service
> cd nginx-[ver]
> start nginx