Nginx绑定IP,解决session共享
1.Nginx通过负载均衡IP地址固定绑定,解决Session共享
upstream note.java.itcast.cn{
ip_hash;
server localhost:8080 weight=1;
server localhost:8081 weight=1;
}
server {
listen 80;
server_name note.java.itcast.cn;
#charset koi8-r;
#access_log logs/host.access.log main;
location / {
proxy_pass http://note.java.itcast.cn;
index index.html index.htm;
}
}
只需要在 upstream添加一个 ip_hash;属性,
相同的请求就会一直请求第一次绑定的这个IP
实现方式
新建一个servlet用于接收请求
@WebServlet("/NginxSessionServlet")
public class NginxSessionServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("当前请求端口:"+req.getLocalPort());
String action=req.getParameter("action");
//向Session中存放一个数据
if(action.equals("setSession")){
req.getSession().setAttribute("username","zhangsan");
}else if(action.equals("getSession")){
resp.getWriter().write((String)req.getSession().getAttribute("username"));
}
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
实现效果
无论访问多少次,都只会请求8080端口


但是这种方式不推荐使用,因为其他的服务器就一直不会使用,达不到负载均衡的目的
推荐使用Spring-session+Redis的方式实现
https://www.cnblogs.com/chx9832/p/12298760.html
Nginx绑定IP,解决session共享的更多相关文章
- 怎么样 解决nginx负载均衡的session共享问题呢
php服务器有多台,用nginx做负载均衡,这样同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,就会出现很多问题,比如说最常见的登录状态,下面提供了几种方式来解决ses ...
- 【转载】解决nginx负载均衡的session共享问题
https://blog.csdn.net/u012081441/article/details/71787164 之前有写过ubuntu环境下搭建nginx环境,今天来谈一下nginx sessio ...
- 几个常见用于解决nginx负载均衡的session共享问题的办法
查了一些资料,看了一些别人写的文档,总结如下,实现nginx session的共享: PHP服务器有多台,用nginx做负载均衡,这样同一个IP访问同一个页面会被分配到不同的服务器上,如果sessio ...
- 解决session共享问题
方法一 使用Nginx让它绑定ip(没有共享所以就没有共享问题了) 配置Nginx upstream backserver { ip_hash; server localhost:8080; serv ...
- nginx+tomcat+redis完成session共享
本文记录nginx+redis+tomcat实现session共享的过程 nginx安装:http://blog.csdn.net/grhlove123/article/details/4783467 ...
- nginx+tomcat+redis完成session共享(转载)
转载:http://blog.csdn.net/grhlove123/article/details/48047735 tomcat7下基于redis的session共享所需jar包: http:// ...
- Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享
Redis+Tomcat+Nginx集群实现Session共享,Tomcat Session共享 ============================= 蕃薯耀 2017年11月27日 http: ...
- nginx+tpmcat+redis实现session共享
nginx+tpmcat+redis实现session共享 版本:nginx nginx-1.8.0.tar.gztomcat apache-tomcat-7.0.78.tar.gzredis re ...
- Nginx+tomcat+redis实现session共享
Nginx+tomcat+redis实现session共享 1,安装nginx,使用yum -y install nginx 这是epel源中的,需要安装epel源. 2,配置nginx. 在ngin ...
随机推荐
- js 预编译
js 运行代码的时候分为几个步骤:语法分析 ==>预编译 ==>解释执行 语法解析:通篇扫描代码,查看语法是否出错 解释执行:读一行 - 解释一行 - 执行一行 预编译执行的操作: // ...
- MySQL必知必会--分 组 数 据
数据分组 目前为止的所有计算都是在表的所有数据或匹配特定的 WHERE 子句的 数据上进行的.提示一下,下面的例子返回供应商 1003 提供的产品数目 但如果要返回每个供应商提供的产品数目怎么办?或者 ...
- webpack打包进行丑化压缩遇到(TypeError Cannot read property 'compilation' of undefined)问题
今天再重新配置老项目node打包环境的时候遇到了一个问题. 在打包的时候报: TypeError: Cannot read property 'compilation' of undefined 错误 ...
- Java对象拷贝备忘
列举 //cglib net.sf.cglib.beans.BeanCopier.create net.sf.cglib.beans.BeanCopier.copy //spring-beans or ...
- R语言常用的矩阵操作
R语言是一门非常方便的数据分析语言,它内置了许多处理矩阵的方法.下面列出一些常用的矩阵操作方法示例. 矩阵的生成 > mat <- matrix(:, ncol = , nrow = , ...
- 学习使用add()()()迭代调用,柯里化处理
将多个参数的函数,转换成单参数函数链 以add()()()举例 function add(){ 使用数组保存参数 let _args = Array.prototype.slice.call(argu ...
- redis深入学习
Redis持久化 官方文档: https://redis.io/topics/persistence 1.RDB和AOF优缺点 RDB: 可以在指定的时间间隔内生成数据集的时间点快照,把当前内存里的状 ...
- P2256 一中校运会之百米跑
----------------------- 题目链接:MIKU --------------------- 我现在发现找BUG的最好方法————喝水 喝一次找一个,喝两次A道题 --------- ...
- 使用高精度计算斐波那契数列 c++
使用高精度计算斐波那契数列 非高精度 Code(Non-high accuracy) 这是不用高精度的代码 #include<bits/stdc++.h> using namespace ...
- Fight Against Monsters Gym - 102222H【贪心】
贪心的策略 #include <bits/stdc++.h> using namespace std; ; typedef long long ll; struct m { int hp, ...