Nginx load balancing configuration

1.Set up an experimental environment (using docker to deploy two nginx containers) h2>

1) Use to build the first nginx service

[[email protected]
-node4 ~]# docker container run- d --name web01 -p 81:80 nginx
Test visit: http://192.168.56.14:81/
[email protected]:
/# docker exec -it web01 bash< /span>
[email protected]:/# echo web01> /usr/share/nginx/html /index.html

2) Use docker to build the second nginx service

[[email protected]
-node4 ~]# docker container run- d --name web02 -p 82:80 nginx
Test visit: http://192.168.56.14:82/
[email protected]:
/# docker exec -it web02 bash< /span>
[email protected]:/# echo web02> /usr/share/nginx/html /index.html

2. Default rotation training (install nginx on the real host and configure load balancing)

  Rotation training: Each request is assigned to different back-end servers one by one in chronological order. If the back-end server is down down, it can be automatically eliminated.

[[emailprotected] ~]# yum -y install nginx

[[email Protected] ~]# vim /etc/nginx/nginx.conf< /span>
#
### Modify nginx.conf to default to rotation ####

‘‘‘
# 1. Upstream is written by itself and must be placed outside the server
upstream myservers {
server 192.168.56.14:81;
server 192.168.56.14:82;
}

# 2. The server actually already has one by default, you only need to modify the configuration in the location and specify the forwarding proxy.
server {
location / {
proxy_pass http://myservers;
}
}
‘‘‘
[[email protected]
-node4 nginx]# systemctl start nginx < /span>

1, poll (Default)

Each request is allocated to different back-end servers one by one in chronological order, if the back-end The server is down and can be automatically removed.

upstream backserver {

server
192.168.0.14;
server
192.168.0.15;
}

2, weight weight< /span>

Specify the polling probability. The weight is proportional to the access ratio. It is used for the performance of the back-end server. All the situation.

upstream backserver {

server
192.168.0.14 weight=3;
server
192.168.0.15 weight=7;
}

3, IP_hash (IP Binding)

There is a problem with the above method, that is, in the load balancing system, if the user is in a certain Log in to the server, then when the user requests the second time, because we are a load balancing system, will be relocated to the server cluster every time a request is made If a user who has logged in to a certain server relocates to another server, his login information will be lost, which is obviously inappropriate. We can use the ip_hash instruction to solve this problem. If the customer has already visited a certain server, when the user visits again, the request will pass the hash algorithm , Automatically locate the server. Each request is allocated according to the hash result of the access ip, so that each visitor has a fixed access to a back-end server, which can solve the session problem.

upstream backserver {

ip_hash;
server
192.168.0.14:88;
server
192.168.0.15:80;
}

4. fair (section Three-party plug-in)

The request is allocated according to the response time of the back-end server, and the short response time is given priority.

upstream backserver {

server server1;
server server2;
fair;
}

5. url_hash (section Three-party plug-in)

According to the hash result of the access URL, the request is allocated, so that each URL is directed to the same one The back-end server is more effective when the back-end server is a cache.

upstream backserver {

server squid1:
3128;
server squid2:
3128;
hash $request_uri;
hash_method crc32;
}

1) Use to build the first nginx service

[[email protected]
-node4 ~]# docker container run- d --name web01 -p 81:80 nginx
Test visit: http://192.168.56.14:81/
[email protected]:
/# docker exec -it web01 bash< /span>
[email protected]:/# echo web01> /usr/share/nginx/html /index.html

2) Use docker to build the second nginx service

[[email protected]
-node4 ~]# docker container run- d --name web02 -p 82:80 nginx
Test visit: http://192.168.56.14:82/
[email protected]:
/# docker exec -it web02 bash< /span>
[email protected]:/# echo web02> /usr/share/nginx/html /index.html

[[email protected] ~]# yum -y install nginx

[[email Protected] ~]# vim /etc/nginx/nginx.conf< /span>
#
### Modify nginx.conf to default to rotation ####

‘‘‘
# 1. Upstream is written by itself and must be placed outside the server
upstream myservers {
server 192.168.56.14:81;
server 192.168.56.14:82;
}

# 2. The server actually already has one by default, you only need to modify the configuration in the location and specify the forwarding proxy.
server {
location / {
proxy_pass http://myservers;
}
}
‘‘‘
[[email protected]
-node4 nginx]# systemctl start nginx < /span>

upstream backserver {

server
192.168.0.14;
server
192.168.0.15;
}

upstream backserver {

server
192.168.0.14 weight=3;
server
192.168.0.15 weight=7;
}

upstream backserver {

ip_hash;
server
192.168.0.14:88;
server
192.168.0.15:80;
}

upstream backserver {

server server1;
server server2;
fair;
}

upstream backserver {

server squid1:
3128;
server squid2:
3128;
hash $request_uri;
hash_method crc32;
}

Leave a Comment

Your email address will not be published.