nginx+tomcat的负载均衡
生活随笔
收集整理的這篇文章主要介紹了
nginx+tomcat的负载均衡
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
nginx+tomcat的負載均衡
一. Nginx的安裝配置
1. Nginx服務器端
Nginx服務器的IP:192.168.5.108
Tomcat01的IP:?? 192.168.5.102
Tomcat02的IP:?? 192.168.5.103
2.#wget http://sysoev.ru/nginx/nginx-0.6.26.tar.gz
#tar zxvf nginx-0.6.26.tar.gz
#cd nginx-0.6.26
#[root@test nginx-0.6.26]# ./configure --with-http_stub_status_module
如果產生如下錯誤請執行第3步:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
3.[root@test nginx-0.6.26]# rpm -qa|grep pcre
pcre-6.6-1.1
[root@test suantop]# rpm -ivh pcre-devel-6.6-1.1.i386.rpm?
warning: pcre-devel-6.6-1.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...??????????????? ########################################### [100%]
?? 1:pcre-devel???????????? ########################################### [100%]
[root@test suantop]# rpm -qa|grep pcre
pcre-6.6-1.1
pcre-devel-6.6-1.1
等再次執行./configure
[root@test nginx-0.6.26]# ./configure --with-http_stub_status_module
[root@test nginx-0.6.26]# make
[root@test nginx-0.6.26]# make install
4. 修改配置文件 /usr/local/nginx/conf/nginx.conf
user? nobody nobody;
worker_processes 10;
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid??????? logs/nginx.pid;
#最大文件描述符
worker_rlimit_nofile 51200;
events
{
????? use epoll;
????? worker_connections 51200;
}
http
{
????? include?????? mime.types;
????? default_type? application/octet-stream;
????? keepalive_timeout 120;
????? tcp_nodelay on;
????? upstream? www.s135.com? {
????????????? server?? 192.168.5.103:8080;
????????????? server?? 192.168.5.104:8080;
????? }
server
????? {
????????????? listen? 80;
????????????? server_name? 192.168.5.108;
????????????? location /HallServerInvoker/????? {
?????????????????????? proxy_pass??????? http://www.s135.com;
?????????????????????? proxy_set_header?? Host???????????? $host;
?????????????????????? proxy_set_header?? X-Real-IP??????? $remote_addr;
?????????????????????? proxy_set_header?? X-Forwarded-For? $proxy_add_x_forwarded_for;
????????????? }
????????????? log_format? www_s135_com? '$remote_addr - $remote_user [$time_local] $request '
??????????????????????????????? '"$status" $body_bytes_sent "$http_referer" '
??????????????????????????????? '"$http_user_agent" "$http_x_forwarded_for"';
????????????? access_log? /data1/logs/www.log? ;
?????? location? /NginxStatus? {
??????????????? stub_status??????????? on;
??????????????? access_log????????????? on;
??????????????? auth_basic????????????? "NginxStatus";
#?????????????? auth_basic_user_file? htpasswd;
??????????????? }
????? }
?}
5. 建立日志的路徑:# mkdir -p /data1/logs/
?????????????????? #? touch www.log
6. 測試配置文件
[root@test conf]# /usr/local/nginx/sbin/nginx -t
2008/05/04 15:40:02 [info] 2363#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is?
ok
2008/05/04 15:40:02 [info] 2363#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested?
successfully
7.啟動
[root@test conf]# /usr/local/nginx/sbin/nginx
[root@test conf]# ps fax
9400 ???????? Ss???? 0:00 nginx: master process /usr/local/nginx/sbin/nginx
?9401 ???????? S????? 0:00? \_ nginx: worker process??????
?9402 ???????? S????? 0:00? \_ nginx: worker process??
http://192.168.1.250:81/NginxStatus
Active connections: 1?
server accepts handled requests
2 2 11?
Reading: 0 Writing: 1 Waiting: 0???
二. 兩臺tomcat_client的配置
1.配置好tomcat,啟動tomcat,例如兩臺的tomcat端口都是8080
2.啟動 tomcat即可 三.測試
在IE里輸入:
http://192.168.5.108/HallServerInvoker/
會跳到兩臺的tomcat的客戶端上 備注:本次實驗針對自己tomcat的應用,具體應用要具體變通
使用nginx的感受,感覺nginx比較牛比的是:
如果后面的服務器其中一臺壞了,它能自動識別,更牛的是它好了之后nginx可以馬上識別
服務器A和B,如果A的響應時間為3,B的響應時間為1,那么nginx會自動調整訪問B的概率是A的3倍,真正做到負載均衡,感覺比LVS方便在于,不用在做象ldirectord監控LVS那樣監控nginx了
?
nginx負載均衡要想解決session的的問題的話可以在配置文件:
upstream www.s135.com {
下加上ip_hash;
例如:
upstream www.s135.com {
??????? ip_hash;
??????? server?? 192.168.5.103:8080;
??????? server?? 192.168.5.104:8080; 這樣就可以解決session的問題。
一. Nginx的安裝配置
1. Nginx服務器端
Nginx服務器的IP:192.168.5.108
Tomcat01的IP:?? 192.168.5.102
Tomcat02的IP:?? 192.168.5.103
2.#wget http://sysoev.ru/nginx/nginx-0.6.26.tar.gz
#tar zxvf nginx-0.6.26.tar.gz
#cd nginx-0.6.26
#[root@test nginx-0.6.26]# ./configure --with-http_stub_status_module
如果產生如下錯誤請執行第3步:
./configure: error: the HTTP rewrite module requires the PCRE library.
You can either disable the module by using --without-http_rewrite_module
option, or install the PCRE library into the system, or build the PCRE library
statically from the source with nginx by using --with-pcre=<path> option.
3.[root@test nginx-0.6.26]# rpm -qa|grep pcre
pcre-6.6-1.1
[root@test suantop]# rpm -ivh pcre-devel-6.6-1.1.i386.rpm?
warning: pcre-devel-6.6-1.1.i386.rpm: Header V3 DSA signature: NOKEY, key ID 37017186
Preparing...??????????????? ########################################### [100%]
?? 1:pcre-devel???????????? ########################################### [100%]
[root@test suantop]# rpm -qa|grep pcre
pcre-6.6-1.1
pcre-devel-6.6-1.1
等再次執行./configure
[root@test nginx-0.6.26]# ./configure --with-http_stub_status_module
[root@test nginx-0.6.26]# make
[root@test nginx-0.6.26]# make install
4. 修改配置文件 /usr/local/nginx/conf/nginx.conf
user? nobody nobody;
worker_processes 10;
#error_log? logs/error.log;
#error_log? logs/error.log? notice;
#error_log? logs/error.log? info;
#pid??????? logs/nginx.pid;
#最大文件描述符
worker_rlimit_nofile 51200;
events
{
????? use epoll;
????? worker_connections 51200;
}
http
{
????? include?????? mime.types;
????? default_type? application/octet-stream;
????? keepalive_timeout 120;
????? tcp_nodelay on;
????? upstream? www.s135.com? {
????????????? server?? 192.168.5.103:8080;
????????????? server?? 192.168.5.104:8080;
????? }
server
????? {
????????????? listen? 80;
????????????? server_name? 192.168.5.108;
????????????? location /HallServerInvoker/????? {
?????????????????????? proxy_pass??????? http://www.s135.com;
?????????????????????? proxy_set_header?? Host???????????? $host;
?????????????????????? proxy_set_header?? X-Real-IP??????? $remote_addr;
?????????????????????? proxy_set_header?? X-Forwarded-For? $proxy_add_x_forwarded_for;
????????????? }
????????????? log_format? www_s135_com? '$remote_addr - $remote_user [$time_local] $request '
??????????????????????????????? '"$status" $body_bytes_sent "$http_referer" '
??????????????????????????????? '"$http_user_agent" "$http_x_forwarded_for"';
????????????? access_log? /data1/logs/www.log? ;
?????? location? /NginxStatus? {
??????????????? stub_status??????????? on;
??????????????? access_log????????????? on;
??????????????? auth_basic????????????? "NginxStatus";
#?????????????? auth_basic_user_file? htpasswd;
??????????????? }
????? }
?}
5. 建立日志的路徑:# mkdir -p /data1/logs/
?????????????????? #? touch www.log
6. 測試配置文件
[root@test conf]# /usr/local/nginx/sbin/nginx -t
2008/05/04 15:40:02 [info] 2363#0: the configuration file /usr/local/nginx/conf/nginx.conf syntax is?
ok
2008/05/04 15:40:02 [info] 2363#0: the configuration file /usr/local/nginx/conf/nginx.conf was tested?
successfully
7.啟動
[root@test conf]# /usr/local/nginx/sbin/nginx
[root@test conf]# ps fax
9400 ???????? Ss???? 0:00 nginx: master process /usr/local/nginx/sbin/nginx
?9401 ???????? S????? 0:00? \_ nginx: worker process??????
?9402 ???????? S????? 0:00? \_ nginx: worker process??
http://192.168.1.250:81/NginxStatus
Active connections: 1?
server accepts handled requests
2 2 11?
Reading: 0 Writing: 1 Waiting: 0???
二. 兩臺tomcat_client的配置
1.配置好tomcat,啟動tomcat,例如兩臺的tomcat端口都是8080
2.啟動 tomcat即可 三.測試
在IE里輸入:
http://192.168.5.108/HallServerInvoker/
會跳到兩臺的tomcat的客戶端上 備注:本次實驗針對自己tomcat的應用,具體應用要具體變通
使用nginx的感受,感覺nginx比較牛比的是:
如果后面的服務器其中一臺壞了,它能自動識別,更牛的是它好了之后nginx可以馬上識別
服務器A和B,如果A的響應時間為3,B的響應時間為1,那么nginx會自動調整訪問B的概率是A的3倍,真正做到負載均衡,感覺比LVS方便在于,不用在做象ldirectord監控LVS那樣監控nginx了
?
nginx負載均衡要想解決session的的問題的話可以在配置文件:
upstream www.s135.com {
下加上ip_hash;
例如:
upstream www.s135.com {
??????? ip_hash;
??????? server?? 192.168.5.103:8080;
??????? server?? 192.168.5.104:8080; 這樣就可以解決session的問題。
轉載于:https://blog.51cto.com/ilexes/178034
總結
以上是生活随笔為你收集整理的nginx+tomcat的负载均衡的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MSMQ消息队列演示程序
- 下一篇: 库存管理系统的开发过程!