nginxlocation语法使⽤介绍
nginx location介绍
Nginx 中的 Location 指令是NginxHttpCoreModule中重要指令。Location 指令,是⽤来为匹配的 URI 进⾏配置,URI 即语法中的”/uri/”,可以是字符串或正则表达式。但如果要使⽤正则表达式,则必须指定前缀。
nginx location语法
基本语法:location [=|~|~*|^~] /uri/ { … }
= 严格匹配。如果这个查询匹配,那么将停⽌搜索并⽴即处理此请求。
~ 为区分⼤⼩写匹配(可⽤正则表达式)
~* 为不区分⼤⼩写匹配(可⽤正则表达式)
!~和!~*分别为区分⼤⼩写不匹配及不区分⼤⼩写不匹配
^~ 如果把这个前缀⽤于⼀个常规字符串,那么告诉nginx 如果路径匹配那么不测试正则表达式。
nginx location应⽤实例
location = / {
# 只匹配 / 查询。
}
location / {
# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
}
location ^~ /images/ {
# 匹配任何已 /images/ 开头的任何查询并且停⽌搜索。任何正则表达式将不会被测试。
}
location ~* \.(gif|jpg|jpeg)$ {
# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。
}
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow sta.igrow;
if ($invalid_referer) {
#防盗链
rewrite ^/ $host/logo.png;
}
}
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
#根据⽂件类型设置过期时间
expires  1h;
break;
}
}
location ~* \.(txt|doc)${
#禁⽌访问某个⽬录
root /data/www/wwwroot/linuxtone/test;
deny all;
}
以下是补充:
Nginx Location基本语法
location
syntax: location [=|~|~*|^~] /uri/ { … }
语法:location [=|~|~*|^~] /uri/ { … }
default: no
默认:否
context: server
上下⽂:server
This directive allows different configurations depending on the URI. It can be configured using both conventional strings and regular expressions. To use regular expressions, you must use the prefix ~* for case insensitive match and ~ for case sensitive match.
这个指令随URL不同⽽接受不同的结构。你可以配置使⽤常规字符串和正则表达式。如果使⽤正则表
达式,你必须使⽤ ~* 前缀选择不区分⼤⼩写的匹配或者 ~ 选择区分⼤⼩写的匹配。
To determine which location directive matches a particular query, the conventional strings are checked first. Conventional strings match the beginning portion of the query and are case-sensitive - the most specific match will be used (see below on how nginx determines this). Afterwards, regular expressions are checked in the order defined in the configuration file. The first regular expression to match the query will stop the search. If no regular expression matches are found, the result from the convention string search is used.
确定哪个location 指令匹配⼀个特定指令,常规字符串第⼀个测试。常规字符串匹配请求的开始部分并且区分⼤⼩写,最明确的匹配将会被使⽤(查看下⽂明⽩ nginx 怎么确定它)。然后正则表达式按照配置⽂件⾥的顺序测试。到第⼀个⽐配的正则表达式将停⽌搜索。如果没有到匹配的正则表达式,使⽤常规字符串的结果。
There are two ways to modify this behavior. The first is to use the prefix “=”, which matches an exact query only. If the query matches, then searching stops and the request is handled immediately. For example, if the request “/” occurs frequently, then using “location = /” will expedite the processing of this request.
有两个⽅法修改这个⾏为。第⼀个⽅法是使⽤ “=”前缀,将只执⾏严格匹配。如果这个查询匹配,那么将停⽌搜索并⽴即处理这个请求。例⼦:如果经常发⽣”/”请求,那么使⽤ “location = /” 将加速处理这个请求。
The second is to use the prefix ^~. This prefix is used with a conventional string and tells nginx to not check regular expressions if the path provided is a match. For instance, “location ^~ /images/” would halt searching if the query begins with /images/ - all regular expression directives would not be checked.
第⼆个是使⽤ ^~ 前缀。如果把这个前缀⽤于⼀个常规字符串那么告诉nginx 如果路径匹配那么不测试正则表达式。
Furthermore it is important to know that NGINX does the comparison not URL encoded, so if you have a URL like
“/images/%20/test” then use “/images/ /test” to determine the location.
⽽且它重要在于 NGINX 做⽐较没有 URL 编码,所以如果你有⼀个 URL 链接'/images/%20/test' , 那么使⽤ “images/ /test” 限定location。
To summarize, the order in which directives are checked is as follows:
总结,指令按下列顺序被接受:
1. Directives with the = prefix that match the query exactly. If found, searching stops.
1. = 前缀的指令严格匹配这个查询。如果到,停⽌搜索。
2. All remaining directives with conventional strings, longest match first. If this match used the ^~ prefix, searching stops.
2. 剩下的常规字符串,长的在前。如果这个匹配使⽤ ^~ 前缀,搜索停⽌。
3. Regular expressions, in order of definition in the configuration file.
3. 正则表达式,按配置⽂件⾥的顺序。
4. If #3 yielded a match, that result is used. Else the match from #2 is used.
4. 如果第三步产⽣匹配,则使⽤这个结果。否则使⽤第⼆步的匹配结果。
Example:
例⼦:
location = / {
# matches the query / only.
# 只匹配 / 查询。
[ configuration A ]
}
location / {
# matches any query, since all queries begin with /, but regular
# expressions and any longer conventional blocks will be
# matched first.
# 匹配任何查询,因为所有请求都已 / 开头。但是正则表达式规则和长的块规则将被优先和查询匹配。
[ configuration B ]
}
location ^~ /images/ {
# matches any query beginning with /images/ and halts searching,
# so regular expressions will not be checked.
# 匹配任何已 /images/ 开头的任何查询并且停⽌搜索。任何正则表达式将不会被测试。
[ configuration C ]
}
location ~* ".(gif|jpg|jpeg)$ {
# matches any request ending in gif, jpg, or jpeg. However, all
# requests to the /images/ directory will be handled by
# Configuration C.
# 匹配任何已 gif、jpg 或 jpeg 结尾的请求。然⽽所有 /images/ ⽬录的请求将使⽤ Configuration C。
[ configuration D ]
}
Example requests:
例⼦请求:
*
/ -> configuration A
*
/documents/document.html -> configuration B
*
/
images/1.gif -> configuration C
*
/documents/1.jpg -> configuration D
Note that you could define these 4 configurations in any order and the results would remain the same.
注意:按任意顺序定义这4个配置结果将仍然⼀样。
Nginx Location 语法,与简单配置
⼀、介绍Nginx是俄罗斯⼈编写的⼗分轻量级的HTTP服务器,Nginx,它的发⾳为“engine X”,是⼀个⾼性能的HTTP和反向代理服务器,同时也是⼀个IMAP/POP3/SMTP 代理服务器.
⼆、Location语法语法:location [=|~|~*|^~] /uri/ { … }
注:
1、~  为区分⼤⼩写匹配
2、~* 为不区分⼤⼩写匹配
3、!~和!~*分别为区分⼤⼩写不匹配及不区分⼤⼩写不匹配
⽰例⼀:
location  / { }
匹配任何查询,因为所有请求都以 / 开头。但是正则表达式规则将被优先和查询匹配。
⽰例⼆:
location =/ {}
仅仅匹配/
⽰例三:
location ~* \.(gif|jpg|jpeg)$ {
rewrite \.(gif|jpg)$ /logo.png;
注:不区分⼤⼩写匹配任何以gif,jpg,jpeg结尾的⽂件
三、ReWrite语法
last - 基本上都⽤这个Flag。
break - 中⽌Rewirte,不在继续匹配
redirect - 返回临时重定向的HTTP状态302
permanent - 返回永久重定向的HTTP状态301
1、下⾯是可以⽤来判断的表达式:
-f和!-f⽤来判断是否存在⽂件
-d和!-d⽤来判断是否存在⽬录
-e和!-e⽤来判断是否存在⽂件或⽬录
-
x和!-x⽤来判断⽂件是否可执⾏
2、下⾯是可以⽤作判断的全局变量
复制代码代码如下:
$host:localhost
$server_port:88
$request_uri: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 四、Redirect语法
复制代码代码如下:
server {
listen 80;
server_name start.igrow;
index index.html index.php;
root html;
if ($http_host !~ "^star\.igrow\$"  {
rewrite ^(.*) star.igrow$1 redirect;
}
}
五、防盗链
复制代码代码如下:
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow sta.igrow;  if ($invalid_referer) {
rewrite ^/ $host/logo.png;
nginx 配置文件}
}
六、根据⽂件类型设置过期时间
复制代码代码如下:
location ~* \.(js|css|jpg|jpeg|gif|png|swf)$ {
if (-f $request_filename) {
expires    1h;
break;
}
}
七、禁⽌访问某个⽬录
复制代码代码如下:
location ~* \.(txt|doc)${
root /data/www/wwwroot/linuxtone/test;
deny all;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。