Nginx configuration upstream for load balancing

Source: Internet
Author: User

If Nginx does not only have to proxy a server, it is not as hot as today, Nginx can be configured to proxy multiple servers, when a server down, can still keep the system available. The specific configuration process is as follows:


1. Under the HTTP node, add the upstream node.


Upstream Favtomcat {server 10.0.6.108:7080; Server 10.0.0.85:8980;}


2. Configure the Proxy_pass in the location node under the server node to be://+ upstream name, i.e. "

Http://favtomcat ".


Location/{root HTML;            Index index.html index.htm; Proxy_pass Http://favtomcat;}


3. Load balancing is now initially complete. The upstream is loaded by polling (the default), and each request is assigned to a different back-end server in chronological order, which can be automatically rejected if the backend server is down. Although this method is simple and low cost. However, the disadvantage is low reliability and unbalanced load distribution. Applies to picture server clusters and pure static page server clusters.


In addition, upstream has other allocation strategies, as follows:


weight (weight)


Specifies the polling probability, proportional to the weight and access ratios, for situations where the performance of the backend server is uneven. As shown below, the 10.0.0.88 access ratio is one-fold higher than the 10.0.0.77 access ratio.


Upstream favtomcat{server 10.0.0.77 weight=5; Server 10.0.0.88 weight=10;}


ip_hash (Access IP)


Each request is allocated according to the hash result of the access IP, so that each visitor has fixed access to a back-end server that resolves the session issue.


Upstream favresin{Ip_hash;      Server 10.0.0.10:8080; Server 10.0.0.11:8080;}


Fair (third party)


The response time of the back-end server is allocated to the request, and the response time is short of priority allocation. Similar to the weight assignment policy.


Upstream favresin{server 10.0.0.10:8080;      Server 10.0.0.11:8080; Fair;}


Url_hash (third party)


Assign requests by the hash result of the access URL so that each URL is directed to the same back-end server, which is more efficient when the backend server is cached.


Note: In upstream the hash statement is added, the server statement cannot 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 a status value for each device, with the meanings of these status values as follows:

down indicates that the server before the single is temporarily not participating in the load.


weight By default, the larger the 1.weight, the greater the load weight.


max_fails : The number of times that a request failed is allowed defaults to 1. Returns the error defined by the Proxy_next_upstream module when the maximum number of times is exceeded.


fail_timeout : The time of the pause after the max_fails failure.


backup: When all other non-backup machines are down or busy, request the backup machine. So the pressure on this machine is the lightest.


Upstream bakend{#定义负载均衡设备的Ip及设备状态 Ip_hash;      Server 10.0.0.11:9090 down;      Server 10.0.0.11:8080 weight=2;      Server 10.0.0.11:6060; Server 10.0.0.11:7070 Backup;}



This article is from the "This person's IT World" blog, be sure to keep this source http://favccxx.blog.51cto.com/2890523/1622091

Nginx configuration upstream for load balancing

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.