Location matching rules

Source: Internet
Author: User

Syntax rules  location [=|~|~*|^~]/uri/{...} Symbols     Meanings =    The beginning of an exact match ^~    the beginning of a URI that begins with a regular string and is understood to match the URL path. Nginx does not encode the URL, so the request is/STATIC/20%/AA, can be ^~ by the rule/static//aa match to (note is a space) ~    beginning to indicate a case-sensitive regular match ~*   The   starts with a case-insensitive regular match!~ and!~*    are case-insensitive and case-insensitive matching of regular/    generic matches, and any request will match. In the case of multiple location configurations, the matching order is (reference comes, not actually verified, try to know, do not rigidly, for reference only): first match = second match ^~ followed by the order of the file in the regular match finally is to give/generic match when there is a match success, stop matching, The example of processing the request according to the current matching rule has the following matching rules: location =/{   #规则A}location =/login {   #规则B}location ^~/static/{ &N Bsp #规则C}location ~ \. (GIF|JPG|PNG|JS|CSS) $ {   #规则D}location ~* \.png$ {   #规则E}location!~ \.xhtml$ {   #规则F} Location!~* \.xhtml$ {   #规则G}location/{   #规则H} The resulting effect is as follows: Access to the root directory/, such as http://localhost/will match rule a access Http://localhost/login matches the rule b,http://localhost/register then matches the rule H access http://localhost/static/a.html will match rule C to access/HTTP/ Localhost/a.gif, http://localhost/b. jpg will match rule D and rule e, but rule D order takes precedence, rule e does not work, and http://localhost/static/c.png matches to rule C Access http://localhost/a.PNG matches rule E, Rule d is not matched because rule e is case-insensitive. Access http://localhost/a.xhtml does not match rule f and rule g,http://localhost/a.xhtml does not match rule G because it is case insensitive. Rule f, rule g belongs to the exclusion method, conforms to the matching rule but does not match, so think about where the actual application will be used. Access to the http://localhost/category/id/1111 is finally matched to the rule H, because the above rules do not match, this time should be nginx forwarding request to the backend application server, such as fastcgi (PHP), Tomcat (JSP), Nginx as the direction proxy server exists. Therefore, in actual use, the individual feel that there are at least three matching rule definitions, as follows: #直接匹配网站根, through the domain name to visit the site home more frequently, using this will speed up processing, the official website said. #这里是直接转发给后端应用服务器了, can also be a static home # first required rule location =/{    Proxy_pass http://tomcat:8080/index}# The second required rule is to handle a static file request, which is Nginx's strength as an HTTP server # There are two configuration modes, a directory match or a suffix match, either one or the same with location ^~/static/{    root/ webroot/static/;} Location ~* \. (Gif|jpg|jpeg|png|css|js|ico) $ {    root/webroot/res/;} #第三个规则就是通用规则, used to forward a dynamic request to a backend application server # non-static file request is the default is dynamic request, own according to the actual grasp # after all, some of the current framework of the popular, with the. php,.jsp suffix is rare. Location/{     Proxy_pass http://tomcat:8080/}rewrite Syntax  last– basically use this flagbreak– to abort rewirte, not to continue matching redirect– Returns the HTTP status of a temporary redirect 302permanent– returns the HTTP status of permanent redirection 3011, the following is the expression that can be used to judge:-F and!-f used to determine if there is a file-D and!-d used to determine if there is a directory-E and!-e used to determine if there is a file or directory-X and! X is used to determine if a file is executable 2, the following are examples of global variables that can be used as judgments: 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.phpredirect syntax  server {    listen 80;     server_name start.igrow.cn;    index index.html index.php;    root html;     if ($http _host!~ "^star\.igrow\.cn$&quot {        rewrite ^ (. *) http://star.igrow.cn$1 redirect;   }} Anti-theft chain  location ~* \. (gif|jpg|swf) $ {    valid_referers None blocked start.igrow.cn sta.igrow.cn;    if ($ invalid_referer) {       rewrite ^//http $host/logo.png;   }} Set the expiration time according to the file type  location ~* \.(js|css|jpg|jpeg|gif|png|swf) $ {    if (-F $request _filename) {         Expires 1h;        break;   }} Disable access to a directory  location ~* \. (txt|doc) ${    root/data/www/wwwroot/linuxtone/test;    deny all;}

Location matching rules

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.