Nginx location base configuration and location best practices

Source: Internet
Author: User
Turn http://www.tuicool.com/articles/Jr63qy

Syntax rules: location [=|~|~*|^~]/uri/{...}
= Start with exact match
^~ begins by indicating 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
~* start indicates a case-insensitive regular match
!~ and!~* are case-insensitive and match-insensitive.
/generic match, any request will be matched to.
Multiple location configuration in the case of the matching order (reference, not actually verified, try to know, do not have to rigidly, for reference only):
First match =, next match ^~, followed by the regular match in the order of the file, 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:
Location =/{
#规则A
}
Location =/login {
#规则B
}
Location ^~/static/{
#规则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 root directory/, e.g. http://localhost/will match rule a
Access Http://localhost/login will match rule b,http://localhost/register then match rule h
Access http://localhost/static/a.html will match rule C
Accessing Http://localhost/a.gif, Http://localhost/b.jpg will match rule D and rule e, but rule D order takes precedence and rule e does not work, and http://localhost/static/c.png First match to rule C
Access to 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.
Several rules are commonly used in practical applications, 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 page
# 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, directory matching or suffix matching, either one or use
Location ^~/static/{
# request/static/a.txt will be mapped to the actual directory file:/webroot/res/static/a.txt
root/webroot/res/;
}
Location ~* \. (Gif|jpg|jpeg|png|css|js|ico) $ {
root/webroot/res/;
}

#第三个规则就是通用规则, used to forward dynamic requests to backend application servers
#非静态文件请求就默认是动态请求, according to the actual grasp
#毕竟目前的一些框架的流行, with a. php,.jsp suffix is rare.
Location/{
Proxy_pass http://tomcat:8080/
}
For the above basic recommendation configuration, there is a supplement, that is, about the forwarding has a little to note. For example, the following configuration, forwarding to a directory:
The key is the last/, Access localhost/outer/in.html, where case A is forwarded to tomcat:8080/in.html, and case B is forwarded to tomcat:8080/outer/in.html, so be sure to pay attention.

The above describes the Nginx location base configuration and location best practices, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • 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.