ホームページ データベース Redis nginx+tomcat で Redis セッション共有を使用する方法

nginx+tomcat で Redis セッション共有を使用する方法

May 30, 2023 pm 06:40 PM
redis nginx tomcat

環境の準備

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 設定ファイル ポートが競合しないように、これら 3 つの場所を変更します。

<server port="8005" shutdown="shutdown">
<connector port="8080" protocol="http/1.1"
connectiontimeout="20000"
redirectport="8443" />
<connector port="8009" protocol="ajp/1.3" redirectport="8443" />
ログイン後にコピー

Tomcat ルート ディレクトリの Index.jsp を変更し、各 Tomcat の ID を追加し、ページにセッション 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.
--%>
<!doctype html>
<%@ 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/");
%>
<html>
 <head>
  <title><%=request.getservletcontext().getserverinfo() %></title>
  <link href="favicon.ico" rel="icon" type="image/x-icon" />
  <link href="favicon.ico" rel="shortcut icon" type="image/x-icon" />
  <link href="tomcat.css" rel="stylesheet" type="text/css" />
 </head>

 <body>
  <div id="wrapper">
   <div id="navigation" class="curved container">
    <span id="nav-home"><a href="${tomcaturl}">home--<%=request.getsession().getid() %></a></span>
    <span id="nav-hosts"><a href="${tomcatdocurl}">documentation</a></span>
    <span id="nav-config"><a href="${tomcatdocurl}config/">configuration</a></span>
    <span id="nav-examples"><a href="${tomcatexamplesurl}">examples</a></span>
    <span id="nav-wiki"><a href="http://wiki.apache.org/tomcat/frontpage">wiki</a></span>
    <span id="nav-lists"><a href="${tomcaturl}lists.html">mailing lists</a></span>
    <span id="nav-help"><a href="${tomcaturl}findhelp.html">find help</a></span>
    <br />
   </div>
   <div id="asf-box">
    <h1>${pagecontext.servletcontext.serverinfo}--8080</h1>
   </div>
   <div id="upper" class="curved container">
    <div id="congrats" class="curved container">
     <h2>if you&#39;re seeing this, you&#39;ve successfully installed tomcat. congratulations!</h2>
    </div>
    <div id="notice">
     <img src="tomcat.png" alt="[tomcat logo]" />
     <div id="tasks">
      <h3>recommended reading:</h3>
      <h4><a href="${tomcatdocurl}security-howto.html">security considerations how-to</a></h4>
      <h4><a href="${tomcatdocurl}manager-howto.html">manager application how-to</a></h4>
      <h4><a href="${tomcatdocurl}cluster-howto.html">clustering/session replication how-to</a></h4>
     </div>
    </div>
    <div id="actions">
     <div>
      <a class="container shadow" href="/manager/status"><span>server status</span></a>
     </div>
     <div>
      <a class="container shadow" href="/manager/html"><span>manager app</span></a>
     </div>
     <div>
      <a class="container shadow" href="/host-manager/html"><span>host manager</span></a>
     </div>
    </div>
    <!--
    <br />
    -->
    <br />
   </div>
   <div id="middle" class="curved container">
    <h3>developer quick start</h3>
    <div>
     <div>
      <p><a href="${tomcatdocurl}setup.html">tomcat setup</a></p>
      <p><a href="${tomcatdocurl}appdev/">first web application</a></p>
     </div>
    </div>
    <div>
     <div>
      <p><a href="${tomcatdocurl}realm-howto.html">realms & aaa</a></p>
      <p><a href="${tomcatdocurl}jndi-datasource-examples-howto.html">jdbc datasources</a></p>
     </div>
    </div>
    <div>
     <div>
      <p><a href="${tomcatexamplesurl}">examples</a></p>
     </div>
    </div>
    <div>
     <div>
      <p><a href="http://wiki.apache.org/tomcat/specifications">servlet specifications</a></p>
      <p><a href="http://wiki.apache.org/tomcat/tomcatversions">tomcat versions</a></p>
     </div>
    </div>
    <br />
   </div>
   <div id="lower">
    <div id="low-manage" class="">
     <div class="curved container">
      <h3>managing tomcat</h3>
      <p>for security, access to the <a href="/manager/html">manager webapp</a> is restricted.
      users are defined in:</p>
      <pre class="brush:php;toolbar:false">$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:

getting help

faq and mailing lists

the following mailing lists are available:


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

ログイン後にコピー

nginx+tomcat怎么使用redis session共享

次の場所で nginx 設定を変更します。今回は nginx.conf を作成し、3 つの 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 &#39;$remote_addr - $remote_user [$time_local] "$request" &#39;
 #     &#39;$status $body_bytes_sent "$http_referer" &#39;
 #     &#39;"$http_user_agent" "$http_x_forwarded_for"&#39;;

 #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&#39;s document root
  # concurs with nginx&#39;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;
 # }
 #}

}
ログイン後にコピー

このとき、3 つの Tomcat と nginx をそれぞれ起動し、アクセスして毎回更新しますページは 8080 または 8081 または 8082 にランダムにアクセスし、ページに表示されるセッション ID も異なります。これら 3 つの Tomcat でセッションを共有するにはどうすればよいでしょうか? これには Redis を使用します。

この時点では、3 つの 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 をインストールした後、3 つの Tomcat ライブラリをインストールします。フォルダー内のそれぞれの 5 つの依存 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 はここにあります。

次に、3 つの Tomcat コンテキストを修復します。 .xml ファイルを開き、次の設定を追加します

<?xml version=&#39;1.0&#39; encoding=&#39;utf-8&#39;?>

<context>

 <!-- default set of monitored resources -->
 <watchedresource>web-inf/web.xml</watchedresource>

 
 <!-- 这里增加redis session共享的配置 6379是redis的端口-->
 <valve classname="com.orangefunction.tomcat.redissessions.redissessionhandlervalve" /> 
 <manager classname="com.orangefunction.tomcat.redissessions.redissessionmanager" 
 host="127.0.0.1" 
 port="6379" 
 database="0" 
 maxinactiveinterval="60" /> 
</context>
ログイン後にコピー

この時点で、3 つの Tomcat と nginx をそれぞれ再起動します。Tomcat のログを確認したところ、異常なエラーはないことがわかりました。これは成功したことを意味します。テスト中。

nginx サーバーのアドレスにアクセスします:

得られるのはポート 8080 の tomcat1 で、セッション ID は 1a0625767f27ba95ef4d5f061fe0568d

nginx+tomcat怎么使用redis session共享

この時点で f5 を押してページを更新します。取得されるのはポート 8081 の tomcat2 です。セッション ID はまだ 1a0625767f27ba95ef4d5f061fe0568d

nginx+tomcat怎么使用redis session共享

です。ページをもう一度更新してください。得られるものはポート 8082 の tomcat3 です。セッション ID はまだ 1a0625767f27ba95ef4d5f061fe0568d です。 ,

nginx+tomcat怎么使用redis session共享

#現時点では、tomcat nginx の負荷分散、redis セッションの同期が正常に設定されたことを意味します。

nginx はリクエストを 3 つの Tomcat に均等に分散するのに役立ちます -->>tomcat1、tomcat2、tomcat3

redis はセッションの同期に役立ち、サーバーのパフォーマンスが向上します。いずれかの Tomcat が失敗した場合、多くの場合、そこに問題が発生します。サービス全体には影響しません。

以上がnginx+tomcat で Redis セッション共有を使用する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

このウェブサイトの声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

Video Face Swap

Video Face Swap

完全無料の AI 顔交換ツールを使用して、あらゆるビデオの顔を簡単に交換できます。

ホットツール

メモ帳++7.3.1

メモ帳++7.3.1

使いやすく無料のコードエディター

SublimeText3 中国語版

SublimeText3 中国語版

中国語版、とても使いやすい

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

SublimeText3 Mac版

SublimeText3 Mac版

神レベルのコード編集ソフト(SublimeText3)

Dockerコンテナの名前を確認する方法 Dockerコンテナの名前を確認する方法 Apr 15, 2025 pm 12:21 PM

すべてのコンテナ(Docker PS)をリストする手順に従って、Dockerコンテナ名を照会できます。コンテナリストをフィルタリングします(GREPコマンドを使用)。コンテナ名(「名前」列にあります)を取得します。

Nginxが開始されるかどうかを確認する方法 Nginxが開始されるかどうかを確認する方法 Apr 14, 2025 pm 01:03 PM

nginxが開始されるかどうかを確認する方法:1。コマンドラインを使用します:SystemCTLステータスnginx(Linux/unix)、netstat -ano | FindStr 80(Windows); 2。ポート80が開いているかどうかを確認します。 3.システムログのnginx起動メッセージを確認します。 4. Nagios、Zabbix、Icingaなどのサードパーティツールを使用します。

Centos RedisでLUAスクリプト実行時間を構成する方法 Centos RedisでLUAスクリプト実行時間を構成する方法 Apr 14, 2025 pm 02:12 PM

Centosシステムでは、Redis構成ファイルを変更するか、Redisコマンドを使用して悪意のあるスクリプトがあまりにも多くのリソースを消費しないようにすることにより、LUAスクリプトの実行時間を制限できます。方法1:Redis構成ファイルを変更し、Redis構成ファイルを見つけます:Redis構成ファイルは通常/etc/redis/redis.confにあります。構成ファイルの編集:テキストエディター(VIやNANOなど)を使用して構成ファイルを開きます:sudovi/etc/redis/redis.conf luaスクリプト実行時間制限を設定します。

Windowsでnginxを構成する方法 Windowsでnginxを構成する方法 Apr 14, 2025 pm 12:57 PM

Windowsでnginxを構成する方法は? nginxをインストールし、仮想ホスト構成を作成します。メイン構成ファイルを変更し、仮想ホスト構成を含めます。 nginxを起動またはリロードします。構成をテストし、Webサイトを表示します。 SSLを選択的に有効にし、SSL証明書を構成します。ファイアウォールを選択的に設定して、ポート80および443のトラフィックを許可します。

Dockerによってコンテナを起動する方法 Dockerによってコンテナを起動する方法 Apr 15, 2025 pm 12:27 PM

Docker Containerの起動手順:コンテナ画像を引く:「Docker Pull [Mirror Name]」を実行します。コンテナの作成:「docker create [options] [mirror name] [コマンドとパラメーター]」を使用します。コンテナを起動します:「docker start [container name or id]」を実行します。コンテナのステータスを確認してください:コンテナが「Docker PS」で実行されていることを確認します。

Docker用のコンテナを作成する方法 Docker用のコンテナを作成する方法 Apr 15, 2025 pm 12:18 PM

Dockerでコンテナを作成します。1。画像を引く:Docker Pull [ミラー名]2。コンテナを作成:Docker Run [Options] [Mirror Name] [コマンド]3。コンテナを起動:Docker Start [Container Name]

nginx apacheを実行する方法 nginx apacheを実行する方法 Apr 14, 2025 pm 12:33 PM

nginxをApacheを実行するには、次のことが必要です。1。nginxとapacheをインストールします。 2。nginxエージェントを構成します。 3。nginxとapacheを起動します。 4.構成をテストして、ドメイン名にアクセスした後にApacheコンテンツを確認できることを確認します。さらに、ポート番号マッチング、仮想ホスト構成、SSL/TLS設定など、他の問題に注意を払う必要があります。

Centos7にRedisをインストールする方法 Centos7にRedisをインストールする方法 Apr 14, 2025 pm 08:21 PM

公式Redisソースからソースコードパッケージをダウンロードして、コンパイルしてインストールして、最新の安定したバージョンを確保し、パーソナライズされた方法でカスタマイズできます。特定の手順は次のとおりです。ソフトウェアパッケージリストを更新してRedisディレクトリを作成するRedisソースコードパッケージをダウンロードしてソースコードパッケージを解凍し、インストール構成をコンパイルし、Redis構成を変更してRedisの起動ステータスを確認します

See all articles