CentOS 7 implements Multi-Tomcat Load Balancing through Nginx reverse proxy, centosnginx
System Environment: CentOS 7.3 64-bit + JDK 1.8
Software Version: Nginx 1.8.1, Tomcat 8
I have always been interested in Nginx. I have learned Nginx through many articles on the Internet, but it has not been very effective. In addition, I have been busy some time ago, so I have suspended my study on Nginx, however, I was not so busy recently. I also remembered what our project manager said to me and asked me if I knew Nginx. Then I told him what I learned about Nginx, however, he recommended me to go to the official English document, but it also makes sense that since this item was from that company, it must have recorded everything in his official document, although I only scored more than 50 points in English during the college entrance examination, the full score is 150 points. Even if it seems very difficult, I still insisted on watching the afternoon, but the harvest was indeed very great, it is a lot more rewarding than reading many articles on the Internet. After reading this, I also used the Nginx reverse proxy to achieve Load Balancing for multiple Tomcat servers. So with this article, I hope to help people who need it!
Recommendation: Official Nginx documentation
I. JDK Installation
There are many ways to install JDK, so I will not list them here. Let's just briefly describe the method I have taken.
First, download the JDK used by linuxthrough the official team. Here, we use jdk-8u162-linux-x64.tar.gz.
Create a folder in CentOS by running the following command, upload the downloaded JDK to the folder, and decompress it.
[root@VM_134_196_centos /]# cd /usr[root@VM_134_196_centos usr]# mkdir java[root@VM_134_196_centos usr]# cd java[root@VM_134_196_centos java]# tar -zxvf jdk-8u162-linux-x64.tar.gz
After decompression, run the following command to configure JDK environment variables:
[root@VM_134_196_centos java]# vim /etc/profile
Add the following JDK environment variables to the file.
################################## JDK environment variable configuration ## ############################## export JAVA_HOME =/usr/java/jdk1.8.0 _ 162 export CLASSPATH =.: $ {JAVA_HOME}/jre/lib/rt. jar: $ {JAVA_HOME}/lib/tools. jarexport PATH = $ PATH: $ {JAVA_HOME}/bin
After the configuration is complete, run the following command to make the modification take effect and check whether the configuration is successful.
[root@VM_134_196_centos java]# source /etc/profile[root@VM_134_196_centos java]# java -versionjava version "1.8.0_162"Java(TM) SE Runtime Environment (build 1.8.0_162-b12)Java HotSpot(TM) 64-Bit Server VM (build 25.162-b12, mixed mode)
If the JDK version number is successfully output, the JDK configuration is complete.
So far, our first step is OK!
Ii. Tomcat installation
Tomcat installation is relatively simple. You only need to download the Linux version of Tomcat and upload it to the server and decompress it.
The Tomcat plugin used here is apache-tomcat-8.5.24.tar.gz. After the download is complete, use the following command to create a folder in CentOS and upload the downloaded tomcat file to this folder for decompression. Because multiple Tomcat files are used, after decompression, modify the tomcat folder name and port number.
Create a folder
[root@VM_134_196_centos /]# cd usr[root@VM_134_196_centos usr]# mkdir tomcat
Unzip Tomcat
[root@VM_134_196_centos usr]# cd tomcat/[root@VM_134_196_centos tomcat]# tar -zxvf apache-tomcat-8.5.24.tar.gz
Modify Tomcat file name
[root@VM_134_196_centos tomcat]# mv apache-tomcat-8.5.24 apache-tomcat-8.5.24-1
Modify Tomcat configuration file
[root@VM_134_196_centos tomcat]# vim apache-tomcat-8.5.24-1/conf/server.xml
The port number to be modified in the configuration file, for example, keep the corresponding port number of each Tomcat different.
To reflect the differences between Tomcat, modify the index. jsp file of each tomcat to differentiate tomcat.
[root@VM_134_196_centos tomcat]# vim apache-tomcat-8.5.24-1/webapps/ROOT/index.jsp
Finally, each tomcat is started using the following command:
[root@VM_134_196_centos tomcat]# cd apache-tomcat-8.5.24-1/bin/[root@VM_134_196_centos bin]# ./startup.sh
Then, test whether each Tomcat is successfully started by accessing the server. I have installed three Tomcat servers, two Tomcat 8 servers, and one Tomcat 7 server. Tomcat 7 is only used for testing.
[root@VM_134_196_centos tomcat]# lsapache-tomcat-7.0.82-1 apache-tomcat-7.0.82.tar.gz apache-tomcat-8.5.24-1 apache-tomcat-8.5.24-2 apache-tomcat-8.5.24.tar.gz
So far, Tomcat installation is complete!
Iii. Nginx Installation
Use the wget command to download Nginx and decompress Nginx
[root@VM_134_196_centos /]# cd home[root@VM_134_196_centos home]# wget -c https://nginx.org/download/nginx-1.8.1.tar.gz[root@VM_134_196_centos home]# tar -zxvf nginx-1.8.1.tar.gz
Configuration. The default configuration is used here. Of course, you can also configure it yourself. For specific configuration information, see Nginx installation configuration.
[root@VM_134_196_centos home]# cd nginx-1.8.1/[root@VM_134_196_centos nginx-1.8.1]# ./configure
Compile and install
[root@VM_134_196_centos nginx-1.8.1]# make[root@VM_134_196_centos nginx-1.8.1]# make install
The default installation path of Nginx is/usr/local/nginx. You can also view the nginx location by running the following command:
[root@VM_134_196_centos nginx-1.8.1]# whereis nginxnginx: /usr/local/nginx[root@VM_134_196_centos nginx-1.8.1]# cd /usr/local/nginx
Common Nginx Commands include:
Nginx
Stop-fast shutdown
Quit-graceful shutdown
Reload-reloading the configuration file
Reopen-reopening the log files
Nginx: start;
Stop: Check the nginx process number and kill-9 to disable the process;
Quit: Shut down the nginx process after the task is processed;
Reload: restart nginx;
Reopen: restart the log file.
For more information, see the official Beginner's Guide.
To test whether Ngxin is successfully installed, enter the sbin folder to start Nginx.
[root@VM_134_196_centos nginx]# cd sbin/[root@VM_134_196_centos sbin]# ./nginx
Access the Server IP address directly. If the following information is displayed, the Nginx installation is successful!
4. Configure Nginx for reverse proxy and Server Load balancer
Modify Nginx configuration file
[root@VM_134_196_centos sbin]# vim ../conf/nginx.conf
The final configuration file is
# User nobody; events {# maximum number of connections in each project worker_connections 1024;} http {# address and weight of each upstream server. A higher weight indicates a higher renewal rate upstream alie.com {server 119.29.81.95: 8081 weight = 2; server 119.29.81.95: 8082 weight = 2; server 119.29.81.95: 8083 weight = 1;} server {# port default 80 # configure domain name, because there is no domain name, therefore, use localhost server_name localhost; location/{# reverse proxy, the address here must be consistent with the upstream configured above, to achieve Load Balancing proxy_pass http://alie.com; proxy_redirect default ;}}}
Restart Nginx and perform access tests.
[root@VM_134_196_centos sbin]# ./nginx -s reload
Directly access port 80 of the server, Nginx will forward the request to the configured server cluster through reverse proxy, through continuous refreshing, we can find that the number of visits to two servers with the weight of 2 is more than that with the weight of 1.
Now, using Nginx to implement Load Balancing through reverse proxy is complete.