Nginx Location Configuration Detailed

Source: Internet
Author: User
Tags tomcat
Syntax rules: location [=|~|~*|^~]/uri/{...} = begins with an exact match ^~ the beginning means that the URI 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, which can be matched to the rule ^~/static//aa (note is a space). ~ Start indicates a case-sensitive regular match ~* the beginning of the case-insensitive regular match!~ and!~* are case-insensitive and case-insensitive for regular/generic matches, and any requests will match. Multiple location configuration in the case of the matching order (reference, not actually verified, try to know, do not rigidly, for reference only): first match =, followed by matching ^~, followed by the order of the file in the regular match, and finally to the/General match. When a match succeeds, the match is stopped and the request is processed according to the current matching rule.

example, there are the following matching rules:

[plain] view plain Copy location =/{#规则A} location =/login {#规则B} location ^~/static/ {#规则C} location ~ \. (GIF|JPG|PNG|JS|CSS) $ {#规则D} location ~* \.png$ {#规则E} location!~ \.xhtml$ {#规则F} Locati On!~* \.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 will match rule b,http://localhost/register match 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 the first match to rule C Access http://localhost/a.PNG matches rule e without matching rule d because rule e is not case sensitive. 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. So in practice, there are usually at least three matching rule definitions, as follows:

[Plain]   View plain  copy #直接匹配网站根, access to the site through the domain name more frequent, 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    #  has two configuration modes, a directory match or a suffix match, either one or the other with     location ^~ /static/ {       root /webroot/static/;   }   location ~* \. (Gif|jpg|jpeg|png|css|js|ico) $ {       root /webroot/res/;  }        #第三个规则就是通用规则, used to forward dynamic requests to back-end application servers    #非静态文件请求就默认是动态请求, with their own grasp    # After all, some of the current frameworks are popular, with the. php,.jsp suffix rarely in the case    location / {       proxy_ pass http://tomcat:8080/  }  


The following part of the direct copy came, a little messy, can be used as a reference

Third, rewrite grammar
last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the HTTP status of a temporary REDIRECT 302
permanent– returns the HTTP status of a permanent REDIRECT 301 Note: Last and break are the biggest difference-break is to terminate the current location of the rewrite detection, and no longer place to match- Last is the rew that terminates the current location

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.