展开菜单
首页 精品内容 本月促销 装机必备 Windows PDF教程 macOS软件 IOS软件 Android AI 专题
全部分类

当前位置:

首页 > 编程开发 > nginx+tomcat怎么使用redis session共享

nginx+tomcat怎么使用redis session共享

Redis Assistant
Redis Assistant

Redis Assistant 是一款为Redis设计的高效可视化管理与监控工具,提供多种操作模式,高效处理服务器数据。日常使用里的持续防护和信息管理会更突出,适合把安全控制放进长期使用流程中的场景。

¥19
Windows/macOS/Linux 2026-08-22
正版软件 Windows macOS软件 Linux 数据库管理工具
立即下载

环境准备1、准备一台nginx服务器ip192.168.1.133端口81安装过程:#首先安装依赖:yum-yinstallgcc-c++yum-yinstallpcrepcre-develyum-yinstallzlibzlib-develyum-yinstallopensslopenssl—devel#注意:安装nginx必须使用root用户安装#创建一个nginx目录mkdir/usr/local/src/nginx#进入到nginx目录cd/usr/local/src/nginx#下载或上传安装包

本文目录

    环境准备

    1、准备一台nginx服务器 ip192.168.1.133 端口81

    安装过程:

    #首先安装依赖:
    yum -y install gcc-c++ 
    yum -y install pcre pcre-devel 
    yum -y install zlib zlib-devel 
    yum -y install openssl openssl—devel
    #注意 : 安装nginx必须使用 root 用户安装
    #创建一个nginx目录
    mkdir /usr/local/src/nginx
    #进入到nginx目录
    cd /usr/local/src/nginx
    #下载或上传安装包
    wget http://nginx.org/download/nginx.tar.gz 或 rz上传
    #解压安装包
    tar -xvf nginx.tar.gz
    #进入到解压后的目录
    cd nginx
    # 下面 才开始正式安装
    #把nginx安装到指定用户的目录
    mkdir -p /ucenter/soft/nginx
    #安装配置 prefix为安装目录 user为用户 group为 组
    ./configure --prefix=/ucenter/soft/nginx --user=ucenter --group=ucenter
    #编译
    make 
    #安装
    make install
    #在linux系统中由于非root用户不能占用80端口,所以需要使普通用户以root身份启动nginx。 
    cd /ucenter/soft/nginx/sbin
    #把soft文件下所有的文件所属者修改为ucener -r 表示递归
    chown ucenter:ucenter ./soft/ -r
    #修改 ./nginx 的所属为root
    chown root nginx
    #让普通用户可以使用80端口,可以使用root权限启用nginx
    chmod u+s nginx
    #修改配置文件 在修改配置文件之前 ,要备份该文件
    cd conf/
    # 要注意nginx 的工作进程,一般根据cpu的核数去修改
    vim nginx.conf
    #关闭防火墙,打开80端口
    service iptables stop
    #启动nginx
    ./nginx
    #重启nginx
    ./nginx -s reload
    #关闭nginx
    ./nginx -s stop

    准备一台tomcat服务器,先准备java环境,安装jdk步骤省略

    然后分别安装3个tomcat 服务器ip地址:192.168.1.143,tomcat1 8080端口,tomcat2 8081端口,tomcat3 8082端口。

    nginx+tomcat怎么使用redis session共享

    apache-tomcat-7.0.64/conf/server.xml配置文件修改这三个地方,这样端口就不会冲突

    
    
    

    修改tomcat root目录下index.jsp,分别增加每个tomcat的标识,以及在页面上显示session id

    <%--
    licensed to the apache software foundation (asf) under one or more
    contributor license agreements. see the notice file distributed with
    this work for additional information regarding copyright ownership.
    the asf licenses this file to you under the apache license, version 2.0
    (the "license"); you may not use this file except in compliance with
    the license. you may obtain a copy of the license at
    
     http://www.apache.org/licenses/license-2.0
    
    unless required by applicable law or agreed to in writing, software
    distributed under the license is distributed on an "as is" basis,
    without warranties or conditions of any kind, either express or implied.
    see the license for the specific language governing permissions and
    limitations under the license.
    --%>
    
    <%@ page session="true" %>
    <%
    java.text.simpledateformat sdf = new java.text.simpledateformat("yyyy");
    request.setattribute("year", sdf.format(new java.util.date()));
    request.setattribute("tomcaturl", "http://tomcat.apache.org/");
    request.setattribute("tomcatdocurl", "/docs/");
    request.setattribute("tomcatexamplesurl", "/examples/");
    %>
    
     
      <%=request.getservletcontext().getserverinfo() %>
      
      
      
     
    
     
      
       
        home--<%=request.getsession().getid() %>
        documentation
        configuration
        examples
        wiki
        mailing lists
        find help
        
       
            

    ${pagecontext.servletcontext.serverinfo}--8080

       
                  

    if you're seeing this, you've successfully installed tomcat. congratulations!

        
                           

    recommended reading:

          

    security considerations how-to

          

    manager application how-to

          

    clustering/session replication how-to

         
                   
          server status      
         
          manager app      
         
          host manager      
                               

    developer quick start

        
         
          

    tomcat setup

          

    first web application

         
        
        
         
          

    realms & aaa

          

    jdbc datasources

         
        
        
         
          

    examples

         
        
        
         
          

    servlet specifications

          

    tomcat versions

         
        
                                  

    managing tomcat

          

    for security, access to the manager webapp is restricted.       users are defined in:

          
    $catalina_home/conf/tomcat-users.xml
          

    in tomcat 7.0 access to the manager application is split between        different users.   read more...

                 

    release notes

          

    changelog

          

    migration guide

          

    security notices

                                

    documentation

          

    tomcat 7.0 documentation

          

    tomcat 7.0 configuration

          

    tomcat wiki

          

    find additional important configuration information in:

          
    $catalina_home/running.txt
          

    developers may be interested in:

          
             
    • tomcat 7.0 bug database
    •        
    • tomcat 7.0 javadocs
    •        
    • tomcat 7.0 svn repository
    •       
                                

    getting help

          

    faq and mailing lists

          

    the following mailing lists are available:

          
             tomcat-announce         important announcements, releases, security vulnerability notifications. (low volume).                
    • tomcat-users         user support and discussion        
    •        
    • taglibs-user         user support and discussion for apache taglibs        
    •        
    • tomcat-dev         development mailing list, including commit messages        
    •       
                                
         
          

    other downloads

          
             
    • tomcat connectors
    •        
    • tomcat native
    •        
    • taglibs
    •        
    • deployer
    •       
         
        
        
         
          

    other documentation

          
             
    • tomcat connectors
    •        
    • mod_jk documentation
    •        
    • tomcat native
    •        
    • deployer
    •       
         
        
        
         
          

    get involved

          
             
    • overview
    •        
    • svn repositories
    •        
    • mailing lists
    •        
    • wiki
    •       
         
        
        
         
          

    miscellaneous

          
             
    • contact
    •        
    • legal
    •        
    • sponsorship
    •        
    • thanks
    •       
         
        
        
         
          

    apache software foundation

          
             
    • who we are
    •        
    • heritage
    •        
    • apache home
    •        
    • resources
    •       
         
        
                

    copyright ©1999-${year} apache software foundation. all rights reserved

        

    nginx+tomcat怎么使用redis session共享

    这时候 修改nginx配置文件nginx.conf,把三台tomcat的ip地址以及端口号加入进去,使用nginx做代理

    #user nobody;
    worker_processes 1;
    
    #error_log logs/error.log;
    #error_log logs/error.log notice;
    #error_log logs/error.log info;
    
    #pid  logs/nginx.pid;
    
    
    events {
     worker_connections 1024;
    }
    
    
    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 localhost1 { 
      
       #ip_hash; 
       server 192.168.1.143:8080; 
       server 192.168.1.143:8081; 
       server 192.168.1.143:8082; 
     } 
    
     server {
      listen  81;
      server_name localhost;
    
      #charset koi8-r;
    
      #access_log logs/host.access.log main;
    
      location / {
        proxy_connect_timeout 3; 
        proxy_send_timeout  30; 
        proxy_read_timeout  30; 
        proxy_pass http://localhost1; 
      }
    
      #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;
     # server_name localhost;
    
     # ssl     on;
     # ssl_certificate  cert.pem;
     # ssl_certificate_key cert.key;
    
     # ssl_session_timeout 5m;
    
     # ssl_protocols sslv2 sslv3 tlsv1;
     # ssl_ciphers high:!anull:!md5;
     # ssl_prefer_server_ciphers on;
    
     # location / {
     #  root html;
     #  index index.html index.htm;
     # }
     #}
    
    }

    这时候,分别启动三台tomcat以及nginx,访问,这时候每次刷新页面,都会随机访问8080或者8081或者8082,而且页面上出现的session id也都是不一样的,我们应该如何让这三台tomcat共享session呢,我们使用redis来做。

    这时候,在已经按照三台tomcat的服务器192.168.1.143上,安装redis,安装步骤如下:

    $ wget http://download.redis.io/releases/redis-3.2.3.tar.gz
    $ tar xzf redis-3.2.3.tar.gz
    $ cd redis-3.2.3
    $ make malloc=libc
    #启动redis src前面是安装的路径
    $ src/redis-server &
    
    #关闭redis
    src/redis-cli shutdown
    #使用redis 放入键值对 key value
    $ src/redis-cli
    127.0.0.1:6379> set foo bar
    ok
    127.0.0.1:6379> get foo
    "bar"
    $

    安装完redis之后,在三个tomcat的lib文件夹内分别上传这五个所依赖的jar,分别是

    commons-pool2-2.0.jar

    jedis-2.5.2.jar

    tomcat-redis-session-manager1.2.jar

    tomcat-juli.jar

    tomcat-juli-adapters.jar

    所有jar在这里了,

    然后分别修个三个tomcat的context.xml文件,增加如下的配置

    
    
    
    
     
     web-inf/web.xml
    
     
     
      
      
    

    这时候,分别重启三个tomcat以及nginx,查看tomcat日志之后,发现没有任何异常报错,说明我们成功了,接下来开始测试。

    我们访问nginx服务器地址:

    得到的是8080端口的 tomcat1 ,session id为1a0625767f27ba95ef4d5f061fe0568d 

    nginx+tomcat怎么使用redis session共享

    这时候按f5刷新页面,得到的是8081端口的 tomcat2 ,session id依旧是 1a0625767f27ba95ef4d5f061fe0568d

    nginx+tomcat怎么使用redis session共享

    再次刷新页面,得到的是8082端口的 tomcat3,session id依旧是 1a0625767f27ba95ef4d5f061fe0568d。 、

    nginx+tomcat怎么使用redis session共享

    这时候,说明我们搭建tomcat + nginx负载均衡 + redis session同步成功啦!

    nginx帮助把我们的请求均匀的分发给三个tomcat --》tomcat1 、tomcat2以及tomcat3

     redis帮助我们同步session,这样一来,我们的服务器性能就会提高许多,任何一台tomcat发生故障后,对整体的服务都不会有影响了。

    本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系bd@zhengruan.com
    作者最新文章
    编程开发 nginx tomcat
    相关文章 更多
    精品专题 更多
    装机必备

    正软商城装机必备专区,精选办公、浏览器、安全防护、影音播放、压缩解压、设计创作和系统工具等电脑常用正版软件,帮助用户快速完成新电脑软件配置。

    Windows

    正软商城Windows软件专区,汇集适用于Windows电脑的办公、设计、安全防护、影音播放、开发工具和系统优化软件,提供软件介绍、系统要求、正版授权及购买下载服务。

    PDF教程

    正软商城PDF教程频道提供PDF编辑、转换、合并、拆分、压缩及格式处理方法,同时介绍常用PDF软件和工具的使用技巧。

    macOS软件

    正软商城macOS软件专区,精选适用于Mac电脑的办公、设计、影音、效率、开发和系统工具,提供软件功能介绍、macOS兼容版本、正版授权及购买下载服务。

    IOS软件

    正软商城iOS软件专区,精选适用于iPhone和iPad的办公、学习、影音、设计、效率及AI应用,提供功能介绍、适用设备、系统要求和正版获取方式等信息。

    AI

    正软商城AI软件专区,汇集AI写作、AI绘画、AI视频、AI办公、AI编程、AI翻译、智能客服和数据分析等人工智能工具,提供功能介绍、适用平台、收费方式及正版购买信息。

    Mac软件 更多
    Aperty macOS版
    Aperty macOS版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw macOS版
    AionClaw macOS版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy macOS版
    Loomy macOS版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirBuddy 3 macOS版
    AirBuddy 3 macOS版

    AirBuddy 3 是面向 macOS 的蓝牙外设增强工具,可集中查看 AirPods、Beats 与苹果设备电量,提供快速连接、播放控制、电量提醒、桌面小组件和多台 Mac 外设接力等功能。

    AirServer macOS版
    AirServer macOS版

    AirServer macOS版可把 Mac 转换为无线投屏接收端,通过 AirPlay 或 Google Cast 接收手机、平板和电脑画面,并支持多设备同时连接、高清镜像及会话录制,适合家庭娱乐、课堂教学、会议演示和应用展示。

    AnyDesk macOS版
    AnyDesk macOS版

    AnyDesk macOS版是一款适用于远程办公、设备维护和客户支持的远程桌面工具,可连接并操作异地计算机,支持文件传输、无人值守访问、会话权限控制及跨平台协作。

    WINDOWS 更多
    讯飞绘文 网页版
    讯飞绘文 网页版

    讯飞绘文网页版是科大讯飞推出的AI图文创作与内容运营工具,覆盖热点选题、文章生成、稿件校对、智能配图、排版发布和数据复盘等环节,适合自媒体创作者及内容团队在线使用。

    Maya(Autodesk Maya) Windows版
    Maya(Autodesk Maya) Windows版

    Autodesk Maya 是面向影视、电视、动画和游戏行业的专业三维制作软件,集多边形与 NURBS 建模、角色绑定、关键帧和动作捕捉动画、Bifrost 特效模拟、材质开发及 Arnold 渲染于一体,

    Aperty Windows版
    Aperty Windows版

    Aperty 是 Skylum 推出的 AI 人像修图软件,支持自然肤质修饰、瑕疵处理、面部与身体调整、预设及批量同步。

    火山引擎 网页版
    火山引擎 网页版

    火山引擎是字节跳动旗下企业级云与AI服务平台。聚焦大模型核心能力,依托字节自研豆包多模态大模型,凭借火山方舟一站式平台,提供模型精调、推理部署、智能应用搭建等全链路服务,结合高性能AI算力,助力各行业快速落地大模型数字化与智能化应用。

    立刻MV 网页版
    立刻MV 网页版

    立刻MV 面向音乐人、内容创作者和品牌团队,帮助用户低成本完成歌曲 MV、歌词视频、AI 分镜、图生视频与最终合成。

    蛙蛙写作 网页版
    蛙蛙写作 网页版

    蛙蛙写作 AI 创作工具,覆盖小说、剧本、文案,一键生成文稿,打通文字转视频全流程,高效解决创作难题。

    讯飞智作 网页版
    讯飞智作 网页版

    讯飞智作是一款基于语音合成技术、虚拟人驱动技术与数字人形象声音构建能力的 AI 数字人音视频创作平台。

    超级简历WonderCV 网页版
    超级简历WonderCV 网页版

    超级简历WonderCV是面向应届生和AI小白的智能求职平台,覆盖简历、面试与职业规划。

    音述AI 网页版
    音述AI 网页版

    音述AI,是一个以“用声音述说故事”为核心的 AI 音乐创作与声音分享社区。

    AionClaw Windows版
    AionClaw Windows版

    AionClaw 是可在桌面端运行的任务型 AI 助手,通过数字专家与技能市场组合办公、创作、数据和开发能力。

    Loomy Windows版
    Loomy Windows版

    Loomy 是科大讯飞推出的桌面 AI 助手,可通过对话、任务与工作目录协助处理办公、内容创作和资料整理。

    AirServer Windows版
    AirServer Windows版

    AirServer Windows版可把电脑变成AirPlay、Google Cast和Miracast无线投屏接收端,适合会议演示、课堂教学及家庭大屏分享。发送设备通常无需另装客户端,并可同时接收和智能排列多个镜像会话。

    即将离开本站
    您即将前往第三方网站,请确认是否继续?