Load Balancing with Tomcat + Nginx
1. Introduction to Tomcat
- Tomcat is a free and open source Serlvet container, which is a core project in the Jakarta project of the Apache Foundation;
- Developed by Apache, Sun and other companies and individuals.
- Unlike traditional desktop applications, an application in Tomcat is a WAR (Web Archive) file.
- WAR is a Web application format proposed by Sun, and similar to JAR, it is also a compressed package of many files.
2. Install the Tomcat service
Ready to work:
CPU name | operating system | IP address |
---|---|---|
Tomcat-A | CentOS7.x |
192.168.1.1 |
1. Install and configure Tomcat
1) Install JDK and configure JAVA environment:
[root@Tomcat-A ~]# ls
anaconda-ks.cfg apache-tomcat-8.5.16.tar.gz jdk-8u91-linux-x64.tar.gz
[root@Tomcat-A ~]# tar zxf jdk-8u91-linux-x64.tar.gz
[root@Tomcat-A ~]# mv jdk1.8.0_91/ /usr/local/java # 移动,并重命名
[root@Tomcat-A ~]# cat <<END >> /etc/profile # 配置环境变量
export JAVA_HOME=/usr/local/java
export PATH=$PATH:/usr/local/java/bin
END
[root@Tomcat-A ~]# source /etc/profile
[root@Tomcat-A ~]# java -version # 查看 Java 版本号
2) Install the Tomcat software:
[root@Tomcat-A ~]# tar zxf apache-tomcat-8.5.16.tar.gz
[root@Tomcat-A ~]# mv apache-tomcat-8.5.16 /usr/local/tomcat8 # 移动,并重命名
[root@Tomcat-A ~]# /usr/local/tomcat8/bin/startup.sh # 启动 Tomcat
[root@Tomcat-A ~]# netstat -anpt | grep 8080 # Tomcat 的工作端口
[root@Tomcat-A ~]# netstat -anpt | grep java # 查看 Java 端口号
annotation:
- Port 8005: The port number that needs to be used when
shutdown.sh
closing Tomcat. If this port is not activated, Tomcat cannot beshutdown
closed with the command. - Port 8009: It is the port that Tomcat is responsible for establishing connections with other HTTP servers, such as Nginx and Apache to communicate with each other. (AJP/1.3 protocol)
- Port 8080: The port used for HTTP access. (HTTP/1.1 protocol)
2. Verify
3. Configure the Java Web Directory
[root@Tomcat-A ~]# mkdir -p /web/tomcat
[root@Tomcat-A ~]# echo "192.168.1.1:Tomcat-A" > /web/tomcat/index.jsp
4. Modify the Tomcat main configuration file
[root@Tomcat-A ~]# vim /usr/local/tomcat8/conf/server.xml
在148-151行添加:
<Context docBase="/web/tomcat" path="" reloadable="false">
</Context>
annotation:
unpackWARs="true"
: Set to automatically identify war package.autoDeploy="true"
: Enable automatic deployment.Context docBase="/web/tomcat"
: The root directory of the web page.path=""
: Set the web page; it is equivalent to the Location in Nginx.reloadble="false"
: The web application will not be automatically reloaded. If set to ,true
it will be automatically reloaded when the file is changed.
Restart the Tomcat service
[root@Tomcat-A ~]# /usr/local/tomcat8/bin/shutdown.sh
[root@Tomcat-A ~]# /usr/local/tomcat8/bin/startup.sh
verify:
3. Use Tomcat + Nginx to achieve load balancing
Ready to work:
CPU name | operating system | IP address |
---|---|---|
Tomcat-A | CentOS7.x |
192.168.1.1 |
Tomcat-B | CentOS7.x |
192.168.1.2 |
Nginx-Server | CentOS7.x |
192.168.1.3 |
Notice:
- Tomcat-A can use the above experimental materials;
- Tomcat-B has the same steps as Tomcat-A, but the pages should be separated to facilitate testing;
1. Deploy Nginx server
1) Install Nginx service
[root@Nginx-Server ~]# yum -y install pcre-devel zlib-devel popt-devel openssl-devel openssl
[root@Nginx-Server ~]# wget http://www.nginx.org/download/nginx-1.18.0.tar.gz
[root@Nginx-Server ~]# ls
anaconda-ks.cfg nginx-1.18.0.tar.gz
[root@Nginx-Server ~]# tar zxf nginx-1.18.0.tar.gz -C /usr/src/
[root@Nginx-Server ~]# cd /usr/src/nginx-1.18.0/
[root@Nginx-Server nginx-1.18.0]# useradd -M -s /sbin/nologin nginx
[root@Nginx-Server nginx-1.18.0]# ./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-http_stub_status_module && make && make install
[root@Nginx-Server nginx-1.18.0]# ln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
2) Configure Nginx main configuration file
[root@Nginx-Server ~]# vim /usr/local/nginx/conf/nginx.conf
在 32 行增加:
upstream tomcat_server {
server 192.168.1.1:8080 weight=1;
server 192.168.1.2:8080 weight=1;
}
在 49 行添加:
proxy_pass http://tomcat_server;
[root@Nginx-Server ~]# nginx -t # 检查 Nginx 配置文件是否正确
[root@Nginx-Server ~]# nginx # 启用 Nginx 服务
[root@Nginx-Server ~]# ps aux | grep nginx # 查看 Nginx 服务进程
[root@Nginx-Server ~]# netstat -anpt | grep nginx # 查看 Nginx 端口号和进程号
2. Verify
Use a browser to access the IP address of the Nginx server:
keep refreshing the page and observe the page changes:
use the script to view the effect:
[root@Nginx-Server ~]# for i in $(seq 10);do curl http://192.168.1.3;done