Nginx+resin——session问题
 
环境:server1 : 192.168.2.33 nginx+resin 192.168.2.33:8081/web1
      Server2 : 192.168.2.34 resin       192.168.2.34:8081/web1
 
1、192.168.2.34上resin配置:
 Jdk和resin安装配置过程略!
 
[root@xxoo34 ~]# vi /usr/local/resin/conf/resin.xml
末尾自定义的站点段:
 
<cluster id="a">
    <server-default>
      <socket-timeout>40s</socket-timeout>
      <keepalive-max>3000</keepalive-max>
      <keepalive-timeout>40s</keepalive-timeout>
      <load-balance-idle-time>30s</load-balance-idle-time>
      <thread-max>512</thread-max> (线程池)
      <jvm-arg>-Xms64m</jvm-arg> 
      <jvm-arg>-Xmx64m</jvm-arg>
      <jvm-arg>-Xss32m</jvm-arg>
      <jvm-arg>-server</jvm-arg>
   </server-default>
        <resin:import path="${resin.home}/conf/app-default.xml"/>
    <server id="a" address="127.0.0.1" port="6801"> (这里的a 将是nginx配置文件里srun_id= 所对应的 ID别名)
        <http id="" port="8081"/>
    </server>
    <host id="" root-directory=".">
           <web-app id="/" root-directory="/usr/local/web/web1" />
           <character-encoding>UTF-8</character-encoding>
 </host>
 </cluster>
 
2、192.168.2.33上resin配置:
 [root@xxoo33~]# vi /usr/local/resin/conf/resin.xml
末尾自定义的站点段:
<cluster id="b">
   <server-default>
      <socket-timeout>40s</socket-timeout>
      <keepalive-max>3000</keepalive-max>
      <keepalive-timeout>40s</keepalive-timeout>
      <load-balance-idle-time>30s</load-balance-idle-time>
      <thread-max>512</thread-max>
      <jvm-arg>-Xms64m</jvm-arg>
      <jvm-arg>-Xmx64m</jvm-arg>
      <jvm-arg>-Xss32m</jvm-arg>
      <jvm-arg>-server</jvm-arg>
   </server-default>
        <resin:import path="${resin.home}/conf/app-default.xml"/>
    <server id="b" address="127.0.0.1" port="6801">
        <http id="" port="8081"/>
    </server>
    <host id="" root-directory=".">
           <web-app id="/" root-directory="/usr/local/web/web1" />
           <character-encoding>UTF-8</character-encoding>
 </host>
 </cluster>
 
3、nginx 上如果原来安装好了则从新 ./configure 和 make 如果没安装过则全新安装!
[root@xxoo33 home]# tar zxvf nginx-1.0.13.tar.gz -C /usr/src/
[root@xxoo33 home]# tar zxvf nginx-upstream-jvm-route-0.1.tar.gz -C /usr/src/
[root@xxoo33 home]# tar zcvf ngx_cache_purge-1.6.tar.gz -C /usr/src/
[root@xxoo33 home]# cd /usr/src/nginx-1.0.13/
[root@xxoo33 nginx-1.0.13]#  patch -p0 < /usr/src/nginx_upstream_jvm_route/jvm_route.patch
(打nginx-upstream-jvm补丁 解决session)
 
[root@xxoo33~]# ./configure --prefix=/usr/local/nginx (--add-module=/usr/src/ngx_cache_purge-1.6 –单独清缓存模块) --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/src/pcre-8.30/ --add-module=/usr/src/nginx_upstream_jvm_route
Make
Make install
 [root@xxoo33 nginx-1.0.13]# vi /usr/local/nginx/conf/nginx.conf
user nobody;
worker_processes 8;
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 20480;
events
{
 use epoll;
 worker_connections 20480;
}
 
http {
#     ip_hash;
    upstream web1 {
       server 192.168.2.33:8081 srun_id=b;     (srun_id对应resin server id 别名)
       server 192.168.2.34:8081 srun_id=a;
       jvm_route $cookie_JSESSIONID|sessionid; 
 (jvm_route 解决session) tomcat的是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_header_timeout 20;
 client_max_body_size 20m;
 sendfile on;
 tcp_nopush     on;
 keepalive_timeout 60;
 tcp_nodelay on;
 proxy_connect_timeout 30;
 proxy_read_timeout 60;
 proxy_send_timeout 20;
 proxy_buffer_size 96k;
 proxy_buffers 8 256k;
 proxy_busy_buffers_size 512k;
 proxy_temp_file_write_size 512k;
# proxy_store on;
# proxy_store_access user:rw group:rw all:rw;
 proxy_temp_path /usr/local/nginx/proxy_temp;  
(单独清缓存 无需则注释#注:proxy_temp_path和proxy_cache_path指定的路径必须在同一分区)
#设置Web缓存区名称为cache_one,内存缓存空间大小为20MB,1天没有被访问的内容自动清除,硬盘缓存空间大小为2GB。
 proxy_cache_path /usr/local/nginx/www levels=1:2 keys_zone=cache_one:20m inactive=1d max_size=2g;
 gzip on;
 gzip_proxied any;
 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 one $binary_remote_addr 10m;
 proxy_set_header Host $host;
 proxy_set_header X-Real-IP $remote_addr;
 proxy_set_header X-Forwarded-For $remote_addr;
 
 
 server {
 
        listen       80;
        server_name 192.168.2.40; (这里用虚拟IP代替)
        index index.html index.jsp;
 
   location /
    {
         #如果后端的服务器返回502、504、执行超时等错误,自动将请求转发到upstream负载均衡池中的另一台服务器,实现故障转移。
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         proxy_cache cache_one;
         #对不同的HTTP状态码设置不同的缓存时间
         proxy_cache_valid 200 304 12h;
         #以域名、URI、参数组合成Web缓存的Key值,Nginx根据Key值哈希,存储缓存内容到二级缓存目录内
         proxy_cache_key $host$uri$is_args$args;
         proxy_set_header Host $host;
         proxy_set_header X-Forwarded-For $remote_addr;
         proxy_pass http://backend_server;
         expires      1d;
    }
 
 location /
    {
     proxy_cache cache_one;
     proxy_cache_valid 200 304 2h;
     proxy_cache_valid any 30m;
     proxy_cache_key $host$uri$is_args$args;
     proxy_pass http://web1;
 }
 
 
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
    {
    proxy_cache cache_one;
     proxy_cache_valid 200 304 5h;
     proxy_cache_valid any 3m;
     proxy_cache_key $host$uri$is_args$args;
     expires      8h;
     proxy_pass http://web1;
   }
 
 
 location ~ .*\.(js|css|html)$
   {
     proxy_cache cache_one;
     proxy_cache_valid 200 304 3h;
     proxy_cache_valid any 3m;
     proxy_cache_key $host$uri$is_args$args;
     expires      5h;
     proxy_pass http://web1;
   }
#用于清除缓存,假设一个URL为http://192.168.2.33/aa/xo.jsp,通过访问http://192.168.2.33/purge/aa/xo.jsp就可以清除该URL的缓存。
 location ~ /purge(/.*)
    {
     allow         all; (允许所有操作)
     deny          59.42.95.33/32; (拒绝这个地址操作)
     proxy_cache_purge    cache_one   $host$1$is_args$args;
    }
 }
}
 
4、分别在 两台server 的web1 内建立测试页
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
%>
<html>
<head>
</head>
<body>
192.168.2.33
<!--server2 这里为 192.168.2.34->
<br />
<%out.print(request.getSession()) ;%>
<!--输出session-->
<br />
<%out.println(request.getHeader("Cookie")); %>
<!--输出Cookie-->
</body>
</html>       (方面测试 session 问题)
 
或:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title> xxoo</title>
</head>
 
<body>
192.168.2.33 FACK you!
<br />
<img src="xo.jpg" />
 
</body>              (方便测试缓存测试)
 
5、session测试结果:
192.168.2.33 8081 web1!! 192.168.2.33
SessionImpl[aaacAAl8SMpbS9uCcbGIt,]
JSESSIONID=aaacAAl8SMpbS9uCcbGIt
如果无 jvm_route 模块功能 大家测试的时候如果有疑问可一把 nginx 配置文件的
srun_id=a srun_id=b 去掉,然后在访问,就会知道 页面是轮询访问得了!!
5、缓存清楚测试结果:
Successful purge

Key : 192.168.2.40/bb/ox.jsp
Path: /usr/local/nginx/www/3/5e/d1bee53e3e153261fdf88eb12c3ad5e3

nginx/1.0.13