虽然夜深了,但是还是解决了这个困扰我一个晚上的问题,记录下来备查。
接着我前不久写的这一篇来的:Linux下nginx和tomcat的整合http://hi.baidu.com/gnaiqeh/blog/item/2f43dac9e98d781a7f3e6fc7.html
举个例子,现在是这样的情况:我现在有a、b、c三个不同的应用,每个Tomcat集群机(一共3个)上都建立了这三个应用的虚拟主机,我要把这三个应用用一个nginx来负载均衡。
中间测试了很多次,失败的过程就不多说了,直接说最终解决的办法。
首先要把3个虚拟主机的域名(a.gnaiqeh.cn、b.gnaiqeh.cn、c.gnaiqeh.cn)都指向到nginx机的公网ip上。
然后还是修改nginx的配置文件nginx.conf:
配置文件中upstream段还是保持不变,依旧是3个tomcat集群机的地址及负载因子:
upstream gnaiqeh {
server 192.168.0.11:8080 weight=1;
server 192.168.0.12:8080 weight=1;
server 192.168.0.13:8080 weight=1;
}
server 192.168.0.11:8080 weight=1;
server 192.168.0.12:8080 weight=1;
server 192.168.0.13:8080 weight=1;
}
因为有3个应用,所以应该有3个server段,这里只写其中一个,其他两个只需要修改一下server_name即可:
server {
listen 80;
server_name a.gnaiqeh.cn; #另外两个是b.gnaiqeh.cn、c.gnaiqeh.cn
location / {
root html;
index index.jsp index.html index.htm;
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;
proxy_pass http://gnaiqeh;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
listen 80;
server_name a.gnaiqeh.cn; #另外两个是b.gnaiqeh.cn、c.gnaiqeh.cn
location / {
root html;
index index.jsp index.html index.htm;
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;
proxy_pass http://gnaiqeh;
}
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
}
}
proxy_set_header是nginx的http代理模块中的一个指令。
在nginx中的默认proxy是只能对后面real server做端口转发的,而不能做域名转发,即默认的是:
proxy_set_header Host $proxy_host;
我们要通过域名转发就必须改为:
proxy_set_header Host $host;
最后修改tomcat的配置文件server.xml,主要是配置虚拟主机:
<
Host
name
="a.gnaiqeh.cn"
appBase
="webapps-a"
unpackWARs ="true" autoDeploy ="true"
xmlValidation ="false" xmlNamespaceAware ="false" >
< Context path ="" docBase ="/mnt/a" reloadable ="true" crossContext ="true" />
</ Host >
< Host name ="b.gnaiqeh.cn" appBase ="webapps-b"
unpackWARs ="true" autoDeploy ="true"
xmlValidation ="false" xmlNamespaceAware ="false" >
< Context path ="" docBase ="/mnt/b" reloadable ="true" crossContext ="true" />
</ Host >
< Host name ="c.gnaiqeh.cn" appBase ="webapps-c"
unpackWARs ="true" autoDeploy ="true"
xmlValidation ="false" xmlNamespaceAware ="false" >
< Context path ="" docBase ="/mnt/c" reloadable ="true" crossContext ="true" />
</ Host >
unpackWARs ="true" autoDeploy ="true"
xmlValidation ="false" xmlNamespaceAware ="false" >
< Context path ="" docBase ="/mnt/a" reloadable ="true" crossContext ="true" />
</ Host >
< Host name ="b.gnaiqeh.cn" appBase ="webapps-b"
unpackWARs ="true" autoDeploy ="true"
xmlValidation ="false" xmlNamespaceAware ="false" >
< Context path ="" docBase ="/mnt/b" reloadable ="true" crossContext ="true" />
</ Host >
< Host name ="c.gnaiqeh.cn" appBase ="webapps-c"
unpackWARs ="true" autoDeploy ="true"
xmlValidation ="false" xmlNamespaceAware ="false" >
< Context path ="" docBase ="/mnt/c" reloadable ="true" crossContext ="true" />
</ Host >
3台集群机均改成上面一样的。
然后重启nginx,重启tomcat,测试访问三个域名都通过,打完收工。