1.准备需要的安装包及jar包
nginx-1.6.3.tar.gz
apache-tomcat-7.0.62.tar.gz
redis-3.0.2.tar.gz
jdk-7u79-linux-x64.tar.gz
jedis-2.1.0.jar
commons-pool-1.6.jar
tomcat-redis-session-manager-tomcat-7.jar
说明:此处使用的jedis版本是2.1.0,commons-pool为1.6,
网上有很多地方使用的jedis2.6以上、commons-pool2.0的版本,会出现class not found的错误,是由于commons-pool2.0版本中缺少commons/pool/impl文件夹。
2.tomcat配置
在context.xml中加入tomcat的session管理
<Valve className="com.radiadesign.catalina.session.RedisSessionHandlerValve" />
<Manager className="com.radiadesign.catalina.session.RedisSessionManager"
host="192.168.2.20"//redis的host地址
port="6379"//端口
database="0"
maxInactiveInterval="60" />//redis的key值超时时间
3.nginx配置
upstream tomcat{
server 127.0.0.1:18099;
server 127.0.0.1:18098;
}
server
{
listen 80;
server_name www.myserver.com;
index maomingIndex.do index.do index.jsp index.html;
location /{
proxy_redirect off;
server 127.0.0.1:18099;
server 127.0.0.1:18098;
}
server
{
listen 80;
server_name www.myserver.com;
index maomingIndex.do index.do index.jsp index.html;
location /{
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-Ip $remote_addr;
proxy_set_header X-Forwarded-For $remote_addr;
client_max_body_size 10m;
client_body_buffer_size 128k;
proxy_connect_timeout 5;//此处时间根据应用实际情况配置
proxy_send_timeout 5;//太短会导致bad gateway的错误
proxy_read_timeout 5;//太长会导致tomcat宕机切换慢
proxy_buffer_size 4k;
proxy_buffers 4 32k;
proxy_busy_buffers_size 64k;
proxy_temp_file_write_size 64k;
proxy_pass http://tomcat;
}
rewrite_log off;
}
proxy_pass http://tomcat;
}
rewrite_log off;
}
4.此处附上对应的jar包和相关下载地址
tomcat-redis-session源码地址:
https://github.com/jcoleman/tomcat-redis-session-manager/downloads
jedis源码地址:
https://github.com/xetorthio/jedis