linux+nginx+tomcat集群(sticky sessions)

本文介绍如何通过Nginx部署Sticky Sessions,实现服务器间的会话粘滞性,确保用户请求被定向到相同的服务器,提高系统稳定性和用户体验。

Nginx简介

Nginx(发音为“engine x”)是一款由俄罗斯软件工程师Igor Sysoev写的开源的web服务器。自从2004年发布以来Nginx一直关注于高性能、高并发、低内存的使用,另外还有一些特色的Web服务器功能,如负载均衡、缓存、访问和带宽控制以及能够有效的与各种应用集成这些特点使得nginx成为现代网站架构中一个不错的选择。目前,nginx在互联网最受欢迎web服务器软件排行榜上排名第二。

服务器管理session方式

服务器通常有两种管理session的方式:replicated sessions和sticky sessions。

第一种是基于复制的session共享,主要功能是修改tomcat的session存储机制,使之能够把session序列化存放到memcached中,代表工具是memcached-session-manager。

第二种是实现基于cookie的“粘性会话”(Sticky  Session),这种方式将同一用户的请求转发到特定的Tomcat服务器上,避免了集群中Session的复制,缺点是用户只跟一种的一台服务器通信,如果此服务器down掉,那就废了。这个功能是基于nginx的扩展功能开发的一个扩展插件实现的,主要的代表有nginx_upstream_jvm_route。

sticky sessions安装部署

1.      准备工作

链接: http://pan.baidu.com/s/1o6DOVwe 密码: ii76,下载所需要的jar包并解压到服务器上。

zlib-1.2.8.tar.gz、pcre-8.35.tar.gz、nginx-1.4.7.tar.gz、nginx_upstream_jvm_route.tar.gz

2.      为nginx添加nginx_upstream_jvm_route的模块

patch -p0 < /opt/http/nginx_upstream_jvm_route/jvm_route.patch 

3.      nginx configure

进入/opt/http/nginx-1.4.7,执行

./configure --prefix=/opt/http/nginxserverfiles--sbin-path=/opt/http/nginxserverfiles/nginx--conf-path=/opt/http/nginxserverfiles/nginx.conf--pid-path=/opt/http/nginxserverfiles/nginx.pid  --with-http_ssl_module--with-pcre=../pcre-8.35 --with-zlib=../zlib-1.2.8--add-module=/opt/http/nginx_upstream_jvm_route  命令

4.      nginx make编译

进入/opt/http/nginx-1.4.7执行make命令

5.      nginx make install

进入/opt/http/nginx-1.4.7执行make install命令

6.      修改nginx配置文件

#设定http服务器,利用它的反向代理功能提供负载均衡支持  

http {

#这里是您需要修改的地方,修改为您的服务器IP:端口号 srun_id为您在tomcat中所配置的jvmRoute  

upstreamhuipu.nginx.com {   

server ip1:8088srun_id=a weight=2;  

server ip2:8088srun_id=b weight=1;  

jvm_route$cookie_JSESSIONID|sessionid reverse;  

}

include      mime.types;

default_type  application/octet-stream;

sendfile       on;

keepalive_timeout 65;

#gzip  on;

server {

       listen       80;

       server_name  huipu.nginx.com;

        location/ {

           root   /data/nginx/web/html;

           index  index.html index.htm index.jsp;

        }

        root/data/nginx/web;

        location~ .*\.(gif|jpg|jpeg|png|bmp|swf|html)$

        {

               expires      30d;

        }

        location~ .*\.(js|css)?$

        {

               expires      1h;

        }

        location~ .*\.(shtml)$ {

               proxy_pass   http://huipu.nginx.com;

               proxy_buffer_size128k;

           proxy_buffers 64 32k;

        }

        location~ \.jsp$ { 

               proxy_pass  http://huipu.nginx.com;

        } 

       error_page   500 502 503 504  /50x.html;

        location= /50x.html {

           root   html;

        }

7.      Tomcat server.xml文件修改

注意端口冲突,修改以下两个节点

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

<ClusterclassName="org.apache.catalina.ha.tcp.SimpleTcpCluster"/>

8.      Tomcat web.xml修改

<welcome-file-list>

 <welcome-file>index.html</welcome-file>

 <welcome-file>index.htm</welcome-file>

 <welcome-file>index.jsp</welcome-file>

</welcome-file-list>

<distributable/>

9.      替换index.jsp

用下面的内容替换掉tomcat webapps/ROOT下的index.jsp

<%@ page language="java"contentType="text/html; charset=UTF-8"

pageEncoding="UTF-8"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<html>

<head>

<meta http-equiv="Content-Type"content="text/html; charset=UTF-8">

<title>Insert title here</title>

</head>

<body>

session is :

<%

HttpSession sess = request.getSession();

sess.setMaxInactiveInterval(100);

out.print(sess);

%>

<br>

cookie is :

<%

out.print(request.getHeader("Cookie"));

%>

</body>

</html>

10.    启动nginx tomcat

./nginx -c /opt/http/nginxserverfiles/nginx.conf

挂掉以前,访问index.jsp


挂掉以后,访问index.jsp


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值