Nginx+Tomcat关于Session的管理

前言

Nginx+Tomcat对Session的管理一直有了解,但是一直没有实际操作一遍,本文从最简单的安装启动开始,通过实例的方式循序渐进的介绍了几种管理session的方式。

nginx安装配置

1.安装nginx

[root@localhost ~]# yum install nginx

提示报如下错误:

No package nginx available.

解决办法安装epel:EPEL是企业版 Linux 附加软件包的简称,EPEL是一个由Fedora特别兴趣小组创建、维护并管理的,针对 红帽企业版 Linux(RHEL)及其衍生发行版(比如 CentOS、Scientific Linux、Oracle Enterprise Linux)的一个高质量附加软件包项目;

[root@localhost ~]# yum install epel-release

安装完之后,即可成功安装nginx;

2.启动、停止nginx

先进入nginx的目录

[root@localhost nginx]# cd /usr/sbin/

执行命令

./nginx 开启
./nginx -s stop  使用kill命令强制杀掉进程
./nginx -s quit  待nginx进程处理任务完毕进行停止
./nginx -s reload

nginx+tomcat负载均衡

1.准备2个tomcat,分别指定端口为8081,8082

drwxr-xr-x. 9 root root      4096 May  7 14:16 apache-tomcat-7.0.88_8081
drwxr-xr-x. 9 root root      4096 May  7 14:16 apache-tomcat-7.0.88_8082

修改webapps/ROOT的index.jsp,方便测试

<%
if(request.getSession().getAttribute("key")==null){
   out.println("key is null,ready init.....");   
   request.getSession().setAttribute("key","value");
}else{
   out.println("key is not null,key="+request.getSession().getAttribute("key"));  
}
%>
<br> 
sessionID:<%=session.getId()%>   
<br>   
sessionCreateTime:<%= session.getCreationTime() %>
<br>
<% 
out.println("tomcat port 8081");   
%> 

最后的输出在两个tomcat下面指定各自的端口号8081和8082

2.nginx配置负载均衡(默认策略)

修改/etc/nginx/下面的nginx.conf

原文地址:https://www.cnblogs.com/jpfss/p/10281011.html