: This article mainly introduces nginxtomcat to achieve load balancing. if you are interested in the PHP Tutorial, please refer to it. Using nginx in tomcat to achieve load balancing is very simple.
1. prepare the environment
The IP addresses of the two machines are 192.168.0.166 192.168.0.167.
Nginx 166 installation
Install tomcat in 166 and 167 respectively
Of course, you can choose another machine to install nginx because I don't have that machine, so nginx is installed on 166.
2. configuration file
In the previous article, we talked about the basic configuration of nginx. here we only need to add the following two configurations to the basic configuration.
# Use the domain name www.test.com (you can define it yourself or use an IP address)
upstream www.test.com {
# Server is the default port number of the two tomcat servers for load balancing. the default ip addresses are 8080 and 166.
server 192.168.0.166:8080;
server 192.168.0.167:8080;
}
Server {
listen 80;
server_name www.test.com;
#charset koi8-r;
# Where ~ ^/Rzdb is a regular expression, meaning that rzdb (my project name) appears in the Accessed address)
location ~^/rzdb {
proxy_pass http://www.test.com;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
After the configuration is complete, start nginx.
Access http://www.test.com/rzdb/index.html access successful;
The above section describes how to implement load balancing for nginx tomcat, including some content. I hope to help anyone who is interested in PHP tutorials.