The end of nginx configuration reverse agent and load balancing

Posted by johnseito on Mon, 02 Mar 2020 09:28:25 +0100

Refer to previous articles for specific installation configuration

Article directory

1, Common commands for nginx operations

Prerequisites for using nginx operation command: you must enter the directory of nginx

# View nginx version number
./nginx -v
# Close nginx
./nginx -s stop
# Open nginx
# Reload nginx
./nginx -s reload

nginx profile

Location of nginx configuration file

nginx configuration file consists of three parts

  • Part I, global block

    • From the beginning of the configuration file to the events block, some configuration instructions that affect the overall operation of nginx server will be set.
    • For example, the larger the word "processes 1"; value is, the more concurrent processes can be supported.
  • Part two, events block

    • The instructions of events block design mainly affect the network connection between nginx server and users,
    • For example, the maximum number of connections supported by worker \ u connections 1024.
  • The third part, http block

    • The most frequent part of nginx server configuration
    • http block also includes http global block and server block.

2, nginx configure reverse agent (1)

The essential purpose of reverse proxy: when the window browser accesses and the port is 80, nginx will forward to the address of

Start Tomcat in the linux virtual machine first (enter the bin directory of tomcat, execute. / file to start Tomcat)

Access to

In the configuration file of nginx, change the server name to the ip address of linux.

After this sub configuration, when the window browser accesses and the port is 80, nginx will forward to the address of

After configuration, nginx needs to be reloaded to take effect

cd /usr/local/nginx/sbin Get into sbin Catalog
# Execute command reload
./nginx -s reload

Then we implement nginx reverse proxy.

3, nginx configure reverse agent (2)

Effect to achieve: use nginx reverse proxy, jump to different service ports according to different access paths.

Preparation: prepare two tomcat servers, one port 8080 and one port 8081.

First, kill the tomcat process

[root@bogon src]# ps -ef | grep tomcat
root       8298      1  0 22:36 pts/0    00:00:04 /usr/local/java/jdk1.8.0_231/bin/java -Djava.util.logging.config.file=/opt/apache-tomcat-7.0.96/conf/ -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djdk.tls.ephemeralDHKeySize=2048 -Dignore.endorsed.dirs= -classpath /opt/apache-tomcat-7.0.96/bin/bootstrap.jar:/opt/apache-tomcat-7.0.96/bin/tomcat-juli.jar -Dcatalina.base=/opt/apache-tomcat-7.0.96 -Dcatalina.home=/opt/apache-tomcat-7.0.96 org.apache.catalina.startup.Bootstrap start
root       8949   2973  0 23:17 pts/0    00:00:00 grep --color=auto tomcat
[root@bogon src]# kill 8298
[root@bogon src]# ps -ef | grep tomcat
root       8957   2973  0 23:18 pts/0    00:00:00 grep --color=auto tomcat

Then create two new folders tomcat8080 and tomcat8081 in the / usr/src directory, and use xftp to pass in Tomcat compression package to the two folders respectively.

If the upload error is to give permission with the command chmod 777 folder name.

Separate decompression

tar -xvf apache-tomcat-7.0.96.tar.gz

Start tomcat on port 8080

Start the Tomcat of port 8081, unzip it, enter the conf directory of tomcat, and edit the server.xml configuration file

[root@bogon tomcat8081]# ls
apache-tomcat-7.0.96  apache-tomcat-7.0.96.tar.gz
[root@bogon tomcat8081]# cd apache-tomcat-7.0.96/
[root@bogon apache-tomcat-7.0.96]# ls
bin  BUILDING.txt  conf  lib  LICENSE  logs  NOTICE  RELEASE-NOTES  RUNNING.txt  temp  webapps  work
[root@bogon apache-tomcat-7.0.96]# cd conf
[root@bogon conf]# ls
catalina.policy  context.xml  server.xml  tomcat-users.xml  web.xml
[root@bogon conf]# vim server.xml

First, change this to port 8015

Then change to port 8081 and port 8019 respectively

Then start the tomcat of port 8081

[root@bogon bin]# ls
bootstrap.jar  catalina-tasks.xml            configtest.bat  digest.bat  startup.bat      tomcat-native.tar.gz  version.bat
catalina.bat   commons-daemon.jar           shutdown.bat       tool-wrapper.bat    commons-daemon-native.tar.gz       setclasspath.bat      tomcat-juli.jar
[root@bogon bin]# ./
Using CATALINA_BASE:   /usr/src/tomcat8081/apache-tomcat-7.0.96
Using CATALINA_HOME:   /usr/src/tomcat8081/apache-tomcat-7.0.96
Using CATALINA_TMPDIR: /usr/src/tomcat8081/apache-tomcat-7.0.96/temp
Using JRE_HOME:        /usr/local/java/jdk1.8.0_231
Using CLASSPATH:       /usr/src/tomcat8081/apache-tomcat-7.0.96/bin/bootstrap.jar:/usr/src/tomcat8081/apache-tomcat-7.0.96/bin/tomcat-juli.jar
Tomcat started.

Create folders and test pages to prepare for the test.

Create a new folder in the webapps folder of two tomcat;

Create a new folder edu in webapps of tomcat on port 8080, and a new html file < H1 > on port 8080 < / H1 > in edu;

Create a new folder vod in webapps of tomcat on port 8081, and create a new html file in vod < H1 > 8081!! < / H1 >;

And then visit the

Find the nginx configuration file to configure the reverse agent

[root@bogon vod]# cd /usr/local/nginx/conf
[root@bogon conf]# ls
fastcgi.conf          fastcgi_params          koi-utf  mime.types          nginx.conf          scgi_params          uwsgi_params          win-utf
fastcgi.conf.default  fastcgi_params.default  koi-win  mime.types.default  nginx.conf.default  scgi_params.default  uwsgi_params.default
[root@bogon conf]# vim nginx.conf

Add the following configuration

Note: listen to the address, if the following path is edu, forward to the address; if the following address vod, forward to the address

Then reload nginx and go to the directory / usr/local/nginx/sbin

# Turn off nginx first
./nginx -s stop
# Turn nginx on again

Testing: accessing in windows Explorer

nginx reverse proxy is done.

4, nginx configuration load balancing

Implementation effect: input into browser address bar, load balancing effect, average to port 8080 and port 8081.


  • Two tomcat servers, one 8080 port and one 8081 port, which have been prepared.
  • Create edu folder in webapps directory of two tomcat, and create a.html file in edu folder
  • Load balancing configuration in nginx configuration file

Load balancing configuration

Add the following configuration in the following location.

Add the following rules to server {}

Test: access in window browser

Refresh the following browser, access tomcat on port 8081, and keep refreshing and switching.

This shows that nginx distributes multiple requests equally to different servers, and achieves load balancing.

Server allocation strategy for nginx load balancing

First: polling

Each request is allocated to different back-end servers in chronological order. If the back-end servers are down, they can be automatically eliminated.

Second: weight weight

Weight represents the weight, which is 1 by default. The higher the weight, the more clients are assigned.

upstream myserver {

server weight=5;

server weight=10;


The third is IP hash

Each request is allocated according to the hash result of the access ip, so that each visitor's fixed access is to the back-end server, which can solve the problem of session sharing (single sign on will encounter this problem).

upstream myserver {



server ;


Fourth: fair (third party)

Requests are allocated according to the response time of the back-end server, and those with short response time are allocated preferentially.

upstream myserver {


server ;



Published 14 original articles, won praise 119, visited 20000+
Private letter follow

Topics: Nginx Tomcat Apache xml