语法规则: location [=|~|~*|^~] /uri/ { … }
= 开头表示精确匹配
<wbr></wbr>
^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
<wbr></wbr>
~ 开头表示区分大小写的正则匹配
<wbr></wbr>
~* <wbr>开头表示不区分大小写的正则匹配</wbr>
<wbr></wbr>
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则
<wbr></wbr>
/ 通用匹配,任何请求都会匹配到。
<wbr></wbr>
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):
<wbr></wbr>
首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。
<wbr></wbr>
例子,有如下匹配规则:
<wbr></wbr>
<wbr></wbr>
location = / {
<wbr><wbr>#规则A</wbr></wbr>
}
location = /login {
<wbr><wbr>#规则B</wbr></wbr>
}
location ^~ /static/ {
<wbr><wbr>#规则C</wbr></wbr>
}
location ~ \.(gif|jpg|png|js|css)$ {
<wbr><wbr>#规则D</wbr></wbr>
}
location ~* \.png$ {
<wbr><wbr>#规则E</wbr></wbr>
}
location !~ \.xhtml$ {
<wbr><wbr>#规则F</wbr></wbr>
}
location !~* \.xhtml$ {
<wbr><wbr>#规则G</wbr></wbr>
}
location / {
<wbr><wbr>#规则H</wbr></wbr>
}
那么产生的效果如下:
<wbr></wbr>
访问根目录/, 比如http://localhost/<wbr>将匹配规则A</wbr>
<wbr></wbr>
访问<wbr><a href="http://localhost/login" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/login</a><wbr>将匹配规则B,<a href="http://localhost/register" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/register</a><wbr>则匹配规则H</wbr></wbr></wbr>
<wbr></wbr>
访问<wbr><a href="http://localhost/static/a.html" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/static/a.html</a><wbr>将匹配规则C</wbr></wbr>
<wbr></wbr>
访问<wbr><a href="http://localhost/a.gif" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/a.gif</a>,<wbr><a href="http://localhost/b.jpg" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/b.jpg</a><wbr>将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用, 而<wbr><a href="http://localhost/static/c.png" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/static/c.png</a><wbr>则优先匹配到 规则C</wbr></wbr></wbr></wbr></wbr>
<wbr></wbr>
访问<wbr><a href="http://localhost/a.PNG" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/a.PNG</a><wbr>则匹配规则E, 而不会匹配规则D,因为规则E不区分大小写。</wbr></wbr>
<wbr></wbr>
访问<wbr><a href="http://localhost/a.xhtml" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/a.xhtml</a><wbr>不会匹配规则F和规则G,<a href="http://localhost/a.XHTML%E4%B8%8D%E4%BC%9A%E5%8C%B9%E9%85%8D%E8%A7%84%E5%88%99G%EF%BC%8C%E5%9B%A0%E4%B8%BA%E4%B8%8D%E5%8C%BA%E5%88%86%E5%A4%A7%E5%B0%8F%E5%86%99%E3%80%82%E8%A7%84%E5%88%99F%EF%BC%8C%E8%A7%84%E5%88%99G%E5%B1%9E%E4%BA%8E%E6%8E%92%E9%99%A4%E6%B3%95%EF%BC%8C%E7%AC%A6%E5%90%88%E5%8C%B9%E9%85%8D%E8%A7%84%E5%88%99%E4%BD%86%E6%98%AF%E4%B8%8D%E4%BC%9A%E5%8C%B9%E9%85%8D%E5%88%B0%EF%BC%8C%E6%89%80%E4%BB%A5%E6%83%B3%E6%83%B3%E7%9C%8B%E5%AE%9E%E9%99%85%E5%BA%94%E7%94%A8%E4%B8%AD%E5%93%AA%E9%87%8C%E4%BC%9A%E7%94%A8%E5%88%B0%E3%80%82" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。</a></wbr></wbr>
<wbr></wbr>
访问<wbr><a href="http://localhost/category/id/1111" style="text-decoration:initial; color:rgb(62,115,160)">http://localhost/category/id/1111</a><wbr>则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。</wbr></wbr>
<wbr></wbr>
所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
<wbr></wbr>
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
<wbr><wbr>proxy_pass<wbr><a href="http://tomcat:8080/index" style="text-decoration:initial; color:rgb(62,115,160)">http://tomcat:8080/index</a></wbr></wbr></wbr>
}
<wbr></wbr>
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
<wbr><wbr>root /webroot/static/;</wbr></wbr>
}
location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
<wbr><wbr>root /webroot/res/;</wbr></wbr>
}
<wbr></wbr>
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
<wbr><wbr>proxy_pass<wbr><a href="http://tomcat:8080/" style="text-decoration:initial; color:rgb(62,115,160)">http://tomcat:8080/</a></wbr></wbr></wbr>
}
<wbr></wbr>
<wbr></wbr>
未试验过的其他信息:
三、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: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
四、Redirect语法
server {
listen 80;
server_name start.igrow.cn;
index index.html index.php;
root html;
if ($http_host !~ “^star\.igrow\.cn$" {
rewrite ^(.*)<wbr><a href="http://star.igrow.cn%241/" style="text-decoration:initial; color:rgb(62,115,160)">http://star.igrow.cn$1</a><wbr>redirect;</wbr></wbr>
}
}
五、防盗链location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/<wbr><a href="http://%24host/logo.png;" style="text-decoration:initial; color:rgb(62,115,160)">http://$host/logo.png;</a></wbr>
}
}
六、根据文件类型设置过期时间
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;
}
<wbr></wbr>
<wbr></wbr>
<wbr></wbr>
++ 一些可用的全局变量
$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