nginx可以作为Web前置机,将客户请求按照一定算法合理分配给后置的tomcat应用服务器,从而达到均衡目的。当然如果应用使用了session会话,后
置的tomcat应用还要保证session共享才能达到应用集群效果。本文不讨论tomcat的集群问题,重点讨论nginx如何配置使后置的tomcat负载均衡用户
请求。
nginx.conf中,http { 下加入如下upstream块,指明后置的两台tomcat:
upstream tomcat {
server localhost:8280 weight=1 max_fails=1 fail_timeout=30s;
server localhost:8180 weight=4 max_fails=2 fail_timeout=30s;
}
同时定义server块中的proxy_pass,指向上面定义的upstream:
server {
listen 8888;
server_name api.my.com;
access_log logs/api.zfwx.com main;
location / {
root html;
index index.html index.htm;
proxy_connect_timeout 3;
proxy_send_timeout 30;
proxy_read_timeout 30;
proxy_pass http://tomcat;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
此时,客户端访问http://api.my.com:8888 请求会分别转向后置的两个tomcat。