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

  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. 分别修改为:
  20. Tomcat01:
  21. Tomcat02:
  22. 并在webapps下面建立aa文件夹,在里面建立要测试的index.jsp文件,内容如下:
  23. 88


  24. 两个tomcat一样只需要修改红色的部分
  25. 分别启动两个tomcat
  26. 3.设置nginx
  27. shell $> cd /usr/local/nginx/conf
  28. shell $> mv nginx.conf nginx.bak
  29. shell $> vi nginx.conf
  30. ## 以下是配置 ###
  31. user www www;
  32. worker_processes4;
  33. error_log logs/nginx_error.log crit;
  34. pid /usr/local/nginx/nginx.pid;
  35. #Specifies the valuefor maximum file descriptors that can be opened bythis process.
  36. worker_rlimit_nofile51200;
  37. events
  38. {
  39. use epoll;
  40. worker_connections2048;
  41. }
  42. http
  43. {
  44. upstream backend {
  45. server 192.168.2.88:8080 srun_id=a;
  46. server 192.168.2.89:8080 srun_id=b;
  47. jvm_route $cookie_JSESSIONID|sessionid reverse;
  48. }
  49. include mime.types;
  50. default_type application/octet-stream;
  51. #charset gb2312;
  52. charset UTF-8;
  53. server_names_hash_bucket_size128;
  54. client_header_buffer_size 32k;
  55. large_client_header_buffers4 32k;
  56. client_max_body_size 20m;
  57. limit_rate 1024k;
  58. sendfile on;
  59. tcp_nopush on;
  60. keepalive_timeout60;
  61. tcp_nodelay on;
  62. fastcgi_connect_timeout300;
  63. fastcgi_send_timeout300;
  64. fastcgi_read_timeout300;
  65. fastcgi_buffer_size 64k;
  66. fastcgi_buffers4 64k;
  67. fastcgi_busy_buffers_size 128k;
  68. fastcgi_temp_file_write_size 128k;
  69. gzip on;
  70. #gzip_min_length 1k;
  71. gzip_buffers 4 16k;
  72. gzip_http_version1.0;
  73. gzip_comp_level2;
  74. gzip_types text/plain application/x-javascript text/css application/xml;
  75. gzip_vary on;
  76. #limit_zone crawler $binary_remote_addr 10m;
  77. server
  78. {
  79. listen 80;
  80. server_name 192.168.2.88;
  81. index index.html index.htm index.jsp;
  82. root /var/www;
  83. #location ~ .*\.jsp$
  84. location / aa/
  85. {
  86. proxy_pass http://backend;
  87. proxy_redirect off;
  88. proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  89. proxy_set_header X-Real-IP $remote_addr;
  90. proxy_set_header Host $http_host;
  91. }
  92. location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
  93. {
  94. expires 30d;
  95. }
  96. location ~ .*\.(js|css)?$
  97. {
  98. expires 1h;
  99. }
  100. location /Nginxstatus {
  101. stub_status on;
  102. access_log off;
  103. }
  104. log_format access'$remote_addr - $remote_user [$time_local] "$request" '
  105. '$status $body_bytes_sent "$http_referer" '
  106. '"$http_user_agent" $http_x_forwarded_for';
  107. # access_log off;
  108. }
  109. }
  110. 4.测试
  111. 打开浏览器,输入:http://192.168.2.88/aa/
  112. 刷新了N次还都是88,也就是补丁起作用了,cookie 值也获得了,为了测试,我又打开了“遨游浏览器”(因为session 和 cookie问题所以从新打开别的浏览器),输入网址:
  113. http://192.168.2.88/aa/
  114. 显示89,刷新N次后还是89,大家测试的时候如果有疑问可一把 nginx 配置文件的
  115. srun_id=a srun_id=b 去掉,然后在访问,就会知道页面是轮询访问得了!!

    推荐阅读