nginx learning nine upstream load balancing

Keywords: Nginx

  • grammar
    Syntax: upstream name {...}
    Default:--
    Context:http

     

  • Status of backend server in load balancing scheduling:
    down The current server does not participate in load balancing temporarily
    backup Reserved backup server
    max_fails Number of requests allowed to fail
    fail_timeout Service pause time after Max failed
    max_conns Limit the maximum number of connections received

     

  • Scheduling algorithm of nginx:
    polling Allocate to different back-end servers in chronological order
    Weighted polling The higher the weight value, the higher the access probability assigned
    ip_hash Each request is allocated according to the hash result of the access IP, so that the request from the same IP accesses one
    least_conn The minimum number of connections. The machine with the least number of connections will be distributed
    url_hash The request is allocated according to the hash result of the visited URL. Each URL is directed to the same back-end server
    hash key value hash custom key

     

  • For example, / etc/nginx/conf.d/default.con
    server {
        listen       8001;
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/server.access.log  main;
    
        location / {
            root   /opt/app/code1;
            index  index.html index.htm;
        }
    ... ...
    }
    
    server {
        listen       8002;
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/server.access.log  main;
    
        location / {
            root   /opt/app/code2;
            index  index.html index.htm;
        }
    ... ...
    }
    
    server {
        listen       8003;
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/server.access.log  main;
    
        location / {
            root   /opt/app/code3;
            index  index.html index.htm;
        }
    ... ...
    }
    
    
    server {
        listen       80;
        server_name  localhost;
    
        #charset koi8-r;
        access_log  /var/log/nginx/test_proxy.access.log  main;
    
        location / {
            proxy_pass http://test_upstream;
        }
    ... ...
    }
    
    
    ##General poll
    upstream test_upstream {
    	server localhost:8001;
    	server localhost:8002;
    	server localhost:8003;
    }

     

  • Weighted polling
    upstream test_upstream {
        server localhost:8001;
        server localhost:8002 weight=5;
        server localhost:8003;
    }

     

  • Backup node
    upstream test_upstream {
        server localhost:8001 down; 
        server localhost:8002 backup;
        server localhost:8003 max_fails=1 fail_timeout=10s;
    }

     

  • ip_hash 
    upstream test_upstream {
    	ip_hash;
    	server localhost:8001; 
    	server localhost:8002;
    	server localhost:8003;
    }

     

  • URL hash syntax (from version 1.7.2)
    Syntax: hash key [consistent];
    Default: --
    Context: upstream
    ##Example:
    upstream test_upstream {
    	hash $request_uri;
    	server localhost:8001; 
    	server localhost:8002;
    	server localhost:8003;
    }
     

Posted by zavin on Sat, 04 Jan 2020 14:54:52 -0800