nginx 多tomcat 动静分离 分布式前后台共享session

1、安装nginx:http://nginx.org/en/download.html

2、安装多个tomcat:

  为了多个tomcat启动,修改其中一个tomcat的占用端口,apache-tomcat-7.0.67\conf\server.xml,修改一下四处

<Server port="8095" shutdown="SHUTDOWN">


<Connector port="8090" protocol="HTTP/1.1"
               connectionTimeout="20000"
               redirectPort="8443" useBodyEncodingForURI="true" URIEncoding="UTF-8"/>


<Connector port="8099" protocol="AJP/1.3" redirectPort="8443" />


 <Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm1">

3、\nginx-1.11.3\conf  增加proxy.conf文件  供设置代理使用

proxy_redirect          off;
proxy_set_header        Host $host;
proxy_set_header        X-Real-IP $remote_addr; #获取真实IP
#proxy_set_header       X-Forwarded-For   $proxy_add_x_forwarded_for; #获取代理者的真实ip
client_max_body_size    10m;
client_body_buffer_size 128k;
proxy_connect_timeout   90;
proxy_send_timeout      90;
proxy_read_timeout      90;
proxy_buffer_size       4k;
proxy_buffers           4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;

4、\nginx-1.11.3\conf\nginx.conf   配置如下

系统分为三个地方:

1)静态文件static使用 root D:\adforum; 

2)proxy_pass http://127.0.0.1:8080;

3)proxy_pass http://127.0.0.1:8090;

http {
    include     mime.types;
    include     proxy.conf;  #要指向代理文件
    default_type  application/octet-stream;

    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;
    
    upstream localhost {  
      #根据ip计算将请求分配各那个后端tomcat,许多人误认为可以解决session问题,其实并不能。  
      #同一机器在多网情况下,路由切换,ip可能不同  
      #ip_hash;   
      server localhost:8080;  
      server localhost:8090;  
     }  

    server {
        listen       80;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        #nginx默认页面
        #location / {
        #   root html;
        #   index index.html index.htm;
        #}
        location /status {
            stub_status on;
            access_log off;
        }

      location / {
        proxy_pass http://localhost;

      }

        #动静分离
        location ^~ /static/ { 
            root D:\adforum; 
            expires  3d; 
        } 
        
        #expires定义用户浏览器缓存的时间为3天,如果静态页面不常更新,可以设置更长,这样可以节省带宽和缓解服务器的压力 
        #location ~ .*\.(html|htm|gif|jpg|jpeg|bmp|png|ico|txt|js|css)$ { 
        #    root D:\adforum; 
        #    expires  3d; 
        #} 


...........

5、前后台共享session  具体见http://blog.csdn.net/xluren/article/details/16951247

方式:  1)不使用session,换用cookie  

     2)session存在数据库(MySQL等)中

     3)session存在memcache或者redis中 

      .......

6、使用redis共享session()

1)windows  最新releases:https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.msi

              https://github.com/MSOpenTech/redis/releases/download/win-3.2.100/Redis-x64-3.2.100.zip  

   linux版本:http://download.redis.io/releases/redis-3.2.3.tar.gz

 

2)cd redis目录  启动

redis-server.exe  redis.windows.conf

启动成功,6379端口。[2352] 27 Aug 14:25:01.164 * The server is now ready to accept connections on port 6379  

 

3)下载tomcat-redis-session-manager相应的jar包,并拷贝到$TOMCAT_HOME/lib中。主要有三个:

https://github.com/downloads/jcoleman/tomcat-redis-session-manager/tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar
http://central.maven.org/maven2/redis/clients/jedis/2.5.2/jedis-2.5.2.jar
http://central.maven.org/maven2/org/apache/commons/commons-pool2/2.0/commons-pool2-2.0.jar

 

org.xml.sax.SAXParseException; systemId: file:/D:/SOFT/SERVER/apache-tomcat-7.0.67-8090/conf/context.xml; lineNumber: 34; columnNumber: 88; Error at (34, 88) : com.oran
gefunction.tomcat.redissessions.RedisSessionHandlerValve

 

错误的解决方法:

https://github.com/jcoleman/tomcat-redis-session-manager下载源码编译。

依赖两个包:依赖了tomcat其他的包:tomcat-juli-adapters.jar和tomcat-juli.jar两个包   可以从http://archive.apache.org/dist/tomcat/tomcat-7/v7.0.67/bin/extras/下载,并拷贝到$TOMCAT_HOME/lib中。

4)修改两tomcat的context.xml:

<Context>  
  
    <!-- Default set of monitored resources -->  
    <WatchedResource>WEB-INF/web.xml</WatchedResource>  
  <Valve className="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve" />
    <Manager className="com.orangefunction.tomcat.redissessions.RedisSessionManager"
         host="localhost" 
         port="6379" 
         database="0" 
         maxInactiveInterval="60"/> 
</Context>  

5)在集群部署中我的每个服务都有不同的sessionCookieName(web = WJSESSIONID、wap = MJSESSIONID、cms = CMSJSESSIONID)

不知道怎么配置的,这里简单说一下,直接在tomcat7/conf/server.xml 的最下面的Context中增加 sessionCookieName 配置即可:

<Context docBase="F:\workspace\web" path="" reloadable="false" sessionCookieName="WJSESSIONID" />  

7、tomcat放在nginx和redis启动之后

8、测试:

在两个tomcat /webapps/test新建index.jsp

<%@ page language="java" %>
<html>
  <head><title>TomcatA</title></head>
  <body>
 
    <table align="centre" border="1">
      <tr>
        <td>Session ID</td>
        <td><%= session.getId() %></td>
      </tr>
      <tr>
        <td>Created on</td>
        <td><%= session.getCreationTime() %></td>
     </tr>
    </table>
  </body>
</html>
sessionID:<%=session.getId()%> 
<br> 
SessionIP:<%=request.getServerName()%> 
<br> 
SessionPort:<%=request.getServerPort()%> 
<% 
//另一个8090
out.println("This is Tomcat Server 8080"); 
%>  

 

posted on 2016-08-27 15:42  tour1986  阅读(747)  评论(0)    收藏  举报