tomcat8 nginx负载均衡 + 动静资源分离, 利用redis 共享 session

一、基本环境:css

   tomcat-8.0   2台  (端口 8180, 8280)nginx

   redis-2.8 (port:6379 )web

   nginx-1.8redis

二、添加tomcat和redis作session共享的jar包  (在tomcat本身的lib下添加session共享所需的jar包,须要注意tomcat的版本对应)apache

    a、commons-pool2-2.4.2.jarjson

    b、jedis-2.7.2.jar缓存

    c、tomcat-redis-session-manager-1.0.jartomcat

    d、tomcat-juli-8.0.23.jar 服务器

三、使tomcat之间共享session,把session放到分布式缓存redis中去。session

     到tomcat的conf目录下找到context.xml配置文件,在标签<Context>下添加:

   <Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />  
    <Manager className="com.radiadesign.catalina.session.RedisSessionManager"  
             host="192.168.1.42"  
             port="6379"  
             database="0"  
             password="test123" 
             maxInactiveInterval="60" />  

 

注:其实就是配置redis服务器信息 

 

四、nginx配置

  a、在nginx.conf文件的http块中增长负载服务器地址

    upstream myser {  
      server localhost:8080;  
      server localhost:8180;  
      #ip_hash;
     } 

b、设置代理和静态资源访问(静态资源访问不通过tomcat,直接走nginx)

        location /{
            proxy_pass http://myser;  
            # proxy_pass http://114.55.37.19:8280$request_uri;
            proxy_redirect  off; 
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;     
            proxy_set_header X-Forwarded-For $http_x_forwarded_for;
        }

        location ~ .*\.(gif|jpg|jpeg|png|bmp|swf)$
        {
            root /home/tomcat/80.apache-tomcat-8.0.26/webapps/yunlong-backend-web/;
            expires 30d;
        }
 
        location ~ .*\.(js|css)?$
       {
            root /home/tomcat/80.apache-tomcat-8.0.26/webapps/yunlong-backend-web/;
            expires 24h;
        }

      location ~ ^/(yunlong.json|yunlong.apk|yunlong_location.json|yunlong_location.apk)        {         root /alidata/download/;       }  

相关文章
相关标签/搜索