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

小编辑 发布于 2010/03/08 14:20
阅读 12K+
收藏 20
测试环境:
server1   服务器上安装了 nginx + tomcat01
server2 服务器上只安装了 tomcat02         
server1 IP 地址: 192.168.2.88
server2 IP 地址: 192.168.2.89
安装步骤:
1. server1 上安装配置 nginx + nginx_upstream_jvm_route
shell $> wget -c http://sysoev.ru/nginx/nginx-0.7.61.tar.gz
shell $> svn checkout http://nginx-upstream-jvm-route.googlecode.com/svn/trunk/ nginx-upstream-jvm-route-read-only
shell $> tar zxvf nginx-0.7.61
shell $> cd nginx-0.7.61
shell $> patch -p0 < ../nginx-upstream-jvm-route-read-only/jvm_route.patch
shell $> useradd www
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
shell $> make
shell $> make install
2.分别在两台机器上安装 tomcatjava (略)
设置tomcatserver.xml, 在两台服务器的tomcat的配置文件中分别找到:
<Engine name="Catalina" defaultHost="localhost" >
分别修改为:
Tomcat01:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="a">
Tomcat02:
<Engine name="Catalina" defaultHost="localhost" jvmRoute="b">
并在webapps下面建立aa文件夹,在里面建立要测试的index.jsp文件,内容如 下:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
%>
<html>
<head>
    </head>
      <body>
       88
       <!--server1 这里为 88 -->
        <br />
       <%out.print(request.getSession()) ;%>
       <!--输出session-->
        <br />
        <%out.println(request.getHeader("Cookie")); %>
      <!--输出Cookie-->
      </body>
</html>
两个tomcat一样只需要修改红色的部分
分别启动两个tomcat
3.设置nginx
shell $> cd /usr/local/nginx/conf
shell $> mv nginx.conf nginx.bak
shell $> vi nginx.conf
## 以下是配置 ###
user www www;
worker_processes 4;
error_log logs/nginx_error.log crit;
pid        /usr/local/nginx/nginx.pid;
#Specifies the value for maximum file descriptors that can be opened by this process.
worker_rlimit_nofile 51200;
events
{
use epoll;
worker_connections 2048;
}
http
{
upstream backend {
    server 192.168.2.88:8080 srun_id=a;
   server 192.168.2.89:8080 srun_id=b;
   jvm_route $cookie_JSESSIONID|sessionid reverse;
}
include       mime.types;
default_type application/octet-stream;
#charset gb2312;
charset UTF-8;
server_names_hash_bucket_size 128;
client_header_buffer_size 32k;
large_client_header_buffers 4 32k;
client_max_body_size 20m;
limit_rate 1024k;
sendfile on;
tcp_nopush     on;
keepalive_timeout 60;
tcp_nodelay on;
fastcgi_connect_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;
fastcgi_buffer_size 64k;
fastcgi_buffers 4 64k;
fastcgi_busy_buffers_size 128k;
fastcgi_temp_file_write_size 128k;
gzip on;
#gzip_min_length 1k;
gzip_buffers     4 16k;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_types       text/plain application/x-javascript text/css application/xml;
gzip_vary on;
#limit_zone crawler $binary_remote_addr 10m;
server
{
   listen       80;
   server_name 192.168.2.88;
   index index.html index.htm index.jsp;
   root /var/www;
   #location ~ .*\.jsp$
   location / aa/
{
     proxy_pass http://backend;
     proxy_redirect    off;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
     proxy_set_header X-Real-IP $remote_addr;
     proxy_set_header Host $http_host;
   }
   location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
   {
     expires      30d;
   }
   location ~ .*\.(js|css)?$
   {
     expires      1h;
   }
   location /Nginxstatus {
     stub_status on;
     access_log   off;
   }
log_format access '$remote_addr - $remote_user [$time_local] "$request" '
             '$status $body_bytes_sent "$http_referer" '
             '"$http_user_agent" $http_x_forwarded_for';
# access_log off;
}
}
4.测试
打开浏览器,输入:http://192.168.2.88/aa/
刷新了N次还都是88,也就是补丁起作用了,cookie 也获得了,为了测试,我又打开了“遨游浏览器”(因为session cookie问题所以从新 打开别的浏览器),输入网址:
http://192.168.2.88/aa/
显示89,刷新N次后还是89,大家测试的时候如果有疑问可一把 nginx 配置文件的
srun_id=a srun_id=b 去掉,然后在访问,就会知道页面是轮询访问得了!!
加载中
0
清凌渡
清凌渡
srun_id  提示非法参数?
0
Hartwell
Hartwell
nginx: [emerg] invalid parameter "srun_id=tomcat1" in /usr/local/nginx-1.

0.5/conf/nginx.conf:58

srun_id提示非法参数啊!

0
汤糖
汤糖

引用来自“small crab”的答案

nginx: [emerg] invalid parameter "srun_id=tomcat1" in /usr/local/nginx-1.

0.5/conf/nginx.conf:58

srun_id提示非法参数啊!

碰到同样的问题啊!!!!
仁江
仁江
这个最后怎么解决的啊。我也遇到这个问题。为什么人家的就可以。我的就怎么搞都有问题,烦死了
0
汤糖
汤糖
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c: In function ‘ngx_http_upstream_jvm_route_get_socket’:
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:191: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘reverse’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:192: 错误:‘ngx_http_upstream_server_t’ 没有名为 ‘srun_id’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:193: 错误:‘ngx_http_upstream_server_t’ 没有名为 ‘srun_id’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:198: 错误:‘ngx_http_upstream_server_t’ 没有名为 ‘srun_id’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:198: 错误:‘ngx_http_upstream_server_t’ 没有名为 ‘srun_id’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c: In function ‘ngx_http_upstream_jvm_route_get_session_value’:
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:223: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘lengths’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:223: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘values’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:237: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:238: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:241: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_cookie’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c: In function ‘ngx_http_upstream_jvm_route’:
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:428: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_cookie’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:429: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_cookie’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:434: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:435: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:446: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:447: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:472: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘reverse’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:476: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘values’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:477: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘lengths’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:485: 错误:‘NGX_HTTP_UPSTREAM_SRUN_ID’ 未声明 (在此函数内第一次使用)
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:485: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:485: 错误:所在的函数内只报告一次。)
make[1]: *** [objs/addon/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.o] 错误 1
make[1]: Leaving directory `/home/tools/nginx-1.0.8'
make: *** [build] 错误 2

jvm route编译出错啦,为什么呢?

justin
justin
1.要先在Nginx安装目录里执行 patch -p0 < /opt/nginx_upstream_jvm_route/jvm_route.patch (我这里是在opt下放着) 2.安装时:--add-module=/opt/nginx_upstream_jvm_route/ 参数,你再试一下。
0
Hartwell
Hartwell
集群不这么去整了,用cookie+memcached
0
张宋付
张宋付
可以采用 cookie+memcached。可以查看http://my.oschina.net/zhangsongfu/blog/5107
0
yaoweibin
yaoweibin

引用来自“汤糖”的答案

/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c: In function ‘ngx_http_upstream_jvm_route_get_socket’:
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:191: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘reverse’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:192: 错误:‘ngx_http_upstream_server_t’ 没有名为 ‘srun_id’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:193: 错误:‘ngx_http_upstream_server_t’ 没有名为 ‘srun_id’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:198: 错误:‘ngx_http_upstream_server_t’ 没有名为 ‘srun_id’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:198: 错误:‘ngx_http_upstream_server_t’ 没有名为 ‘srun_id’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c: In function ‘ngx_http_upstream_jvm_route_get_session_value’:
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:223: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘lengths’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:223: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘values’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:237: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:238: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:241: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_cookie’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c: In function ‘ngx_http_upstream_jvm_route’:
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:428: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_cookie’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:429: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_cookie’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:434: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:435: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:446: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:447: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘session_url’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:472: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘reverse’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:476: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘values’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:477: 错误:‘ngx_http_upstream_srv_conf_t’ 没有名为 ‘lengths’ 的成员
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:485: 错误:‘NGX_HTTP_UPSTREAM_SRUN_ID’ 未声明 (在此函数内第一次使用)
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:485: 错误:(即使在一个函数内多次出现,每个未声明的标识符在其
/home/tools/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.c:485: 错误:所在的函数内只报告一次。)
make[1]: *** [objs/addon/nginx_upstream_jvm_route/ngx_http_upstream_jvm_route_module.o] 错误 1
make[1]: Leaving directory `/home/tools/nginx-1.0.8'
make: *** [build] 错误 2

jvm route编译出错啦,为什么呢?

你看下模块的README,这个模块需要打patch的。
返回顶部
顶部