NGINX Configuration Location Summary and rewrite rule writing _nginx

Source: Internet
Author: User

Syntax rules: location [=|~|~*|^~]/uri/{...}
= The beginning indicates an exact match
The beginning of the ^~ indicates 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 and can be matched by the rule ^~/static//aa (note is a space).
~ starts with a case-sensitive regular match
~* begins with a case-insensitive regular match
!~ and!~*, respectively, are case insensitive and case-insensitive mismatches
/Universal match, any request will match to.
Multiple location configuration in the case of matching order for (resources, not actually validated, try to know, do not adhere to, for reference only):
First match =, second match ^~, followed by regular matching in file order, finally to/Universal match. When a match is successful, stop the match and process the request 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 to the root directory/, such as http://localhost/will match rule a
Access http://localhost/login match Rule b,http://localhost/register match rule H
Access to http://localhost/static/a.html will match rule C
Access to Http://localhost/a.gif, Http://localhost/b.jpg will match the rules D and rule e, but the rule D order takes precedence, and rule e does not work, and http://localhost/static/c.png Then match precedence to rule C
Access to Http://localhost/a.PNG matches rule e without matching rule d 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 is an exclusion, conforms to matching rules but does not match, so think about where the actual application will be used.
The access http://localhost/category/id/1111 is ultimately matched to rule H because none of the above rules match, and this should be the Nginx forwarding request to the backend application server, such as fastcgi (PHP), Tomcat (JSP), Nginx exists as a directional proxy server.


Therefore, in actual use, individuals feel that there are at least three matching rule definitions, as follows:
#直接匹配网站根, through the domain name to visit the home page more frequently, use this will speed up processing, said the official website.
#这里是直接转发给后端应用服务器了, can also be a static home page
# First Required rule
Location =/{
Proxy_pass Http://tomcat:8080/index
}
# The second required rule is to process the static file request, which is the strength of Nginx as an HTTP server
# There are two modes of configuration, directory matching or suffix matching, optional or in combination with
Location ^~/static/{
root/webroot/static/;
}
Location ~* \. (Gif|jpg|jpeg|png|css|js|ico) $ {
root/webroot/res/;
}
#第三个规则就是通用规则, for forwarding dynamic requests to the backend application server
#非静态文件请求就默认是动态请求, according to the actual grasp of their
#毕竟目前的一些框架的流行, with a. php,.jsp suffix is rare.
Location/{
Proxy_pass http://tomcat:8080/
}

Third, rewrite grammar
last– basically use this flag.
break– abort Rewirte, do not continue to match
redirect– returns the temporarily redirected HTTP status 302
permanent– returns HTTP status for permanent redirection 301
1. The following are the expressions that you can use to determine:
-F and!-f are used to determine whether a file exists
-D and!-d used to determine whether a directory exists
-E and!-e used to determine whether a file or directory exists
-X and!-x are used to determine whether a file is executable
2, the following is a global variable that can be used as a judgment
Example: 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$&quot {
Rewrite ^ (. *) http://star.igrow.cn$1 redirect;
}
}
Five, 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 expiration time based on 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;
}
Some of the global variables available:
$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 _string
$scheme
$server _protocol
$server _addr
$server _name
$server _port
$uri

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.