Nginx Ultra Detailed explanation of Location,rewrite, reverse proxy and load balancing

Source: Internet
Author: User

the grammar of a location

Locltion can position requests in different ways to a different approach (the individual feels a bit like the filter in Java)
1.1location Classification and usage

Location is broadly divided into three categories:

Location = Patt {} [exact match]

Location patt{} [General match]

Location ~ patt{} [regular match]

Location/{//positioning, personal understanding is the filter in Java.
            root   html;   Match the conditional request forwarding Path
            index  index.html index.htm;      Index
        }
If the page you are visiting is xxx.com
First, because the access is/will enter the location and then give the index to index.html
Next go back to visit/index.html continue to hit the location
Then go down to the HTML path and look for index.html
The directory where root is the resource, which is placed in the HTML (the relative path of the HTML folder under the Nginx installation root) and can be used/prefaced to represent the absolute path
Index is indexed 1.2location Execution Process

1, first see whether the exact match is successful, if successful, return the results and stop the parsing process

2, to see whether the common match is successful, if the matching success, the matching success of the longest record down

3, in turn to find a regular match, if there is a match, immediately return the results and stop the parsing process

4. Returns the longest matching record in a normal match if none is matched

second, rewrite rewrite 2.1 Rewriting the instructions used

if (condition) {} set the condition, then rewrite

Set #设置变量

Return #返回状态码

Break #跳出rewrite

Rewrite #重写

2.2 Grammatical formats

IF syntax format

If Space (condition) {

overriding patterns

}


How to write the conditions?

Answer: 3 Kinds of writing

1: "=" to judge equality, for string comparisons

2: "~" is matched by regular (here is the case)

~* case-insensitive Regular

3:-f-d-E To determine whether the file, for the directory, exists.

Example:

if ($remote _addr = 192.168.1.100) {return
403;
}
if ($http _user_agent ~ msie) {
rewrite ^.*$/ie.htm;
Break # (Do not break loops redirect)
}
if (!-e $document _root$fastcgi_script_name) {
rewrite ^.*$/404.html break;
} 
if ($http _user_agent ~* msie) {
set $isie 1;
}
if ($fastcgi _script_name = ie.html) {
set $isie 0;
}
if ($isie 1) {
rewrite ^.*$ ie.html;
}
third, reverse proxy and load balancing 3.1 Reverse proxy

1. The main use of Proxy_pass method

For example:

Location ~. *\. (jpg|png|gif|bmp) $ {
proxy_pass http://imagesever;
}

Upstream Imagesever {
server 127.0.0.1:8080 weight=1 max_fails=2 fail_timeout=30s;
Server 127.0.0.1:8081 weight=1 max_fails=2 fail_timeout=30s;
}

2.upstream Status description for each device

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.

3.upstream Load Balancing mode

Nginx's upstream currently supports 5 different ways of allocating

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.

2, Weight

Specifies the polling probability, proportional to the weight and the access ratio, for the performance of the backend server.

For example:

Upstream Bakend {
server 192.168.0.14 weight=10;
Server 192.168.0.15 weight=10;
}
3, Ip_hash

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.

For example:

Upstream Bakend {
ip_hash;
Server 192.168.0.14:88;
Server 192.168.0.15:80;
}
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 backend {
server server1;
Server Server2;
Fair;
}
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.

Example: Add hash statement in upstream, server statement can not write weight and other parameters, Hash_method is the hash algorithm used

Upstream backend {
server squid1:3128;
Server squid2:3128;
Hash $request _uri;
Hash_method crc32;
}
Tips

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

Increase in servers that need to use load balancing

Proxy_pass http://bakend/;

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.