Configure upstream in Nginx for Load Balancing

Source: Internet
Author: User

Configure upstream in Nginx for Load Balancing

If Nginx does not have to proxy only one server, then it cannot be as popular as today. Nginx can configure proxy for multiple servers. When one server is down, the system remains available. The specific configuration process is as follows:

1. Add an upstream node under the http node.

Upstream linuxidc {
Server 10.0.6.108: 7080;
Server 10.0.0.85: 8980;
}

2. Configure proxy_pass in the location node under the server node as: http: // + upstream name, that is,"
Http: // linuxidc ".


Location /{
Root html;
Index index.html index.htm;
Proxy_pass http: // linuxidc;
}

3. Now the Server Load balancer is complete. Upstream loads according to the round-robin (default) method. Each request is distributed to different backend servers one by one in chronological order. If the backend servers are down, they can be automatically removed. This method is simple and cost-effective. However, the disadvantage is that the reliability is low and load distribution is not balanced. It is applicable to image Server Clusters and pure Static Page Server clusters.

In addition, upstream has other allocation policies as follows:

Weight (weight)

Specify the round-robin probability. weight is proportional to the access ratio, which is used when the backend server performance is uneven. As shown in the following figure, the access rate of 10.0.0.88 is twice higher than that of 10.0.0.77.

Upstream linuxidc {
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 address, so that each visitor accesses a backend server at a fixed time, which can solve the session problem.

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

Fair (third-party)

Requests are allocated based on the response time of the backend server. Requests with short response time are prioritized. Similar to the weight allocation policy.

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

Url_hash (third-party)

Requests are allocated based on the hash result of the access url so that each url is directed to the same backend server. The backend server is effective when it is cached.

Note: When a hash statement is added to upstream, other parameters such as weight cannot be written to the server statement. 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 meanings of these status values are as follows:

Down indicates that the server before a ticket is not involved in the load for the moment.

The default weight value is 1. The larger the weight value, the larger the load weight.

Max_fails: the default number of failed requests is 1. If the maximum number is exceeded, an error defined by the proxy_next_upstream module is returned.

Fail_timeout: The pause time after max_fails failed.

Backup: Requests the backup machine when all other non-backup machines are down or busy. Therefore, this machine is under the least pressure.

Upstream bakend {# define the Ip address and device status of the Server Load balancer Device
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;
}

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.