Several ways to achieve load balancing by "Nginx"

Source: Internet
Author: User

http://blog.csdn.net/qq_28602957/article/details/61615876


What is load balancing

The greater the amount of traffic that a server has in a unit of time, the greater the pressure on the server, and the greater the server's ability to withstand, the more it crashes. In order to avoid server crashes and give users a better experience, we share the server pressure in a load-balanced way.

We can build a lot of servers to form a server cluster, when users visit the site, first access to an intermediary server, the intermediary server in the server cluster to select a less pressure server, and then introduce the access request to the server. In this way, each visit of the user will ensure that the pressure of each server in the server cluster tends to balance, sharing the server pressure and avoiding the server crash.

Load balancing is realized by the principle of reverse proxy. Several common ways of load balancing

1. Polling (default)
Each request is assigned to a different back-end server in chronological order, and can be automatically removed if the backend server is down.

Upstream Backserver {
    server 192.168.0.14;
    Server 192.168.0.15;
}
1 2 3 4

2, Weight
Specifies the polling probability, proportional to the weight and the access ratio, for the backend server performance uneven
Situation

Upstream Backserver {
    server 192.168.0.14 weight=3;
    Server 192.168.0.15 weight=7;
}
1 2 3 4

The higher the weight, the greater the probability of being accessed, as in the example above, 30%, 70%, respectively.

3, the above way there is a problem is that in the load balancing system, if the user logged on a server, then the second request of the user, because we are load balancing system, each request will be relocated to a server cluster in one of the It is obviously inappropriate for a user who has already logged on to a server to relocate to another server and their login information will be lost.

We can use the Ip_hash directive to solve this problem, if the customer has access to a server, when the user again access, the request will be the hash algorithm, automatically navigate to the server.

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

Upstream Backserver {
    ip_hash;
    Server 192.168.0.14:88;
    Server 192.168.0.15:80;
}
1 2 3 4 5

4, Fair (third party)
The response time of the backend server is allocated to the request, and the response time is short for priority assignment.

Upstream Backserver {
    server server1;
    Server Server2;
    Fair;
}
1 2 3 4 5

5, Url_hash (third party)
The request is allocated by the hash result of the access URL, which directs each URL to the same back-end server, which is more efficient when cached.

Upstream Backserver {
    server squid1:3128;
    Server squid2:3128;
    Hash $request _uri;
    Hash_method crc32;
}
1 2 3 4 5 6

The status of each device is set to:

1.down indicates that the server before the single is temporarily not participating in the load
2.weight The default is 1.weight larger, the weight of the load will be greater.
3.max_fails: The number of allowed requests failed defaults to 1. Returns the error Proxy_next_upstream module definition when the maximum number of times is exceeded
4.fail_timeout:max_fails after a failure, the time of the pause.
5.backup: All other non-backup machines request backup machines when down or busy. So this machine will be the lightest pressure.

Configuration instance:

#user  Nobody;
Worker_processes  4;
Events {
    # maximum concurrency number
    worker_connections  1024
}
http{
    # To select the server list
    upstream myproject{
        # ip_hash directive to bring the same user into the same server.
        Ip_hash;
        Server 125.219.42.4 fail_timeout=60s;
        Server 172.31.2.183;
        }

    server{
                # Listening Port
                listen;
                # root directory
                location/{
                    # Select which server list
                    proxy_pass http://myproject
                }
}

Related Article

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.