nginx+tomcat集群負(fù)載均衡中的多虛擬主機(jī)配置
關(guān)鍵詞:nginx tomcat 多虛擬主機(jī) 集群 負(fù)載均衡
雖然夜深了,但是還是解決了這個(gè)困擾我一個(gè)晚上的問題,記錄下來備查。
接著我前不久寫的這一篇來的:Linux下nginx和tomcat的整合http://hi.baidu.com/gnaiqeh/blog/item/2f43dac9e98d781a7f3e6fc7.html
舉個(gè)例子,現(xiàn)在是這樣的情況:我現(xiàn)在有a、b、c三個(gè)不同的應(yīng)用,每個(gè)Tomcat集群機(jī)(一共3個(gè))上都建立了這三個(gè)應(yīng)用的虛擬主機(jī),我要把這三個(gè)應(yīng)用用一個(gè)nginx來負(fù)載均衡。
中間測試了很多次,失敗的過程就不多說了,直接說最終解決的辦法。
首先要把3個(gè)虛擬主機(jī)的域名(a.gnaiqeh.cn、b.gnaiqeh.cn、c.gnaiqeh.cn)都指向到nginx機(jī)的公網(wǎng)ip上。
然后還是修改nginx的配置文件nginx.conf:
配置文件中upstream段還是保持不變,依舊是3個(gè)tomcat集群機(jī)的地址及負(fù)載因子:
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;
}
因?yàn)橛?個(gè)應(yīng)用,所以應(yīng)該有3個(gè)server段,這里只寫其中一個(gè),其他兩個(gè)只需要修改一下server_name即可:
server {
listen 80;
server_name a.gnaiqeh.cn; #另外兩個(gè)是b.gnaiqeh.cn、c.gnaiqeh.cn
location / {
root html;
index index.jsp index.html index.htm;
proxy_redirect off;
proxy_set_header Host $host; #這一句至關(guān)重要
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代理模塊中的一個(gè)指令。
在nginx中的默認(rèn)proxy是只能對后面real server做端口轉(zhuǎn)發(fā)的,而不能做域名轉(zhuǎn)發(fā),即默認(rèn)的是:
proxy_set_header Host $proxy_host;
我們要通過域名轉(zhuǎn)發(fā)就必須改為:
proxy_set_header Host $host;
最后修改tomcat的配置文件server.xml,主要是配置虛擬主機(jī):
<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>
3臺集群機(jī)均改成上面一樣的。
然后重啟nginx,重啟tomcat,測試訪問三個(gè)域名都通過,打完收工。
posted on 2011-04-03 11:03 都市淘沙者 閱讀(2269) 評論(0) 編輯 收藏 所屬分類: lighttpd/nginx/HA/LVS/FTP