Document preparation
nginx: nginx-1.10.3.tar.gz
tomcat8: apache-tomcat-8.0.43.zip
redis: redis-3.2.8.tar.gz
Ruby (when using redis cluster): ruby-2.4.1.tar.gz
TomcatRedisSessionManager(From GitHub): TomcatRedisSessionManager-1.1.1.zip
jdk :jdk-8u131-linux-x64.tar.gz
Environmental installation
PS: It is recommended that all files be downloaded and uploaded to / usr/local/src; without other instructions, all operations in this tutorial should start with / usr/local/src
redis
tar -zxvf redis-3.2.8.tar.gz
cd redis-3.2.8
make
It is recommended that redis be moved to / usr / local / redis 3.2 or similar memorable directories after installation to facilitate subsequent operations.
Check if the installation is successful after installation
cd src
./redis-server ../redis.conf
Specific reference: redis Installation
nginx
yum -y install make zlib zlib-devel gcc-c++ libtool openssl openssl-devel
wget http://downloads.sourceforge.net/project/pcre/pcre/8.35/pcre-8.35.tar.gz
tar zxvf pcre-8.35.tar.gz
cd pcre-8.35
./configure
make && make install
tar -zxvf nginx-1.10.3.tar.gz
cd nginx-1.10.3
./confignre --prefix=/usr/local/webserver/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre=/usr/local/src/pcre-8.35
make && make install
After installation, enter the sbin directory, enter nginx-s start default port 80, and try to access
The installation of nginx source code is more complicated. Refer to the complete installation tutorial on the internet: Nginx Installation Configuration
tomcat
tar -zxvf apache-tomcat-8.0.43.zip
cd apache-tomcat-8.0.43
cd bin
./startup.sh
The default port is 8080 after booting. Test whether the server is open properly by yourself. If it fails, try the following commands
systemctl stop firewalld
systemctl stop iptabled
nginx+tomcat cluster construction
Duplicate a Tomcat 8
mv apache-tomcat-8.0.43 /usr/local/tomcat8/8081
cp /usr/local/tomcat8/8081 /usr/local/tomcat8/8082
Modify tomcat startup and shutdown ports
Change configuration file
tomcat/conf/server.xml
Change the shutdown port, such as 8005 - > 8015
<Server port="8005" shutdown="SHUTDOWN">
.....
Change the startup port, such as 8080 - > 8081
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
Configure nginx
nginx.conf
#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 tomcatcluster{
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8082 weight=1;
}
server {
listen 80;
server_name localhost;
charset utf-8;
#access_log logs/host.access.log main;
location / {
proxy_pass http://tomcatcluster;
proxy_redirect default;
}
#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;
}
}
}
The core is
upstream tomcatcluster{
server 127.0.0.1:8081 weight=1;
server 127.0.0.1:8082 weight=1;
}
Define an upstream identity as Tomcat cluster;
Server: Define a server
ip:port: Specify the URL
weight: weight, the bigger
location / {
proxy_pass http://tomcatcluster;
proxy_redirect default;
}
http://followed by the identifier specified by upstream, such as Tomcat cluster
start nginx
Enter the nginx directory, such as my / usr/local/nginx
cd /usr/local/nignx
cd sbin
./nginx -s start
Before startup, you can use nignx-t to verify that the configuration file is wrong.
The browser accesses port 80 and, if it doesn't happen, displays the welcome interface of tomcat, so it adds items to tomcat.
Modify tomcat/webapps/ROOT
cd /usr/local/tomcat8/8081
cd webapps/ROOT
rm -rf *
vi index.jsp
Enter the following:
<html>
<body>
<h1>This is tomcat 1 , sessionId: <%=session.getId()%></h1>
<% session.setAttribute("aa","guddqs"); session.setAttribute("bb","bbgudqs"); %>
</body>
</html>
Similar modification 8082 identifies which tomcat service is on jsp
After modification, the page was refreshed several times, and two tomcat pages appeared in turn. So far, the nginx+tomcat load balancing has been completed.
redis shared session for tomcat
PS: This tutorial uses Tomcat 8, and uses a foreign github project. Another redis-session-manager.jar is needed for Tomcat 7.
prepare documents
unzip TomcatRedisSessionManager-1.1.1.zip
cd TomcatRedisSessionManager-1.1.1
ls -l
total 740
-rw-r--r--. 1 root root 61829 Mar 19 2016 commons-logging-1.2.jar
-rw-r--r--. 1 root root 111892 Mar 19 2016 commons-pool2-2.4.1.jar
-rw-r--r--. 1 root root 533252 Feb 21 2016 jedis-2.8.0.jar
-rw-r--r--. 1 root root 1850 Nov 26 00:30 ReadMe.txt
-rw-r--r--. 1 root root 324 Nov 25 23:56 RedisDataCache.properties
-rw-r--r--. 1 root root 28807 Nov 26 00:49 TomcatRedisSessionManager-1.1.1.jar
Four of these jar packages need to be copied to tomcat's lib directory
cp commons-logging-1.2.jar /usr/local/tomcat/8081/lib
cp commons-pool2-2.4.1.jar /usr/local/tomcat/8081/lib
.......
RedisDataCache.properties need to be copied to tomcat under conf
cp RedisDataCache.properties /usr/local/tomcat/8081/conf
Modify configuration files
context.xml
Enter the conf directory of tomcat and modify the context.xml file
Join under < Context > Node again
<Valve className="com.r.tomcat.session.management.RequestSessionHandlerValve" />
<Manager className="com.r.tomcat.session.management.RequestSessionManager" />
RedisDataCache.properties
Then copy it to the RedisDataCache.properties file under conf before modifying it, remember that the file name cannot be changed.
vi RedisDataCache.properties
The single redis configuration is as follows:
redis.hosts=127.0.0.1:6379
# Redis Password
redis.password=
# set true to enable redis cluster mode
redis.cluster.enabled=false
# Redis database (default 0)
#redis.database=0
# Redis connection timeout (default 2000)
#redis.timeout=2000
The redis cluster mode configuration file is as follows:
redis.hosts=127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002,127.0.0.1:3003,127.0.0.1:3004,127.0.0.1:3005
# Redis Password
redis.password=
# set true to enable redis cluster mode
redis.cluster.enabled=true
# Redis database (default 0)
#redis.database=0
# Redis connection timeout (default 2000)
#redis.timeout=2000
PS: redis. password left blank equals no password
Similarly, modify the tomcat configuration file under 8082, copy the jar package, RedisDataCache.properties
Finally, restart two tomcats, visit port 80, refresh the page, and see if the page shows that two different tomcat service orders have the same session id.
Summary
Using nginx reverse proxy to two tomcat servers, you only need to modify tomcat configuration to different ports, add upstream configuration to nginx.conf and point the reverse proxy to upstream.
The session sharing with Tomcat makes use of the external session storage mechanism interface provided by tomcat, while the implementation uses redis as the storage source, thus realizing session sharing.