Nginx+tomcat负载均衡session问题解决

文章来源: http://hi.baidu.com/softsystem/blog/item/c7f5d42c3f0af13d349bf7bd.html 

Java代码   收藏代码
  1. 测试环境:  
  2. server1   服务器上安装了 nginx + tomcat01  
  3. server2 服务器上只安装了 tomcat02            
  4. server1 IP 地址: 192.168.2.88  
  5. server2 IP 地址: 192.168.2.89  
  6. 安装步骤:  
  7. 1. 在server1 上安装配置 nginx + nginx_upstream_jvm_route  
  8. shell $> wget -c http://sysoev.ru/nginx/nginx-0.7.61.tar.gz  
  9. shell $> svn checkout http://nginx-upstream-jvm-route.googlecode.com/svn/trunk/ nginx-upstream-jvm-route-read-only   
  10. shell $> tar zxvf nginx-0.7.61  
  11. shell $> cd nginx-0.7.61  
  12. shell $> patch -p0 < ../nginx-upstream-jvm-route-read-only/jvm_route.patch  
  13. shell $> useradd www  
  14. shell $> ./configure --user=www --group=www --prefix=/usr/local//nginx --with-http_stub_status_module --with-http_ssl_module --add-module=/root/nginx-upstream-jvm-route-read-only  
  15. shell $> make  
  16. shell $> make install  
  17. 2.分别在两台机器上安装 tomcat和java (略)  
  18. 设置tomcat的server.xml, 在两台服务器的tomcat的配置文件中分别找到:  
  19. <Engine name="Catalina" defaultHost="localhost" >  
  20. 分别修改为:  
  21. Tomcat01:  
  22. <Engine name="Catalina" defaultHost="localhost" jvmRoute="a">  
  23. Tomcat02:  
  24. <Engine name="Catalina" defaultHost="localhost" jvmRoute="b">  
  25. 并在webapps下面建立aa文件夹,在里面建立要测试的index.jsp文件,内容如下:  
  26. <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>  
  27. <%  
  28. %>  
  29. <html>  
  30. <head>  
  31.     </head>  
  32.       <body>  
  33.        88   
  34.        <!--server1 这里为 88 -->  
  35.         <br />  
  36.        <%out.print(request.getSession()) ;%>  
  37.        <!--输出session-->  
  38.         <br />  
  39.         <%out.println(request.getHeader("Cookie")); %>  
  40.       <!--输出Cookie-->   
  41.       </body>  
  42. </html>  
  43. 两个tomcat一样只需要修改红色的部分  
  44. 分别启动两个tomcat  
  45. 3.设置nginx  
  46. shell $> cd /usr/local/nginx/conf  
  47. shell $> mv nginx.conf nginx.bak  
  48. shell $> vi nginx.conf  
  49. ## 以下是配置 ###  
  50. user www www;  
  51. worker_processes 4;  
  52. error_log logs/nginx_error.log crit;  
  53. pid        /usr/local/nginx/nginx.pid;  
  54. #Specifies the value for maximum file descriptors that can be opened by this process.  
  55. worker_rlimit_nofile 51200;  
  56. events  
  57. {  
  58. use epoll;  
  59. worker_connections 2048;  
  60. }  
  61. http  
  62. {  
  63. upstream backend {  
  64.     server 192.168.2.88:8080 srun_id=a;   
  65.    server 192.168.2.89:8080 srun_id=b;  
  66.    jvm_route $cookie_JSESSIONID|sessionid reverse;  
  67. }  
  68. include       mime.types;  
  69. default_type application/octet-stream;  
  70. #charset gb2312;  
  71. charset UTF-8;  
  72. server_names_hash_bucket_size 128;  
  73. client_header_buffer_size 32k;  
  74. large_client_header_buffers 4 32k;  
  75. client_max_body_size 20m;  
  76. limit_rate 1024k;  
  77. sendfile on;  
  78. tcp_nopush     on;  
  79. keepalive_timeout 60;  
  80. tcp_nodelay on;  
  81. fastcgi_connect_timeout 300;  
  82. fastcgi_send_timeout 300;  
  83. fastcgi_read_timeout 300;  
  84. fastcgi_buffer_size 64k;  
  85. fastcgi_buffers 4 64k;  
  86. fastcgi_busy_buffers_size 128k;  
  87. fastcgi_temp_file_write_size 128k;  
  88. gzip on;  
  89. #gzip_min_length 1k;  
  90. gzip_buffers     4 16k;  
  91. gzip_http_version 1.0;  
  92. gzip_comp_level 2;  
  93. gzip_types       text/plain application/x-javascript text/css application/xml;  
  94. gzip_vary on;  
  95. #limit_zone crawler $binary_remote_addr 10m;  
  96. server  
  97. {  
  98.    listen       80;  
  99.    server_name 192.168.2.88;  
  100.    index index.html index.htm index.jsp;  
  101.    root /var/www;  
  102.    #location ~ .*\.jsp$  
  103.    location / aa/  
  104. {  
  105.      proxy_pass http://backend;  
  106.      proxy_redirect    off;  
  107.      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;  
  108.      proxy_set_header X-Real-IP $remote_addr;  
  109.      proxy_set_header Host $http_host;  
  110.    }  
  111.    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$  
  112.    {  
  113.      expires      30d;  
  114.    }  
  115.    location ~ .*\.(js|css)?$  
  116.    {  
  117.      expires      1h;  
  118.    }  
  119.    location /Nginxstatus {  
  120.      stub_status on;  
  121.      access_log   off;  
  122.    }  
  123. log_format access '$remote_addr - $remote_user [$time_local] "$request" '  
  124.              '$status $body_bytes_sent "$http_referer" '  
  125.              '"$http_user_agent" $http_x_forwarded_for';  
  126. # access_log off;  
  127. }  
  128. }  
  129. 4.测试  
  130. 打开浏览器,输入:http://192.168.2.88/aa/  
  131. 刷新了N次还都是88,也就是补丁起作用了,cookie 值也获得了,为了测试,我又打开了“遨游浏览器”(因为session 和 cookie问题所以从新打开别的浏览器),输入网址:  
  132. http://192.168.2.88/aa/  
  133. 显示89,刷新N次后还是89,大家测试的时候如果有疑问可一把 nginx 配置文件的  
  134. srun_id=a srun_id=b 去掉,然后在访问,就会知道页面是轮询访问得了!!  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值