nginx+tomcat集群负载均衡中的多虚拟主机配置
雖然夜深了,但是還是解決了這個(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ù)載均衡。
中間測(cè)試了很多次,失敗的過程就不多說了,直接說最終解決的辦法。
首先要把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ù)載因子:
??????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即可:
????????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是只能對(duì)后面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ī):
????????????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臺(tái)集群機(jī)均改成上面一樣的。
然后重啟nginx,重啟tomcat,測(cè)試訪問三個(gè)域名都通過,打完收工。
總結(jié)
以上是生活随笔為你收集整理的nginx+tomcat集群负载均衡中的多虚拟主机配置的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: hiveserver2 kerberos
- 下一篇: [分享] 数学学术资源站点