Custom implementation of Nginx URL hash

Source: Internet
Author: User

The use of nginxhttpupstreamrequesthashmodule, increase or reduce the machine when the hash caused by all the confusion is still very worrying, so after a period of careful thinking, think by hand to develop and implement the URL hash rules, You can then use the Nginx location tag or if syntax to achieve more flexibility, operability and usability will be greatly enhanced. However, the configuration is slightly more complex and requires procedural support.

The use of this environment, the main need to consider the form of links, link form can not be/xxx.jsp?id=1 such with, otherwise it will be very complicated to deal with, need to use rewrite this form of URL changes into/1/1.html, The purpose of the added directory is to use the location tag. If it is a pure static page or picture, there will always be a custom directory rule.

First we develop a link division rule, which is somewhat different from the file directory partition rules, it does not need to consider the number of files in the folder, the purpose of this rule is to accommodate enough servers. In general, if the ID is character type, only need to separate 26 letters 10 digits, can accommodate 36 cache server, this is enough. If it is a digital ID, then take the digital id0, you can support 100 cache, is exaggerated enough. Generally speaking, supporting 10 servers should be sufficient, and configuration is easier.

With this directory rule, you can write the configuration by Nginx syntax.

The first is to make a bunch of upstream, if it is even, the theory can be divided more evenly.

Upstream a {

Server 127.0.0.1:3121;

}

Upstream B {

Server 127.0.0.1:3122;

}

#配置一个all的目的是兼容不进行hash的剩余的页面, such as home

Upstream All {

Server 127.0.0.1:3121;

Server 127.0.0.1:3122;

}

1, has been pressed/a/, using location division

Location ~*/a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r/{

Proxy_pass http://a;

}

Location ~*/s|t|u|v|w|x|y|z|0|1|2|3|4|5|6|7|8| 9|0/{

Proxy_pass http://b;

}

Location/{

Proxy_pass Http://all;

}

2, there is a certain link rules, do not want to change, you can use if statements to determine

Location/{

Proxy_pass Http://all;

if ($request _uri ~*/page_ (a|b|c|d|e|f|g|h|i|j|k|l|m|n|o|p|q|r)/) {

Proxy_pass http://a;

}

if ($request _uri ~*/page_ (s|t|u|v|w|x|y|z|0|1|2|3|4|5|6|7|8| 9|0)/) {

Proxy_pass http://b;

}

}

After adding the server, you need to change the configuration, manually separate some directory rules files, the remaining access to the original server, will not cause too serious impact.

In this configuration, such as the home page such a large number of single pages, it has a large number of visits, small capacity, so it is not possible to produce disaster tolerance problem. Working in a round-robin manner would be much better than a cache that was previously divided into death.

Reprint http://www.diyaya.com/a/1298570898400.html

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.