Example analysis of Tomcat7 Nginx Redis configuration under CentOS6.5

09-11-2023

All configurations are completed on one machine, and the deployment topology information is as follows:

Note: Because the redis configuration is strict with the jar package and tomcat version, be sure to use tomcat7 and the jar package provided in this article.

Download address:

http://pan.baidu.com/s/1bo67ky

tomcat: tomcat1 localhost:8080

tomcat2 localhost:9080

nginx: localhost:1210

redis: localhost:6379

1. Installation and configuration of Tomcat

1. In the server.xml file, to modify the port of tomcat2, there are three changes, namely, 8080, 8005 and 8009, which are changed to 9080, 9005 and 9008 respectively.

After configuring this step, enter http://localhost:8080 and http://localhost:9080 in the browser to see tomcat's home page.

2. nginx configuration to achieve load balancing.

2.1 install pcre. Because the rewrite module of nginx needs pcre support, you need to install the pcre library.

2.1.1. Get the pcre compilation installation package, and you can get the latest version on http://www.pcre.org/.

2.1.2. Decompress the pcre-xx.tar.gz packet.

2.1.3. Enter the unzipping directory and execute. /configure.

2.1.4. make

2.1.5. make install

2.2 install nginx. Since there is no nginx we want in the yum source, we need to create a yum source. The steps are as follows:

2.2.1. vim /etc/yum.repos.d/nginx.repo

Enter the following, then save and exit.

2.2.5 Check whether the configuration file is correct and restart nginx.

Nginx -tservice nginx restart the following commands can also be used here: nginx -s reload.

2.3 Verify the Load Balancing Configuration

2.3.1 Modify the homepage files of tomcat1 and tomcat2 respectively to obtain the login session id value, and add the h1 tag in the red box in the figure below.

2.3.2 Enter: localhost:1210 in the browser to check whether the configuration is successful. As you can see, you have jumped to the tomcat page and the configuration is successful.

3. redis configuration, tomcat shared session.

3.1 download and install redis3

3.1.1 Enter redis official website to download redis3, and the steps are omitted.

3.1.2 Unzip the file

tar -xvf  redis-3.0.2.tar.gz

3.1.3 Compilation and installation.

CD redis-3.0.2 Make Make Install./utils/install _ server.sh # Configure Redis to start randomly.

3.1.4 Start-up and shutdown of Redis.

Service redis_6379 start #6379 is the default port number of redis, and the service name will change after modification according to the requirements.

service redis_6379 stop

service redis_6379 restart

3.2 Configure tomcat to share the session mechanism based on redis.

3.2.1 Copy the following three jar packages to the lib directory of tomcat:

commons-pool-1.3.jar jedis-2.0.0.jar tomcat-redis-session-manager-1.2-tomcat-7-java-7.jar

3.22 modify the context.xml file in tomcat's conf directory, and add the following contents in the node:

3.3 Restart tomcat, enter localhost:1210 in the browser, and find that the session of tomcat1 and tomcat2 has been shared.

Copyright Description:No reproduction without permission。

Knowledge sharing community for developers。

Let more developers benefit from it。

Help developers share knowledge through the Internet。

Follow us