The Location instruction in Nginx is an important instruction in Nginxhttpcoremodule. The Location directive is used to configure the matching URI, which is the "/uri/" in the syntax, and can be a string or regular expression. However, if you want to use regular expressions, you must specify a prefix.
Nginx Location Grammar
Basic syntax: location [=|~|~*|^~]/uri/{...}
- = Strict match. If the query matches, the search is stopped and the request is processed immediately.
- ~ to match case sensitivity (regular expressions are available)
- ~* matches case-insensitive (available regular expressions)
- !~ and!~* are case insensitive and case-insensitive mismatches, respectively.
- ^~ If you use this prefix for a regular string, tell Nginx to not test the regular expression if the path matches.
Location syntax Syntax: location [=|~|~*|^~]/uri/{...}
Note:
1, ~ to match case matching
2, ~* for case-insensitive matching
3,!~ and!~* respectively for case-sensitive mismatches and case-insensitive mismatches
Example:
Location =/{
# matches the query/only.
# only match/query.
[Configuration A]
}
Location/{
# matches any query, since all queries begin with/, but regular
# expressions and any longer convent ional blocks would be
# matched.
# matches any query because all requests have been/started. However, regular expression rules and long block rules will be matched by precedence and query.
[Configuration B]
}
Location ^~/images/{
# matches any query beginning With/images/and halts searching,
# so regular expressions Won't is checked.
# matches any queries that have been/images/and stops searching. Any regular expressions will not be tested.
[Configuration C]
}
Location ~*. (gif|jpg|jpeg) $ {
# matches any request ending in gif, JPG, or JPEG. However, all
# requests to The/images/directory is handled by
# Configuration C.
# matches any requests that have an end of GIF, JPG, or JPEG. However, requests for all/images/directories will use Configuration C.
[Configuration D]
}
My add mode, static and dynamic separation
Location ^~/(Images|scripts|styles|upload)/{
root /www/abc.com/www/htdocs;
Expires 30d;
}
Location ~*\. (gif|jpg|jpeg|png|css|ico|html) $ {
root /www/abc.com/www/htdocs;
Expires 30d;
}
If you want to define multiple location, you can have 2 ways:
Use/: Location/{client_max_body_size 200m; proxy_connect_timeout proxy_set_header Host $http _host; proxy_set_header X-forwarded-for $remote _addr; Proxy_pass http://127.0.0.1:8008; location/tmp/{root/; internal} In this way,/tmp can be placed below/, because "/is matching any query, but regular expression rules and long block rules will be prioritized and queried"
Use ~/*: Location ~/tmp/{root/tmp; internal;} location ~//{client_max_body_size 20m; FAs Tcgi_pass Fpass; Include Fastcgi_params; In this way,/tmp must be placed in the front of ~/*, because ~ is a regular match, the regular match is in order, as long as the match will not be matched down. Unless there is a definition = or ^~ in conf, that is, = and ^~ have the highest precedence, and if they match, they will not match any other rules.