nginx+resin session同步

nginx_upstream_jvm_route解决 session 不同步问题,不过他不是共享,也不是同步,而是通过 cookie_session 来判别!

测试环境:

server1   服务器上安装了 nginx + resin

server2 服务器上只安装了resin          

server1 IP 地址: 192.168.6.121

server2 IP 地址: 192.168.6.162

安装步骤:

1. server1 上安装配置 nginx +nginx_upstream_jvm_route

shell $> wget -chttp://sysoev.ru/nginx/nginx-0.7.61.tar.gz

shell $>svn checkouthttp://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/webserver/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.分别在两台机器上安装 resin

### 修改环境变量###

shell $> vim /etc/profile

### umask 022 下填加以下###

JAVA_HOME=/usr/lib/jvm/java-6-sun
export JAVA_HOME

JRE_HOME="${JAVA_HOME}"/jre
export JRE_HOME

CLASSPATH=.:"${JAVA_HOME}"/lib/tools.jar:"${JAVA_HOME}"/lib/dt.jar${RESIN_HOME}"/lib/resin.jar:"${CLASSPATH}
export CLASSPATH

RESIN_HOME=/usr/local/resin
export RESIN_HOME
PATH="${JAVA_HOME}"/bin:"${PATH}"
export PATH

shell $> wget -chttp://www.caucho.com/download/resin-3.1.9.tar.gz

shell $> tar zxvf resin-3.1.9.tar.gz

shell $> cd resin-3.1.9

shell $> ./configure --prefix=/usr/local/resin

shell $> make

shell $> make install

3. 配置两台机器 resin

shell $> cd /usr/local/resin

shell $> cd conf

shell $> vim resin.conf

## 查找     <httpaddress="*" port="8080"/>

## 注释掉 <!--http address="*"port="8080"/-->

## 查找     <server id="" address="127.0.0.1"port="6800">

## 替换成

  <server id="a"address="192.168.6.121" port="6800">

<!-- server2address=192.168.6.162 -->
   <http id="" port="8080"/>
   </server>

   <serverid="b" address="192.168.6.121" port="6801">

<!-- server2address=192.168.6.162 -->
   <http id="" port="8081"/>
   </server>

shell $> cd/usr/local/resin/webapps/ROOT/

shell $> mv index.jsp   index.jsp.bak

shell $> vim index.jsp

## 填入以下内容

<%@ page language="java"import="java.util.*" pageEncoding="UTF-8"%>
<%
%>

<html>
<head>
   </head>
     <body>
       121

      <!--server2 这里为 162 -->
       <br />
      <%out.print(request.getSession());%>

      <!--输出session-->
       <br />
      <%out.println(request.getHeader("Cookie")); %>
     <!--
输出Cookie-->

     </body>
</html>

###重启 resin 服务#####

### server1 服务器#####

shell $> /usr/local/resin/bin/httpd.sh -server a start

###注意如果没修改环境变量会报错

### server2 服务器 ####

shell $> /usr/local/resin/bin/httpd.sh -server b start

### 注意的是 server2 服务器只启动 server_id b ###

4.整合 ngxin resin

shell $> cd /usr/local/nginx/conf

shell $> mv nginx.conf nginx.bak

shell $> vim 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 thatcan be opened by this process.
worker_rlimit_nofile 51200;

events
{
use epoll;
worker_connections 2048;
}

http
{
upstream backend {
   server 192.168.6.121:8080 srun_id=a;

   ####   这里 srun_id=a    对应的是 server1 resin 配置里的 server id="a"
   server 192.168.6.162:8081 srun_id=b;

   #### 这里 srun_id=b   对应的是 server2 resin 配置里的 server id="b"

   jvm_route$cookie_JSESSIONID|sessionid;
}

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/plainapplication/x-javascript text/css application/xml;
gzip_vary on;

#limit_zone crawler $binary_remote_addr 10m;

server
{
  listen       80;
  server_name 192.168.6.121;
  index index.html index.htm index.jsp;
  root /var/www;

  location ~ .*\.jsp$
  {
    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 /stu {
    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;
}


}

5.测试,打开浏览器,输入http://192.168.6.121/index.jsp



你可能感兴趣的:(nginx,resin,session同步)