(6) Nginx configuration example three load balancing

First, achieve the effect

    (1) Enter the address http: // server IP / bs-manager in the address bar of the browser, load balancing effect, on average 8081 and 8082 ports;

                    

2. Preparation

    (1) Prepare two tomcat servers

  • Prepare two tomcat servers, one 8081 and one 8082
  • The reverse proxy above has been configured successfully in the second instance. But something needs to be added, as follows.

    (2) Modify the place

  • In the webapps directory of the two tomcats, create a folder named edu, and create a page a.html in the edu folder for testing.
  • Since in the second example, there is an edu folder in 8082, it can only be created under the 8081 folder.
    Then use the command under the vod file:
cp a.html ../edu/  

To complete, view the command

cd ../edu/     # 进入到 edu 目录下

cat a.html #查看内容

Three, load balancing configuration in the nginx configuration file

  •  Modified the configuration of the first example

      å¨è¿éæå ¥ å¾çæè¿ °

Configuration code:

   upstream myserver { 
    
        ip_hash; //按照IP地址hash轮询
        server 208.208.128.122:8081;
        server 208.208.128.122:8082;
    }
    server {
        listen       80;
        server_name  208.208.128.122;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            root   html;
            proxy_pass   http://myserver;
            #proxy_pass   http://127.0.0.1:8081;
            index  index.html index.htm;
    }

Fourth, the final test

 Test URL:  http: // server IP address / edu / a.html

å¨è¿éæå ¥ å¾çæè¿ °å¨è¿éæå ¥ å¾çæè¿ °

Fifth, Nginx distribution server strategy

        With the explosive growth of Internet information, load balance is no longer a very unfamiliar topic. As the name suggests, load balancing is to distribute the load to different service units, which not only ensures the availability of the service, but also ensures that the response is fast enough , To give users a good experience. The rapid increase in access and data traffic has spawned a variety of load balancing products. Many professional load balancing hardware provide good functions, but they are expensive. This makes load balancing software very popular, and nginx is one of them. One, there are Nginx, LVS, Haproxy and other services under Linux to provide load balancing services, and Nginx provides several distribution methods (strategies):

         

  •  Polling (default) Each request is assigned to different back-end servers one by one in chronological order. If the back-end server is down, it can be automatically eliminated. Although this method is simple and low cost. But the disadvantages are: low reliability and unbalanced load distribution. Applicable to image server cluster and pure static page server cluster.
  • weight . weight stands for weight. The default is 1. The higher the weight is, the more clients are assigned. The specified polling probability, weight is proportional to the access ratio, which is used for uneven performance of the back-end server. As shown below, the access ratio of 8082 is twice that of 8081.

upstream myserver {
        server 208.208.128.122:8081 weight=5;   #  在这儿
        server 208.208.128.122:8082 weight=10;
    }
    server {
        listen       80;
        server_name  208.208.128.122;
        location / {
            root   html;
            proxy_pass   http://myserver;
            index  index.html index.htm;
    }
  • ip_hash (access IP). ip_hash Each request is allocated according to the hash result of accessing ip, so that each visitor fixedly accesses a back-end server. Can solve the problem of session.

    upstream myserver {
    	ip_hash;							//  在这儿
        server 208.208.128.122:8081 ;   
        server 208.208.128.122:8082 ;
    }
    server {
        listen       80;
        server_name  208.208.128.122;
        location / {
            root   html;
            proxy_pass   http://myserver;
            index  index.html index.htm;
    }
  • fair (third party). fair (third party), distribute requests according to the response time of the back-end server, priority is given to short response time. Similar to weight distribution strategy.

    upstream myserver {					
        server 208.208.128.122:8081 ;   
        server 208.208.128.122:8082 ;
        fair;            #  在这儿
    }
    server {
        listen       80;
        server_name  208.208.128.122;
        location / {
            root   html;
            proxy_pass   http://myserver;
            index  index.html index.htm;
    }
  • url_hash (third party ) distributes requests according to the hash result of accessing the url, so that each url is directed to the same back-end server, which is more effective when the back-end server is cached.
  • Note: add hash statement in upstream, server statement can not write other parameters such as weight, hash_method is the hash algorithm used
 upstream resinserver{ 
      server 10.0.0.10:7777; 
      server 10.0.0.11:8888; 
      hash $request_uri; 
      hash_method crc32; 
}

upstream can also set status values ​​for each device, the meaning of these status values ​​are as follows:

  • down: indicates that the server before the order does not participate in the load temporarily.
  • weight: The default is 1. The greater the weight, the greater the weight of the load.
  • max_fails: The default number of failed requests is 1. When the maximum number is exceeded, an error defined by the proxy_next_upstream module is returned.
  • fail_timeout: The time to pause after max_fails failed.
  • backup: When all other non-backup machines are down or busy, request the backup machine. So this machine will have the least pressure.
upstream bakend{ #定义负载均衡设备的Ip及设备状态 
      ip_hash; 
      server 10.0.0.11:9090 down; 
      server 10.0.0.11:8080 weight=2 max_fails=1 fail_timeout=1s; 
      server 10.0.0.11:6060 max_fails=1 fail_timeout=1s; 
      server 10.0.0.11:7070 backup; 
}

  

Published 108 original articles · Like 58 · Visits 50,000+

Guess you like

Origin blog.csdn.net/qq_41893274/article/details/104736197