1. 程式人生 > >nginx+tomcat會話保持

nginx+tomcat會話保持

nginx+tomcat會話保持

在nginx.conf中http標簽內定義upstream

upstream name {                #name自定義一個名字
    server     ip1:port weight=5;
    server     ip2:port weight=5
}

在server標簽內的location/中

loation /{
proxy_pass http name;        #使用upstream定義的name
proxy_set_header X-Real_IP $remote_addr;
client_max_body_size 100m;
}

對特殊目錄限制權限

location ~ ^/(WEB-INF)/ {
    denny all;    
}

tomcat設置

使用memcached-session-manager這個開源項目(http://code.google.com/p/memcached-session-manager,下面簡稱msm)配置Tomcat和memcached實現session共享。

首先將下面的包下載到Tomcat的lib目錄下,這些包都是msm所依賴的包。

[[email protected] ~]# cd /usr/local/tomcat/lib/  
[[email protected] lib]# wget http://memcached-session-manager.googlecode.com/files/memcached-session-manager-1.3.0.jar
[[email protected] lib]# wget http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-jodatime-1.3.0.jar
[[email protected] lib]# wget http://memcached-session-manager.googlecode.com/files/msm-javolution-serializer-cglib-1.3.0.jar
[[email protected] lib]# wget http://spymemcached.googlecode.com/files/memcached-2.4.2.jar
[[email protected] lib]# wget http://memcached-session-manager.googlecode.com/files/javolution-5.4.3.1.jar

修改server.xml

	 <Context docBase="/var/www/html" path="" reloadable="true">  
	 <Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"   
		 memcachedNodes="n1:localhost:11211"   
	     requestUriIgnorePattern=".*\.(png|gif|jpg|css|js)$"   
	     sessionBackupAsync="false"   
	     sessionBackupTimeout="100"   
	     transcoderFactoryClass="de.javakaffee.web.msm.serializer.javolution.JavolutionTranscoderFactory"   
	     copyCollectionsForSerialization="false" />  
	 </Context>

這裏的memcachedNodes是填寫memcached節點,多個節點時可以以空隔分開,如:

n1:localhost:11211 n2:localhost:11212 /localhost改為安裝memcached的服務器的IP

sessionBackupTimeout的單位為分鐘

/var/www/html改為Tomcat服務器web根目錄的路徑

修改後重啟TOMCAT和nginx服務





本文出自 “庭前夜末空看雪” 博客,請務必保留此出處http://12550795.blog.51cto.com/12540795/1962761

nginx+tomcat會話保持