HowTo setup nginx as a reverse load-balancer
Posted on 14th November 2008 by Guillaume MoigneuWe were facing at work a high load on a TomCat application server that is currently configured with Apache acting as a proxy. We had to increase the number of server in order to correctly handle the load. But how do you load-balance multiple TomCat instances ?
We began to test it with Apache’s mod_proxy_balancer but you can only set a theorical server weight (for example : server1 70% of requests, server2 30% of requests). So we began to search for another solution that would do the load-balancing without any new hardware other than simple linux dedicated servers.
After several minutes where we hesitated between nginx and Squid, we actually decided to put nginx on a staging server… and that was wonderfull (even flabbergasting) !
The new setup is as follow :
- Debian Core2Duo 2.66Ghz with 2Gb RAM configured with nginx as a reverse proxy (with load-balancing),
- 2 Debian Quadri-Xeon 3.00Ghz with 8Gb RAM to handle two TomCat instances.
Here is my nginx config with the fair module which test the load on both servers before sending them the real request :
worker_processes 1;
error_log logs/error.log notice;
pid logs/nginx.pid;
events {
worker_connections 1024;
}
http {
include mime.types;
default_type application/octet-stream;
log_format lb '[$time_local] $remote_addr => $upstream_addr($upstream_status) in $upstream_response_time$
access_log logs/lb.log lb;
sendfile on;
tcp_nopush on;
keepalive_timeout 0;
gzip on;
upstream novacat {
server srv1.xxx.eu:8080;
server srv3.xxx.eu:8080;
fair;
}
server {
listen 80;
server_name xxx.eu;
location / {
proxy_pass http://xxx;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect false;
}
}
}
Popularity: 100% [?]











