一、基本環境: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/; }