Location Rules
Grammar rule: location [= || * | ^ ~] / uri / {...}
First Match =, followed by matching ^ ~, followed by the positive matching file in order, finally to / generic matches. When there is a match success when stopped matching, processing requests by the current rule matches.
symbol | meaning |
---|---|
= | = Exact matching at the beginning of |
^~ | ~ ^ Represents the beginning of the uri string beginning with a conventional, is understood to match the path to the url. url do not nginx coding, so the request is / static / 20% / aa, may be regularly ^ ~ / static / / aa to match (note that a space) |
~ | ~ Represents the beginning of a case-sensitive match regular |
~* | ~ * Represents the beginning of a case-insensitive regular match |
! ~ And! ~ * | ! ~ And! ~ * Are case-sensitive and case-insensitive match does not match the regular |
/ | Proxy user used (usually a browser) |
$http_x_forwarded_for | You can record the client IP, to record the ip address of the client through a proxy server |
$http_referer | You can record the user is accessing a link which come from |
Matching rules Example:
location = / {
#规则A
}
location = /login {
#规则B
}
location ^~ /static/ {
#规则C
}
location ~ \.(gif|jpg|png|js|css)$ {
#规则D
}
location ~* \.png$ {
#规则E
}
location !~ \.xhtml$ {
#规则F
}
location !~* \.xhtml$ {
#规则G
}
location / {
#规则H
}
So the effect is as follows:
- Access to the root directory /, such as http: // localhost / A will match rule
- Visit http: // localhost / login will match rule B, HTTP: // localhost / H the Register the matching rules
- Visit http: //localhost/static/a.html the matching rule C
- Visit http: //localhost/a.gif,http: //localhost/b.jpg the matching rules D and E rules, but the rules of the order of precedence D, E does not work rules, and http: // localhost / static / c .png precedence rules to match C
- Visit http: //localhost/a.PNG the matching rules E, without matching rules D, E because the rules are not case sensitive.
- Visit http: //localhost/a.xhtml not match the rules and regulations F G, HTTP: //localhost/a.XHTML not match rule G, because it is not case-sensitive. Rules F, G belongs to the exclusion rule, but does not meet the matching rule to match, so think about practical applications where it would be used.
- Visit http: // localhost / category / id / 1111 then the final match to rule H, because the above rules do not match, this time should be nginx forward the request to the back-end application server, such as FastCGI (PHP), tomcat (jsp ), nginx proxy server as the direction of existence.
Actual common rules
Web site root directly matched, visit the Web site home page through the domain name more frequently, using this will accelerate the process.
Here are forwarded directly to the back-end application server, it can be a static home page.
The first mandatory rule
location = / {
proxy_passhttp://tomcat:8080/index
}
The second rule is mandatory static files requests, which is nginx http server as strengths
location ^~ /static/ {
# 请求/static/a.txt 将被映射到实际目录文件:/webroot/res/static/a.txt
root /webroot/res/;
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)${
root /webroot/res/;
}
The third rule is the general rule, to forward the request to the back-end application server dynamic
non-static file requests on the default dynamic requests, grasp their own according to the actual
situation, after all, some of the current framework popular with .php, .jsp suffix is not enough
location / {
proxy_pass http://tomcat:8080/
}
- 1
- 2
- 3
Location resolution process
Summary:
1, first determine the precise hit, if hit, immediately return the results and end the resolution process.
2, the ordinary hit judgment, if there are multiple hits, "recording" down "longest" of hits (but not the end of the recording, whichever is the longest).
3, continue to determine the result of parsing regular expressions, expressions by the order prevail in the configuration of positive, start from top to bottom match, once a successful match, returns the result immediately, and end the resolution process.
4, common shooting order does not matter, because the length is determined by the hit. Regular hit, the order of the so-called, because in the past into the future hits.
5, an exact match = highest priority, regardless of the order they are placed, which will match and be preferentially executed. / Match is the default, that is, if there is no match on another location, then the last match default matching part.