Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How does nginx+tomcat use redis session sharing

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly explains "how nginx+tomcat uses redis session sharing". The content in the article is simple and clear, and it is easy to learn and understand. Please follow the editor's train of thought to study and learn "how nginx+tomcat uses redis session sharing".

Environmental preparation

1. Prepare a nginx server ip192.168.1.133 port 81

Installation process:

# first installation dependency: yum-y install gcc-c++ yum-y install pcre pcre-devel yum-y install zlib zlib-devel yum-y install openssl openssl-devel# Note: to install nginx must use root user installation # create a nginx directory mkdir / usr/local/src/nginx# enter the nginx directory cd / usr/local/src/nginx# download or upload installation package wget http://nginx.org/download/nginx.tar.gz or rz upload # Unzip the installation package tar-xvf nginx.tar.gz# to the extracted directory cd nginx# before the formal installation begins # install nginx to the specified user's directory mkdir-p / ucenter/soft/nginx# installation configuration prefix to the installation directory user to the user group group. / configure-- prefix=/ucenter/soft/nginx-- user=ucenter-- group=ucenter# compilation make # install make install# in the linux system because non-root users cannot occupy port 80 So you need to make an ordinary user launch nginx as root. Cd / ucenter/soft/nginx/sbin# modifies the ownership of all files under the soft file to ucener-r for recursive chown ucenter:ucenter. / soft/-r # modification. / nginx belongs to rootchown root nginx# so that ordinary users can use port 80, you can use root permissions to enable nginxchmod UBSs nginx# to modify the configuration file before modifying the configuration file, cd conf/# should pay attention to the working process of nginx Generally, modify vim nginx.conf# according to the number of cores of cpu, turn off firewall, open port 80 service iptables stop#, start nginx./nginx#, restart nginx./nginx-s reload#, close nginx./nginx-s stop.

Prepare a tomcat server, prepare the java environment first, and omit the steps to install jdk

Then install three tomcat server ip addresses: 192.168.1.143 port tomcat2 1 8080, port tomcat2 8081, port tomcat3 8082.

The apache-tomcat-7.0.64/conf/server.xml configuration file modifies these three places so that the ports do not conflict

Modify the index.jsp under the tomcat root directory, add the logo of each tomcat, and display session id on the page

Home-- documentation configuration examples wiki mailing lists find help ${pagecontext.servletcontext.serverinfo}-8080 if you're seeing this, you've successfully installed tomcat. Congratulations!

[tomcat logo]

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 apache software foundation ${year}. All rights reserved

At this time, modify the nginx configuration file nginx.conf to add the ip address and port number of the three tomcat, and use nginx as the proxy

# user nobody;worker_processes 1 the errorless 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.143time_local 8080 Server 192.168.1.143 server 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.htl; location = / 50x.html {root html;} # proxy the php scripts to apache listening on 127.0.0.1 pass the php scripts to fastcgi server listening on 80 # # location ~. Php$ {# proxy_pass http://127.0.0.1; #} # pass the php scripts to fastcgi server listening on 127.0.0.19000 pass the php scripts to fastcgi server listening on # # location ~. Php$ {# root html # fastcgi_pass 127.0.0.1 include fastcgi_params; 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 highland ssl_certificate_key cert.key; 5m; # ssl_prefer_server_ciphers on # location / {# root html; # index index.html index.htm; #} #}}

At this time, start three tomcat and nginx respectively to visit. Each time you refresh the page, you will randomly visit 8080 or 8081 or 8082, and the session id that appears on the page is also different. How should we make these three tomcat share session? we use redis to do this.

At this point, install redis on the server 192.168.1.143 that is already in accordance with the three tomcat, the installation steps are as follows:

$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# launch redissrc is preceded by the installation path $src/redis-server & # close redissrc/redis-cli shutdown# use the redis to put the key value to key value$ src/redis-cli127.0.0.1:6379 > set foo barok127.0.0.1:6379 > get foo "bar" $

After installing redis, upload the five dependent jar in the lib folder of the three tomcat, respectively

Commons-pool2-2.0.jar

Jedis-2.5.2.jar

Tomcat-redis-session-manager1.2.jar

Tomcat-juli.jar

Tomcat-juli-adapters.jar

All the jar is here.

Then modify three context.xml files of tomcat and add the following configuration

Web-inf/web.xml

At this time, restart the three tomcat and nginx respectively. After viewing the tomcat log, we found that there were no abnormal errors, indicating that we were successful. Let's start the test.

We access the nginx server address:

What you get is tomcat1 of port 8080, and session id is 1a0625767f27ba95ef4d5f061fe0568d.

At this time, press f5 to refresh the page, and you will get tomcat2 on port 8081, and session id is still 1a0625767f27ba95ef4d5f061fe0568d.

Refresh the page again and get that the tomcat3,session id on port 8082 is still 1a0625767f27ba95ef4d5f061fe0568d. 、

At this time, it shows that we have successfully built tomcat + nginx load balancing + redis session synchronization!

Nginx helps distribute our requests evenly to the three tomcat-"tomcat1, tomcat2 and tomcat3"

Redis helps us synchronize session, so that our server performance will be much better, and the failure of any tomcat will not affect the overall service.

Thank you for your reading, the above is the content of "how nginx+tomcat uses redis session sharing". After the study of this article, I believe you have a deeper understanding of how nginx+tomcat uses redis session sharing, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report