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= 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的问题。