nginx+apache+redis实现负载均衡动静分离session共享

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了nginx+apache+redis实现负载均衡动静分离session共享相关的知识,希望对你有一定的参考价值。

环境centos6.5

nginx:192.168.1.202

tomcat1:192.168.1.240

tomcat2:192.168.1.201

redis:192.168.1.116



nginx安装:

yum install -y nginx

如提示包不存在,安装epel源即可

rpm -ivh https://dl.fedoraproject.org/pub/epel/epel-release-latest-6.noarch.rpm

修改nginx配置文件

vim /etc/nginx/conf.d/default.conf
user nginx;
worker_processes 2;
 
error_log/usr/local/nginx/logs/nginx_error.log debug;
pid /usr/local/nginx/nginx.pid;
 
worker_rlimit_nofile 51200;
 
events {
         use epoll;
         worker_connections 51200;
}
 
http {
         includemime.types;
         default_type application/octet-stream;
         #charset gb2312;
         server_names_hash_bucket_size 128;
         client_header_buffer_size 32k;
         large_client_header_buffers 4 32k;
         client_max_body_size 8m;
         sendfile on;
         tcp_nopush on;
         tcp_nodelay on;
         keepalive_timeout 60;
         server_tokens   off;
         client_body_buffer_size  512k;
 
         gzip on;
         gzip_min_length 1k;
         gzip_buffers 16 64k;
         gzip_http_version 1.0;
         gzip_comp_leve l2;
         gzip_types text/plain application/x-javascript text/css application/xml;
         gzip_vary on;
         #limit_zone crawler $binary_remote_addr 10m;
 
         log_formataccess ‘$remote_addr - $remote_user [$time_local] "$request" ‘
         ‘$status$body_bytes_sent "$http_referer" ‘
         ‘"$http_user_agent"$http_x_forwarded_for‘;
         access_log/usr/local/nginx/logs/access.log access;
 
         upstream test {
                   server192.168.1.201:8081 weight=1 max_fails=2 fail_timeout=30s;
                   server192.168.1.240:8081 weight=1 max_fails=2 fail_timeout=30s;
         }
 
server {
         listen80;
         server_name192.168.1.202;
 
location / {
         proxy_next_upstream http_502 http_504 error timeout invalid_header;
         index  index_tel.jsp index.jsp index.html index.htm;
       proxy_pass http://test;
         proxy_redirect    off;
       proxy_set_header Host  $host;
         proxy_set_header X-Real-IP $remote_addr;
         proxy_set_header X-Forwarded-For  $remote_addr;
         proxy_connect_timeout    5;
         proxy_send_timeout      60;
         proxy_read_timeout       5;
         proxy_buffer_size       16k;
         proxy_buffers         4 64k;
         proxy_busy_buffers_size  128k;
         proxy_temp_file_write_size 128k;
    }
 
location ~.*\.(gif|jpg|jpeg|png|bmp|swf|otf|eot|svg|ttf|woff)$
{
valid_referers  blocked 192.168.1.202;
if ($invalid_referer) {
         return421;
}
 
expires 30d;
}
 
location ~ .*\.(js|css)?$
{
root /usr/local/nginx/html;
expires 1h;
} 
 
}
}

 

安装jdktomcat(1.2401.201配置相同)

jdk这里使用1.7.0_40版本绿色免安装的

tar -zxvf jdk-7u40-linux-x64.tar.gz  -C  /usr/local/
cat > /etc/profile.d/java.sh <<EOF
export JAVA_HOME=/usr/local/jdk1.7.0_40
export PATH=$PATH:$JAVA_HOME/bin
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=$JAVA_HOME/lib:$JRE_HOME/lib
EOF

 

tomcat使用7.0.52版本的

tar -zxvf apache-tomcat-7.0.52.tar.gz -C/usr/local/tomcat

 

tomcat-redis-session-manager组件实现session共享

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

 

 

将下载的jar包放置tomcatlib目录下

 

修改tomcat下的context.conf文件

<ValveclassName="com.orangefunction.tomcat.redissessions.RedisSessionHandlerValve"/>
   <ManagerclassName="com.orangefunction.tomcat.redissessions.RedisSessionManager"
       host="192.168.1.116"
       port="6379"
       database="0"
       maxInactiveInterval="60" />

 

wget http://redis.googlecode.com/files/redis-2.6.13.tar.gz
tar -zxvf redis-2.6.13.tar.gz
cd redis-2.6.13 && make && make install
make test

 

mkdir -pv /usr/local/redis/{bin,etc}

src目录里面的redis-benchmark redis-check-aof redis-check-dump  redis-cli  redis-server拷贝至/usr/local/redis/bin下面

redis.conf拷贝至/usr/local/redis/etc目录下

修改redis.conf

daemonize yes #开启后台运行
# bind 127.0.0.1 ::1注释掉
protected-mode no #关闭保护模式


 

测试

mkdir -pv /usr/local/tomcat/webapps/session/WEB-INF/{classes,lib}
vim index.jsp

192.168.1.240:

<%@ page language="java" %>
<html>
 <head><title>TomcatA</title></head>
 <body>
   <h1><font color="red">1.240</font></h1>
   <table border="1">
     <tr>
       <td>Session ID</td>
   <% session.setAttribute("1.240","1.240"); %>
       <td><%= session.getId() %></td>
     </tr>
     <tr>
       <td>Created on</td>
       <td><%= session.getCreationTime() %></td>
    </tr>
   </table>
 </body>
</html>

 

192.168.1.201

<%@ page language="java" %>
<html>
 <head><title>TomcatB</title></head>
 <body>
   <h1><font color="blue">1.201</font></h1>
   <table border="1">
     <tr>
       <td>Session ID</td>
    <%session.setAttribute("1.201","1.201"); %>
       <td><%= session.getId() %></td>
     </tr>
     <tr>
       <td>Created on</td>
       <td><%= session.getCreationTime() %></td>
    </tr>
   </table>
 </body>
</html>

 


本文出自 “linux_cheng” 博客,请务必保留此出处http://6764097.blog.51cto.com/6754097/1795671

以上是关于nginx+apache+redis实现负载均衡动静分离session共享的主要内容,如果未能解决你的问题,请参考以下文章

Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第二部分

Nginx反向代理动静分离负载均衡及rewrite隐藏路径详解(Nginx Apache MySQL Redis)–第一部分(LAMP)

利用nginx实现Redis的负载均衡,应该怎么配置?

.Net 站点在Windows环境借助Nginx和Redis实现负载均衡系列导航栏

通过Nginx TCP反向代理实现Apache Doris负载均衡

Nginx+Tomcat+Redis实现负载均衡会话保持