nginx 是一款非常優(yōu)秀的代理,靜態(tài)資源,負(fù)載均衡的軟件。用它來(lái)和tomcat結(jié)合,正好可以彌補(bǔ)tomcat靜態(tài)資源處理能力弱的缺點(diǎn)。
配置nginx
1.首先在nginx的conf目錄下新建文件夾vhost 用來(lái)存放虛擬主機(jī)的配置文件(nginx.conf 中默認(rèn)是有包含該文件夾下的所有 conf文件)
mkdir/usr/local/nginx/conf/vhost
2.新建配置文件
viwww.domain.com.conf
內(nèi)容如下
upstreamdumain_server{ server127.0.0.1:80weight=1;#服務(wù)器1ip權(quán)重為1 server118.244.197.83:80weight=1;#服務(wù)器2ip權(quán)重為1 } server{ listen80; server_namewww.domian.com; location/{ roothtml; indexindex.jspindex.htmlindex.htm; proxy_redirectoff; proxy_set_headerHost$host; proxy_set_headerX-Real-IP$remote_addr; proxy_set_headerX-Forwarded-For$proxy_add_x_forwarded_for; proxy_passhttp://domian_server; } error_page500502503504/50x.html;#配置錯(cuò)誤頁(yè)面 location=/50x.html{ roothtml; } }
3.檢查配置文件是否有錯(cuò)
nginx-t
4.如果配置文件沒(méi)錯(cuò)重啟nginx
servicenginxrestart
5.測(cè)試
訪問(wèn)www.domain.com 多次刷新 看是否會(huì)切換頁(yè)面,如果會(huì)的話 第一步就成功啦!
6.配置session共享
session問(wèn)題是負(fù)載均衡必須解決的問(wèn)題,但是當(dāng)集群數(shù)量較多時(shí),不建議采用session共享,因?yàn)楹芎馁Y源。