Nginx作為目前主流的服務(wù)端負(fù)載均衡可以非常高效支持高并發(fā)和大數(shù)據(jù)量訪問(wèn),相比較apache的負(fù)載均衡有很大優(yōu)勢(shì)盗蟆,以下以linux系統(tǒng)為例簡(jiǎn)單記錄了nginx與多個(gè)tomcat配合實(shí)現(xiàn)的負(fù)載均衡配置。
一、Nginx 服務(wù)端安裝:
1坏匪、下載nginx穩(wěn)定版本:http://nginx.org/en/download.html (當(dāng)前是1.6.2)
2、執(zhí)行解壓:
# tar -zxvf nginx-1.6.2.tar.gz
3撬统、組件安裝:
# yum -y install pcre pcre-devel openssl openssl-devel
4适滓、編譯安裝nginx到/usr/local/nginx目錄
# ./configure --prefix=/usr/local/nginx
5、啟動(dòng)nginx
# /usr/local/nginx/sbin/nginx
6恋追、查看是否安裝成功運(yùn)行 netstat -ltnp 能看到80端口已開(kāi)放凭迹,瀏覽器輸入地址能看到Welcome to nginx!
二、nginx的tomcat均衡負(fù)載配置:
1苦囱、準(zhǔn)備2個(gè)tomcat配置不同的端口(3處,如果不在同一個(gè)機(jī)器不需要修改端口)
<Server port="8015" shutdown="SHUTDOWN">
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8" />
<Connector port="18001" protocol="AJP/1.3" redirectPort="8443" />
2蕊苗、配置分載,進(jìn)入/usr/local/nginx/conf目錄vi
upstream myapp1{
server 127.0.0.1:8080 weight=1;
server 127.0.0.1:8081 weight=1;
}
server {
listen 80;
server_name localhost;
location / {
root html;
index index.html index.htm;
proxy_pass http://myapp1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
其他:
1沿彭、添加模塊
進(jìn)入nginx的安裝文件目錄重新執(zhí)行configure 并通過(guò)--with-xxx來(lái)添加模塊朽砰,如下,執(zhí)行后通過(guò)./make 進(jìn)行編譯喉刘。然后將objs目錄下的nginx移動(dòng)到nginx目錄(首次執(zhí)行時(shí)的--prefix指向目錄的sbin中替換同名文件)
./configure --prefix=/usr/local/nginx/sbin/nginx --with-http_stub_status_module --with-http_proxy_module
測(cè)試:
tail -f /home/firefly/webServ/tomcat-6.0.37-1/logs/catalina.out