Nginx configuration and load-balancing algorithm Detailed

1. brief load balancing

If your server nginx web server to do two proxy, load balancing algorithm based on polling, then when you shut down one machine web program can not create web access, then nginx server to distribute or give this request can not access the web server , if the response time of connection here too long, it will result in the client's page has been waiting for a response to the user experience hit discount, here how we avoid this from happening yet. I'm here with Photo to illustrate the next problem.
Here Insert Picture Description
If such a situation occurs web2 load balancing, nginx will first go web1 request, but in the case of poorly configured nginx will continue to distribute requests to web2, web2 then wait for a response until our response time expires, the request will be re distributed web1, where the response time is too long, if the user waiting time will be longer.

2. Preparation

Nginx is equipped with three virtual machines, one to do a reverse proxy server, the other two do real server, simulated load balancing.

192.168.13.129       #反向代理服务器
192.168.13.133       #真实服务器
192.168.13.139       #真实服务器

3. The three-server configuration

我们在反向代理服务器上(192.168.13.139)进行如下配置:
[root@real-server ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下内容
upstream test_server {
      server 192.168.62.157:8080;
      server 192.168.62.158:8080;
    }
 server {
        listen 80;
        server_name localhost;
        location / {
           proxy_pass  http://test_server;
        }
}
[root@server ~]# nginx -t
[root@server ~]# nginx -s reload
第一台真实服务器配置(192.168.13.133):
[root@real-server ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下内容
server {
        listen 80;
        server_name localhost;
        location / {
                root    /usr/share/nginx/html/login;
                index   index.html index.html;
                }
}
[root@real-server ~]# nginx -t
[root@real-server ~]# nginx -s reload
#创建目录和文件并写入测试数据
[root@real-server ~]# mkdir -p /usr/share/nginx/html/login
[root@real-server ~]# echo "this is first real-server" > /usr/share/nginx/html/login/index.html
第二台真实服务器配置(192.168.13.139):
[root@real-server ~]# vim /etc/nginx/conf.d/default.conf
清空并添加以下内容
server {
        listen 80;
        server_name localhost;
        location / {
                root    /usr/share/nginx/html/login;
                index   index.html index.html;
                }
}
[root@real-server ~]# nginx -t
[root@real-server ~]# nginx -s reload
#创建目录和文件并写入测试数据
[root@real-server ~]# mkdir -p /usr/share/nginx/html/login
[root@real-server ~]# echo "this is second real-server" > /usr/share/nginx/html/login/index.html

Test: Open the page and enter the reverse proxy server ip: http: //192.168.13.129/
Here Insert Picture Description
refresh
Here Insert Picture Description

4. Load Balancing Algorithm

upstream supports 4 load balancing scheduling algorithm:

A, 轮询(默认): each request individually assigned to a different time order back-end server;

B, ip_hash: assign each request to access the IP press hash results, with a fixed IP client to access a back-end server. Ensures that requests from the same ip is hit on the stationary machine can solve the session problem.

C, url_hash: url access according to the results of hash allocation request, each url directed to the same back-end server. Back-end server for the cache time efficiency.

D, fair: which is more than the above two intelligent load balancing algorithm. This algorithm can be based on the page size and load duration intelligently load balancing, which is based on an allocation request to the backend server response time, short response time priority allocation. NginxItself is not supported fair, if necessary using this scheduling algorithm, you must download the Nginx upstream_fairmodule.

Configuration Example
1, Hot Standby: If you have two servers, the first server when the accident occurred, was to enable the second server to provide services.
Here Insert Picture Description

upstream test_server {
      server 192.168.13.133:80;
      server 192.168.13.139:80 backup;
    }

Then again, the access direction Proxy IP
Here Insert Picture Description
2. Polling: nginx default polling its weight is default to 1, the order of the access server is the first server, a second, first, and then a second cycle again (because the default polling, there is no need to add any parameters)
3, weighted round-robin: with the configuration according to the weight and size of the different number of distributed servers request different. If not set, the default is 1.
Here Insert Picture Description

upstream test_server {
      server 192.168.13.133:80 weight=1;
      server 192.168.13.139:80 weight=3;
    }

4, ip_hash: the same server nginx will make the same ip client requests.

upstream test_server {
      server 192.168.13.133:80;
      server 192.168.13.139:80;
      ip_hash;
    }

5, nginx load balancing configuration state parameters

  • down, it indicates the current server time being does not participate in load balancing.
  • backup, backup machine reserved. When all other non-backup machine failure or busy, does not request backup machine, so the pressure of the lightest machine.
  • max_fails, allowing the number of failed requests, the default is one. When the number exceeds the maximum, an error is returned.
  • fail_timeout, after a max_fails failures, the unit out of service time of seconds. max_fails can be used with fail_timeout.
upstream test_server {
      server 192.168.13.133:80 weight=2 max_fails=2 fail_timeout=2; 
      #加权轮询的权值是2,允许请求失败两次,然后暂停服务两秒
      server 192.168.13.139:80 weight=3 max_fails=3 fail_timeout=3;
      #加权轮询的权值是3,允许请求失败三次,然后暂停服务三秒
    }

Published 16 original articles · won praise 34 · views 5212

Guess you like

Origin blog.csdn.net/baidu_38803985/article/details/104827395