[Posts] nginx location configuration explained in detail

nginx location configuration explained in detail

http://outofmemory.cn/code-snippet/742/nginx-location-configuration-xiangxi-explain

 


Grammar rules: location [=|~|~*|^~] /uri/ { … }

  • = It represents the beginning of an exact match

  • ^~ Uri represents the beginning of a string beginning with a conventional, understood as the match url path. url do not nginx coding, so the request is / static / 20% / aa, may be regularly ^ ~ / static / / aa matched (note the space).

  • ~ It represents the beginning of a regular case-insensitive match

  • ~* It represents the beginning of a case-insensitive regular match

  • !~And !~*are not case-sensitive and case-insensitive match does not match the regular

  • / General match, any requests are matched.

Match order for the case of multiple location configuration (reference from, yet the actual verification, try to know, not rigidly stick, for reference):

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.

Example, the following mapping rules:

LOCATION = / { 
   # Rule A 
} 
LOCATION = / Login { 
   # Rule B 
} 
LOCATION ^ ~ / static / { 
   # Rule C 
} 
LOCATION ~ \. (GIF | JPG | PNG | JS | CSS) $ { 
   # rule D 
} 
LOCATION * ~ \ .png $ { 
   # rule E 
} 
LOCATION! ~ \ .xhtml $ { 
   # rule F. 
} 
LOCATION! ~ * \ .xhtml $ { 
   # rule G 
} 
LOCATION / { 
   # rule 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 / register the matching rules H

Visit http: //localhost/static/a.html rule will match 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.

So the actual use, personally feel that there are at least three matching rules are defined as follows:

# Web site root directly matched by the domain name visit the Web site home page more frequently, using this will accelerate the process, the official website says. 
# Here is forwarded directly to the back-end application server, and may also be a static home 
# first mandatory rule 
LOCATION = / { 
    proxy_pass HTTP: // Tomcat: 8080 / index 
} 

# The second mandatory rules dealing with static requested file, which is a http server nginx strengths 
# has two configuration modes, matching or suffix match directory, optionally with the use of one or 
LOCATION ~ ^ / static / { 
    the root / Webroot / static /; 
} 
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 dynamically 
# non-static file requests the default is to request a dynamic, based on their own real grasp 
# after all, some of the current popular framework with .php, .jsp suffix rarely the case 

LOCATION / { 
    proxy_pass HTTP: // Tomcat: 8080 / 
}

The other configuration information describes nginx

Three, ReWrite grammar

last - basically use this Flag.
break - suspend Rewirte, do not continue to match
redirect - returns temporary redirect HTTP state 302
permanent - permanent redirect returned HTTP status 301

1, can be used to determine the following expressions:

-fAnd !-fused to determine whether there is a file
-dand !-dused to determine whether there is a directory
-e, and !-eused to determine whether there is a file or directory
-x, and !-xused to determine whether an executable file

2, the following can be used as a determination of global variables

例:http://localhost:88/test1/test2/test.php

$host:localhost
$server_port:88
$request_uri:http://localhost:88/test1/test2/test.php
$document_uri:/test1/test2/test.php
$document_root:D:\nginx/html
$request_filename:D:\nginx/html/test1/test2/test.php

Four, Redirect grammar

server {
    listen 80;
    server_name start.igrow.cn;
    index index.html index.php;
    root html;
    if ($http_host !~ "^star\.igrow\.cn$" {
        rewrite ^(.*) http://star.igrow.cn$1 redirect;
    }
}

Five, anti-hotlinking

location ~* \.(gif|jpg|swf)$ {
    valid_referers none blocked start.igrow.cn sta.igrow.cn;
    if ($invalid_referer) {
        rewrite ^/ http://$host/logo.png;
    }
}

Sixth, set an expiration time depending on the file type

location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
    if (-f $request_filename) {
        expires 1h;
        break;
    }
}

Seven, prohibit access to a directory

location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}

PS: Some of the available global variables

$args
$content_length
$content_type
$document_root
$document_uri
$host
$http_user_agent
$http_cookie
$limit_rate
$request_body_file
$request_method
$remote_addr
$remote_port
$remote_user
$request_filename
$request_uri
$query

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/11527438.html