Nginx + Tomcat 负载均衡
一、什么是Nginx?
nginx [engine x]是Igor Sysoev编写的一个HTTP和反向代理服务器,另外它也可以作为邮件代理服务器。 它已经在众多流量很大的俄罗斯网站上使用了很长时间,这些网站包括Yandex、Mail.Ru、VKontakte,以及Rambler。据Netcraft统计,在2012年8月份,世界上最繁忙的网站中有11.48%使用Nginx作为其服务器或者代理服务器(详见:http://nginx.org/cn/ 或者 google)。
二、在Windows 下Nginx+Tomcat 配置
Nginx :http://nginx.org/
Tomcat:http://tomcat.apache.org/
我本机环境是 Windows XP+Nginx 1.0.15+ apache-tomcat-7.0.34
1、Nginx安装:Nginx下载解压到本机一无空格、汉字的任意目录即可。
2、conf/nginx.conf 配置文件修改
a:修改Nginx默认端口:
server {
listen 80;
server_name localhost;
......
}
此处的listen 即为监听,默认80 端口,若有需要可修改为其它端口。
b:在keepalive_timeout 后面增加
upstream location {
#weigth 参数表示权值,权值越高被分配到的几率越大
server 127.0.0.1:8048 weight=3;
server 127.0.0.1:8049 weight=2;
ip_hash;
}
c:修改
location / {
root html;
index index.html index.htm;
}
为
location / {
root html;
index index.html index.htm;
proxy_pass http://location;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 90;
proxy_send_timeout 90;
proxy_read_timeout 90;
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
}
3、修改Tomcat server.xml中端口配置,让本机能运行多个Tomcat,注意Tomcat发布端口要和2.a 中映射端口一致。
到此配置完成,输入 http://127.0.0.1:80(或自定义端口)即可访问