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. "Catalina" defaultHost="localhost" >
  20. 分别修改为:
  21. Tomcat01:
  22. "Catalina" defaultHost="localhost" jvmRoute="a">
  23. Tomcat02:
  24. "Catalina" defaultHost="localhost" jvmRoute="b">
  25. 并在webapps下面建立aa文件夹,在里面建立要测试的index.jsp文件,内容如下:
  26. <%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
  27. <%
  28. %>
  29. 88

  30. <%out.print(request.getSession()) ;%>

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

你可能感兴趣的:(Nginx,负载均衡)