location matching rules
1. Examples
server{
location = \ {
[配置A]
}
location / {
[配置B]
}
location = /images/ {
[配置C]
}
location ^~ /static/{
[配置D]
}
localtion ~* \.(gif|jpg|png){
[配置E]
}
}
2. location syntax rules
grammar:
location = | ~ | ~* | ^~ | @ /url/{ //注意 =、~、~*、^~ 、@是location的匹配规则,”|“表示或者
“
......省略其他配置
}
Rule Description:
symbol | meaning |
---|---|
= | Literal precise match, if match, the matching process out (no longer regular match) |
~ | The beginning of the case-sensitive regular match |
~* | The beginning of the case-insensitive regular match |
^~ | Uri represents the beginning of the routine begins with a string brother, is understood to match the path to the url |
/ | General match, in the absence of regular expression matching, will match any request to |
@ | The location is not an ordinary match for variable internal redirect location |
~ | The beginning of the case-sensitive regular match |
Matching the plurality of sequentially arranged location:
- First match: "="
- Second match: "^ ~"
- Then match: file location written order
- Finally all requests to the wildcard ( "/")
Note: after each successful match, matching is stopped, the request will be treated in accordance with the present matching location
Matching the order explained above Example:
- First Comparative LOCATION first, if the match will stop matching, and processing the request in accordance with configuration A:
location = \ {
[配置A]
}
- Secondly LOCATION second comparison, if the match will stop matching configuration in accordance with a processing request and D:
location ^~ /static/{
[配置D]
}
- So in order to compare then, it will stop until a match is found, and in accordance with the matched configuration request is processed *
- If no match occurs the above general selection options and processing the request in accordance with the configuration of the matched B:
location / {
[配置B]
}
3. Application examples
- Precise rules (=)
location = / {
root /var/ww/html/;
}
This rule represents the only match requests for access to the default address, access address: http: // NginxIP /
- Url path matching (^ -)
location ^~ /static/ {
root /var/www/html/static;
}
This rule to match only request "/ static /" address beginning with a visit, visit the address: http: // NginxIP / static /
- Ignore case (~ *)
location ~* \.(png|gif|jpg){
root /var/www/html/;
}
This rule to match only " .png | .gif | * .jpg" file, access the address: http: //NginxIP/test.png
location ~* /Test/ {
root /var/www/html/;
}
This rule indicates the presence of an access request Test match url address, not case-sensitive test can also access the address: http: // NginxIP / test /
- Case-sensitive (~)
location ~ /Test/{
root /var/www/html/;
}
This rule indicates the presence of an access request Test match url address, case sensitive, test is matched, visit the address: http: // NginxIP / Test /
- The default match (all rules can match)
location / {
root /var/www/html;
}
This match is the default rule, he can match all links to the default address access, access address: http: //NginxIP/login.html
- Internal jump (@)
location @index_error{
root /var/www/error/
}
This rule means match at the beginning of the request "/ index /", if the link state 404, will match to this rule
Detailed rewrite rules
nginx and apache's rewrite the function as the main function of rewrite it is to achieve RUL address redirection. Nginx rewrite the function is supported by PCRE software, rewrite module that is ngx_http_rewrite_module
modules. In a sense it can be said for aesthetic or search-friendly search due to improved rankings
1. Examples
server{
listen 80;
server_name www.jkyst.xyz jkyst.xyz;
if($host != 'www.jkyst.xyz' ){
rewrite ^/(.*)$ http://www.jkyst.xyz/$1 permanent;
}
location ~.*\.(png|gif|jpg){
return 403;
}
}
2.rewrite grammar rules
grammar:
rewrite regex replacement [flag]
rewrite: rewrite Rewrite keyword can not be omitted
regex: written here is a regular expression
replacement: Here is content to re-
flag: Here is the final flag mark
flag label Definitions:
flag mark | Explanation |
---|---|
last | That is in the apache (L) mark, indicating the completion rewrite, URL address will not change |
break | This rule match is found, the matching stops, no longer matches the following rules, URL address will not change |
redirect | Returned 302 temporary redirect, the browser displays the URL address after the jump |
permanent | Returned 301 permanent redirect, the browser address bar displays the URL address after the jump, Nginx returns response status code 301 |
Description of related symbols:
symbol | Explanation |
---|---|
* | Representative front 0 or more characters |
+ | 1 or a plurality of characters representative of the front |
? | 0 or a representative of the previous character |
^ | On behalf of beginning of the string |
\ (| Represents the end of the string position | | \) the n- | No. n represents a string parameter |
. | Wildcard for any character |
3. Application Examples
- Multi-domain name to jump to the same domain name
server{
listen 80;
server_name www.jkyst.xyz jkyst.xyz abc.jkyst.xyz;
if($host != 'www.jkyst.xyz'){
rewrite ^/(.*)$ http://www,jkyst.xyz/$1 permanent;
}
}
When accessing "abc.jkyst.xyz" will automatically jump to the "www.jkyst.xyz", appears rewrite rewrite "if" statement must determine whether the result is true then perform an internal statement here
- When the file does not exist redirected to the specified file
server{
listen 80;
server_name www.jkyst.xyz;
if(!-e $request_filename){
rewrite ^/test/(.*)$ http://www.jkyst.xyz/test1/$1 permanent;
}
}
When a file or directory access to "test" directory does not exist, skip to "test1" following the file or directory, the URL will be converted after the jump
server{
listen 80;
server_name www.jkyst.xyz;
if(!-e $request_filename){
rewrite ^/test/(.*)$ index.html last;
}
}
Means that when a file or directory access to "test" directory does not exist, redirect to "index.html" file, not converted after the jump URl
server{
listen 80;
server_name www.jkyst.xyz;
if(!-e $request_filename){
rewrite ^/test/([0-9a-z]+)/([0-9a-z]+)/(.*)$ http://www.jkyst.xyz/test/$1$2$3 permanent;
}
}
Change directory, directory represents the original is converted into another directory, [0-9a-z] represents the converted directory name
- Prohibit file access .sh suffix
server{
......其他配置
location ~.*\.(sh)${
return 405;
}
}
Indicates that when file access "* .sh" will return a 405 error
- Matching the user's browser agent information
server{
listen 80;
server_name www.jkyst.xyz;
if ( $http_user_agent ~* ("Android")|(iPhone)){
rewrite ^/test/(.*)$ http://www.jkyst.xyz/test/$1 permanent;
}
}
Representation of the file under the redirected "test" directory when matched to the browser proxy for Android and iPhone, change the access address
- Htaccess ban
server{
location ~//.ht {
deny all;
}
}
4. File directory matches
parameter | Introduction |
---|---|
-f | Determining whether the file exists returns true that there is |
!-f | Determining whether the file exists that there is no return value of false |
-d | It determines whether a directory exists returns true that there is |
!-d | It determines whether a directory exists that there is no return value of true |
-e | Whether a file or directory exists return value of true existence |
!-e | Whether a file or directory exists return value of true does not exist |
-x | Determine whether the executable file the return value of true Executable |
!-x | Determining whether the file is executable returns true nonexecutable |
for example:
server{
listen 80;
server_name www.jkyst.xyz;
if(!-e $request_filename){
rewrite ^/test/(.*)$ http://www.jkyst.xyz/test1/$1 permanent;
}
}
Whether a file or directory exists