{"id":7467,"date":"2018-05-28T14:46:53","date_gmt":"2018-05-28T06:46:53","guid":{"rendered":"http:\/\/rmohan.com\/?p=7467"},"modified":"2018-05-28T14:46:53","modified_gmt":"2018-05-28T06:46:53","slug":"nginx-load-balancing-and-configuration","status":"publish","type":"post","link":"https:\/\/mohan.sg\/?p=7467","title":{"rendered":"Nginx load balancing and configuration"},"content":{"rendered":"<p><strong><span>Nginx load balancing and configuration<\/span><\/strong><\/p>\n<p><span>1 Load Balancing Overview The\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 origin of load balancing is that when a server has a large amount of traffic per unit time, the server will be under great pressure. When it exceeds its own capacity, the server will crash. To avoid crashing the server. The user has a better experience, born load balancing to share the pressure of the server.\u00a0<\/span><\/p>\n<p><span>\u00a0 \u00a0 Load balancing is essentially implemented by the principle of reverse proxy, is a kind of technology that optimizes server resources and reasonably handles high concurrency, and can balance Server pressure to reduce user request wait time and ensure fault tolerance. Nginx is generally used as an efficient HTTP load balancing server to distribute traffic to multiple application servers to improve performance, scalability, and high availability.\u00a0<\/span><\/p>\n<p><span>\u00a0 \u00a0 Principle: Internal A large number of servers can be built on the network to form a server cluster. When users access the site, they first access the public network intermediate server. The intermediate server is assigned to the intranet server according to the algorithm and shares the pressure of the server. Therefore, each visit of the user will ensure the server. The pressure of each server in the cluster tends to balance, sharing server pressure and avoiding servers\u00a0The collapse of the case.<\/span><\/p>\n<p><span>\u00a0 \u00a0 The nginx reverse proxy implementation includes the following load balancing HTTP, HTTPS, FastCGI, uwsgi, SCGI, and memcached.\u00a0<\/span><br \/>\n<span>To configure HTTPS load balancing, simply use the protocol that begins with &#8216;http&#8217;.\u00a0<\/span><br \/>\n<span>When you want to set load balancing for FastCGI, uwsgi, SCGI, or memcached, use the fastcgi_pass, uwsgi_pass, scgi_pass, and memcached_pass commands, respectively.<\/span><\/p>\n<p><span>2 Common Balancing Mechanisms of Load Balancing<\/span><\/p>\n<p><span>1 round-robin: The requests are distributed to different servers in a polling manner. Each request is assigned to different back-end servers in chronological order. If the back-end server goes down, it is automatically removed to ensure normal service. .<\/span><\/p>\n<p><span>Configuration 1:\u00a0<\/span><br \/>\n<span>upstream server_back {#nginx distribution service request\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 server 192.168.162.49;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 server 192.168.162.50;\u00a0<\/span><br \/>\n<span>}<\/span><\/p>\n<p><span>Configuration 2:\u00a0<\/span><br \/>\n<span>http {\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 upstream servergroup { # service group accepts requests, nginx polling distribution service requests\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server srv1.demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server srv2.demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server srv3.demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 }\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 server {\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 listen 80;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 location \/ {\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 Proxy_pass http:\/\/servergroup; #All requests are proxied to servergroup service group\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 }\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 }\u00a0<\/span><br \/>\n<span>}\u00a0<\/span><br \/>\n<span>\u00a0proxy_pass is followed by proxy server ip, can also be hostname, domain name, ip port mode\u00a0<\/span><br \/>\n<span>\u00a0upstream set load balancing background server list<\/span><\/p>\n<p><span>2 Weight load balancing: If no weight is configured, the load of each server is the same. When there is uneven server performance, weight polling is used. The weight parameter of the specified server is determined by load balancing. a part of.\u00a0Heavy load is great.\u00a0<\/span><br \/>\n<span>Upstream server_back {\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 server 192.168.162.49 weight=3;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 server 192.168.162.50 weight=7;\u00a0<\/span><br \/>\n<span>}<\/span><\/p>\n<p><span>3 least-connected: The next request is allocated to the server with the least number of connections. When some requests take longer to respond, the least connections can more fairly control the load of application instances.\u00a0Nginx forwards the request to the less loaded server.\u00a0<\/span><br \/>\n<span>Upstream servergroup {\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 least_conn;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server srv1.demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server srv2.demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server srv3.demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 }<\/span><\/p>\n<p><span>4 ip-hash: Client-based IP address.\u00a0When load balancing occurs, each request is relocated to one of the server clusters. Users who have logged in to one server then relocate to another server and their login information is lost. This is obviously not appropriate.\u00a0Use ip_hash to solve this problem. If the client has accessed a server, when the user accesses it again, the request will be automatically located to the server through a hash algorithm.<\/span><\/p>\n<p><span>Each request is assigned according to the result of the IP hash, so the request is fixed to a certain back-end server, and it can also solve the session problem\u00a0<\/span><br \/>\n<span>upstream\u00a0\u00a0 \u00a0 \u00a0 \u00a0 server group\u00a0{\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 ip-hash;\u00a0<\/span><br \/>\n<span>server srv1.demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server srv2.demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server srv3. Demo.com;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 }<\/span><\/p>\n<p><span>Attach an instance:\u00a0<\/span><br \/>\n<span>#user nobody;\u00a0<\/span><br \/>\n<span>worker_processes 4;\u00a0<\/span><br \/>\n<span>events {\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 # maximum number of concurrent\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 workers_connections 1024;\u00a0<\/span><br \/>\n<span>}\u00a0<\/span><br \/>\n<span>http{\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 # The list of pending servers to be\u00a0\u00a0 \u00a0 \u00a0 \u00a0 followed by the\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 upstream myserver{\u00a0<\/span><br \/>\n<span># ip_hash instruction to bring the same user to the same server.\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 Ip_hash;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server 125.219.42.4 fail_timeout=60s; tentative time after the failure of #max_fails 60s\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 server 172.31.2.183;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 }<\/span><\/p>\n<p><span>\u00a0 \u00a0 Server{\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # listening port\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 listen 80;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # root\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 location \/ \/\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 # select which server list\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 proxy_pass http:\/\/myserver;\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0<\/span><br \/>\n<span>\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 }\u00a0<\/span><br \/>\n<span>}<\/span><\/p>\n<p><span>Max_fails allows the number of request failures to default to 1\u00a0<\/span><br \/>\n<span>fail_timeout=60s fail_timeout=60s timeout for failed timeouts\u00a0<\/span><br \/>\n<span>down indicates that the current server is not participating in the\u00a0loadbackup. All nonbackup\u00a0<\/span><br \/>\n<span>machines will request backups when they are busy, so their stress will be lightest.<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Nginx load balancing and configuration<\/p>\n<p>1 Load Balancing Overview The origin of load balancing is that when a server has a large amount of traffic per unit time, the server will be under great pressure. When it exceeds its own capacity, the server will crash. To avoid crashing the server. The user has a better [&#8230;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[70],"tags":[],"_links":{"self":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7467"}],"collection":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=7467"}],"version-history":[{"count":1,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7467\/revisions"}],"predecessor-version":[{"id":7468,"href":"https:\/\/mohan.sg\/index.php?rest_route=\/wp\/v2\/posts\/7467\/revisions\/7468"}],"wp:attachment":[{"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7467"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7467"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mohan.sg\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7467"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}