nginx + tomcat + redis 部署项目,解决session共享问题。

 最近自己搭了一套nginx的环境,集群部署了公司的一个项目,中间解决了session共享的问题。记录如下,以备日后查看。

1.环境  

 windows10 家庭中文版,jdk 7, tomcat 7.0.27 ,  nginx-1.10.1 windows,  Redis-x64-2.8.2402 windows.

tomcat 我拷贝了一份,并修改server.xml:

<Server port="8006" shutdown="SHUTDOWN">
<!-- Security listener. Documentation at /docs/config/listeners.html <Listener className="org.apache.catalina.security.SecurityListener" /> --> <!--APR library loader. Documentation at /docs/apr.html --> <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /> <!--Initialize Jasper prior to webapps are loaded. Documentation at /docs/jasper-howto.html --> <Listener className="org.apache.catalina.core.JasperListener" /> <!-- Prevent memory leaks due to use of particular java/javax APIs--> <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" /> <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" /> <Listener className="org.apache.catalina.core.ThreadLocalLeakPreventionListener" /> <!-- Global JNDI resources Documentation at /docs/jndi-resources-howto.html --> <GlobalNamingResources> <!-- Editable user database that can also be used by UserDatabaseRealm to authenticate users --> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> </GlobalNamingResources> <!-- A "Service" is a collection of one or more "Connectors" that share a single "Container" Note: A "Service" is not itself a "Container", so you may not define subcomponents such as "Valves" at this level. Documentation at /docs/config/service.html --> <Service name="Catalina"> <!--The connectors can use a shared executor, you can define one or more named thread pools--> <!-- <Executor name="tomcatThreadPool" namePrefix="catalina-exec-" maxThreads="150" minSpareThreads="4"/> --> <!-- A "Connector" represents an endpoint by which requests are received and responses are returned. Documentation at : Java HTTP Connector: /docs/config/http.html (blocking & non-blocking) Java AJP Connector: /docs/config/ajp.html APR (HTTP/AJP) Connector: /docs/apr.html Define a non-SSL HTTP/1.1 Connector on port 8080 --> <Connector port="8081" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> <!-- A "Connector" using the shared thread pool--> <!-- <Connector executor="tomcatThreadPool" port="8080" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" /> --> <!-- Define a SSL HTTP/1.1 Connector on port 8443 This connector uses the JSSE configuration, when using APR, the connector should be using the OpenSSL style configuration described in the APR documentation --> <!-- <Connector port="8443" protocol="HTTP/1.1" SSLEnabled="true" maxThreads="150" scheme="https" secure="true" clientAuth="false" sslProtocol="TLS" /> --> <!-- Define an AJP 1.3 Connector on port 8009 --> <Connector port="8010" protocol="AJP/1.3" redirectPort="8443" />

这样,两个tmcat启起来就时两台服务器。

注意事项:windows用户必须用Administrator, 否则redis不能正常使用

2. redis依赖包

commons-pool2-2.2,jedis-2.5.2,tomcat-redis-session-manager-2.0.0 ,

注意事项:

如果使用jdk 6,启动时会提示 “java.lang.UnsupportedClassVersionError: com/orangefunction/tomcat/redissessions/RedisSessionHandlerValve : Unsupported major.minor version 51.0” , jdk7的版本号时51,所以应该是最小支持jdk7,其他版本的tomcat-redis-session-manager可能支持jdk的其他版本,大家自行实验。

3. nginx 主要配置

	#设定负载均衡的服务器列表
    upstream mysvr {
		server localhost:8080;
		server localhost:8081;
		#ip_hash;
	}
	
    server {
        listen       80; #监听端口 
        server_name  cq.demo.com; #域名可以有多个,用空格隔开  
        charset utf-8;
		
        #access_log  logs/host.access.log  main;

        location / {
			proxy_pass  http://mysvr;  #请求转向mysvr 定义的服务器列表
			proxy_set_header Host $host;
			proxy_set_header X-Real-IP $remote_addr;
			proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
			proxy_connect_timeout 10;
			proxy_read_timeout 10;
			proxy_send_timeout 10;
        }

        # redirect server error pages to the static page /50x.html
        #
        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }
		
		#静态文件,nginx自己处理
		location ~ .*.(html|gif|jpg|jpeg|bmp|png|ico|txt|js|css|woff2|woff|ttf)$
		{	root html/static;
			#过期30天,静态文件不怎么更新,过期可以设大一点,如果频繁更新,则可以设置得小一点。
			expires 30d;
			#proxy_pass  http://mysvr; 
		}
    }

4. tomcat修改context.xml配置

Context标签加入:

<Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
	<Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
			 host="localhost" 
			 port="6379" 
			 database="0" 
			 maxInactiveInterval="60" />

  

先写到这,比较乱,没有按照操作顺序,只是记录了操作中的重点,其实操作顺序是次要的,解决操作中遇到的问题才是最重要的。

原文地址:https://www.cnblogs.com/tibit/p/5807571.html