1.nginx是一款轻量级兼备高性能的Http和反向代理服务器。所谓反向代理就是指用户发起访问请求,由代理服务器接受,然后将请求转发给正式服务器,并且将正式服务器处理完的数据返回给客户单,此时代理服务器就表现为一个服务器。

进入nginx的安装目录后,输入相关命令,一个窗口一闪而过,在任务管理器中可以找到它的进程。

浏览器输入localhost,可以看到nginx的欢迎页面!

修改nginx的配置文件,

      worker_processes:工作进程个数,

       worker_connections:单个进程最大连接数,

       server:每一个server相当于一个代理服务器,

       listern:监听端口,默认80,

       server_name:当前服务的域名,可以有多个,用空格分割,

       location:表示匹配的路径,配置/ 表示所有请求都被匹配到这里,

       index:当没有指定主页时,默认会选择这个指定的文件,可多个,空格分割 ,

     proxy_pass:请求转向自定义的服务器列表,

       upstream name{} :服务器集群名称    

  1. 然后再cmd中输入命令nginx -s reload重启nginx;
  2. 部署三个Tomcat,分别修改server.xml 和context.xml

server.xml依次修改 Server的port为8005,8006,8007 ,Connector的port为8081 8082 8083,8009 8010 8011

context.xml增加

 

并在webapps\ROOT下增加session.jsp ,加上标记tomcat 1 tomcat 2 tomcat 3

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>shared session</title>
</head>
<body>
<br>session id=<%=session.getId()%>
<br>tomcat 1
</body>
</html>

将如下几个jar包复制到${TOMCAT_HOME}/lib下

tomcat-redis-session-manager-VERSION.jar
jedis-2.5.2.jar
commons-pool2-2.2.jar 3.修改Ngnix的配置文件增加负载均衡配置
#user  nobody;
#nginx进程数,建议设置为等于CPU总核心数
worker_processes 4; error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; #进程pid文件
pid logs/nginx.pid; events {
worker_connections 65535;
} http {
include mime.types;
default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" '
# '$status $body_bytes_sent "$http_referer" '
# '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on;
#tcp_nopush on; #keepalive_timeout 0;
keepalive_timeout 65; #gzip on; upstream mt.com {
#upstream的负载均衡,weight是权重,可根据机器配置定义权重.weight参数表示权值,权值越高被分配的几率越大.
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8082 weight=2;
server 127.0.0.1:8083 weight=3;
} server {
listen 80;
server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / {
#root html;
index index.html index.htm;
proxy_pass http://mt.com;
proxy_set_header X-Real-IP $remote_addr;
#允许客户端请求的最大单文件字节数
client_max_body_size 100m;
} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root html;
} # proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#} # deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

4.启动redis,tomcat1,tomcat2,tomcat3 访问localhost/session.jsp且不断刷新

可以看到每次刷新都可能访问不同的tomcat,实现了负载均衡,而且session都是一致的,保证了session的一致性

原文参照: https://www.cnblogs.com/zhrxidian/p/5432886.html

      https://www.cnblogs.com/linjiqin/p/5761281.html

      http://blog.csdn.net/lipc_/article/details/52766884

      https://www.cnblogs.com/machanghai/p/5956195.html

Nginx+tomcat+redis集群共享session实现负载均衡的更多相关文章

  1. Nginx+tomcat+redis 集群session共享

    插件资源下载地址:https://github.com/ran-jit/tomcat-cluster-redis-session-manager/releases/tag/2.0.2 一.前置条件 J ...

  2. nginx+tomcat的集群和session复制

    前端服务器采用nginx,后端应用服务器采用tomcat.nginx负责负载均衡,session复制在tomcat上处理. 1.nginx安装(略) 2.nginx配置负载均衡 http { incl ...

  3. keepalived+nginx+tomcat+redis集群环境部署

    1.所需软件.jar包.配置文件下载:http://pan.baidu.com/s/1dFgntst 2.环境说明: centos6.5  64位 主节点:192.168.40.121 副节点:192 ...

  4. nginx+tomcat实现集群,redis实现session共享,软连接实现文件共享:http://blog.csdn.net/hua1586981/article/details/78132710

    转载 2017年02月08日 16:52:41 730 相信很多人都听过nginx,这个小巧的东西慢慢地在吞食apache和IIS的份额.那究竟它有什么作用呢?可能很多人未必了解. 说到反向代理,可能 ...

  5. tomcat redis 集群 session共享

    jcoleman/tomcat-redis-session-manager: Redis-backed non-sticky session store for Apache Tomcathttps: ...

  6. 图文解说:Nginx+tomcat配置集群负载均衡

    图文解说:Nginx+tomcat配置集群负载均衡 博客分类: appserver nginxTomcatUbuntuLinux网络应用  作者:niumd Blog:http://ari.iteye ...

  7. Nginx+Tomcat+MemCached 集群配置手册

    系统实施文档 Nginx+Tomcat+MemCached 集群配置手册 目    录 第1章   概述 1.1   目标 互联网的快速发展带来了互联网系统的高负载和高可用性, 这要求我们在设计系统架 ...

  8. Nginx + Tomcat搭建集群

    一.Tomcat集群带来的好处 1.提高服务的性能,并发能力,以及高可用性 2.提供项目架构的横向扩展能力 二.Tomcat集群实现原理 通过Nginx负载均衡进行请求转发 三.Nginx + Tom ...

  9. LVS集群中的IP负载均衡技术

    LVS集群中的IP负载均衡技术 章文嵩 (wensong@linux-vs.org) 转自LVS官方参考资料 2002 年 4 月 本文在分析服务器集群实现虚拟网络服务的相关技术上,详细描述了LVS集 ...

随机推荐

  1. 工具类(为控件设置圆角) - iOS

    为了便于日常开发效率,因此创建了一些小的工具类便于使用.具体 code 如下:声明: /* 为控件添加边框样式_工具类 */ #import <UIKit/UIKit.h> typedef ...

  2. #leetcode刷题之路11-盛最多水的容器

    给定 n 个非负整数 a1,a2,...,an,每个数代表坐标中的一个点 (i, ai) .在坐标内画 n 条垂直线,垂直线 i 的两个端点分别为 (i, ai) 和 (i, 0).找出其中的两条线, ...

  3. [SHOI2015]脑洞治疗仪(恶心的线段树,区间最大子段和)

    题目描述: 曾经发明了自动刷题机的发明家 SHTSC 又公开了他的新发明:脑洞治疗仪--一种可以治疗他因为发明而日益增大的脑洞的神秘装置. 为了简单起见,我们将大脑视作一个 01 序列.11代表这个位 ...

  4. CSS实现表单

    效果图如下: HTML代码如下: <!DOCTYPE html> <html lang="en"> <head> <meta charse ...

  5. 【解决】MacOS下 Python3.7 使用 pyinstaller 打包后执行报错 Failed to execute script pyi_rth__tkinter

    Fix tcl/tk libs inclusion in tkinter with Python3.7 under MacOS 使用 Pyinstaller 打包时候报错 3027 ERROR: Tc ...

  6. PHP导出数据到表格的实例

    我发现最近的项目需要导出Excel表的页面非常的多,想来这个也是我们常用的功能了,现在我可以很熟练的导出了,但是记得当时自己第一次导出时还是绕了一些弯路的,那么现在我就来记录下我这次用exshop框架 ...

  7. 九、IIC驱动原理分析

    学习目标:学习IIC驱动原理: 一.IIC总线协议 IIC串行总线包括一条数据线(SDA)和一条时钟线(SCL),支持“一主多从”和“多主机”模式:每个从机设备都有唯一的地址来识别. 图 1 IIC ...

  8. ctf题目writeup(5)

    2019.2.1 今天继续bugku的隐写杂项题:题目链接:https://ctf.bugku.com/challenges 1. 这道题下载后用wireshark打开...看了好久也没看出个所以然, ...

  9. P1107 最大整数

    P1107 最大整数 题目描述 设有n个正整数 (n<=20), 将它们连接成一排, 组成一个最大的多位整数. 例如: n=3时, 3个整数13, 312, 343连接成的最大整数为: 3433 ...

  10. Java的HashMap和HashTable

    Java的HashMap和HashTable 1. HashMap 1)  hashmap的数据结构 Hashmap是一个数组和链表的结合体(在数据结构称“链表散列“),如下图示: 当我们往hashm ...