Nginx Location配置总结

来源:互联网 发布:可乐谢安琪歌词知乎 编辑:程序博客网 时间:2024/06/14 04:52
语法规则: location [=|~|~*|^~] /uri/ { … }

= 开头表示精确匹配

^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。

~ 开头表示区分大小写的正则匹配

~*  开头表示不区分大小写的正则匹配

!~和!~*分别为区分大小写不匹配及不区分大小写不匹配 的正则

/ 通用匹配,任何请求都会匹配到。

多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

首先匹配 =,其次匹配^~, 其次是按文件中顺序的正则匹配,最后是交给 / 通用匹配。当有匹配成功时候,停止匹配,按当前匹配规则处理请求。

例子,有如下匹配规则:
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
}
那么产生的效果如下:
访问根目录/, 比如http://localhost/ 将匹配规则A


访问 http://localhost/login 将匹配规则B,http://localhost/register 则匹配规则H


访问 http://localhost/static/a.html 将匹配规则C


访问 http://localhost/a.gif, http://localhost/b.jpg 将匹配规则D和规则E,但是规则D顺序优先,规则E不起作用,而 http://localhost/static/c.png 则优先匹配到规则C

访问 http://localhost/a.PNG 则匹配规则E,而不会匹配规则D,因为规则E不区分大小写。

访问 http://localhost/a.xhtml 不会匹配规则F和规则G,http://localhost/a.XHTML不会匹配规则G,因为不区分大小写。规则F,规则G属于排除法,符合匹配规则但是不会匹配到,所以想想看实际应用中哪里会用到。

访问 http://localhost/category/id/1111 则最终匹配到规则H,因为以上规则都不匹配,这个时候应该是nginx转发请求给后端应用服务器,比如FastCGI(php),tomcat(jsp),nginx作为方向代理服务器存在。

所以实际使用中,个人觉得至少有三个匹配规则定义,如下:
#直接匹配网站根,通过域名访问网站首页比较频繁,使用这个会加速处理,官网如是说。
#这里是直接转发给后端应用服务器了,也可以是一个静态首页
# 第一个必选规则
location = / {
    proxy_pass http://tomcat:8080/index
}
# 第二个必选规则是处理静态文件请求,这是nginx作为http服务器的强项
# 有两种配置模式,目录匹配或后缀匹配,任选其一或搭配使用
location ^~ /static/ {
    root /webroot/static/;
}


location ~* \.(gif|jpg|jpeg|png|css|js|ico)$ {
    root /webroot/res/;
}
#第三个规则就是通用规则,用来转发动态请求到后端应用服务器
#非静态文件请求就默认是动态请求,自己根据实际把握
#毕竟目前的一些框架的流行,带.php,.jsp后缀的情况很少了
location / {
    proxy_pass http://tomcat:8080/
}


三、ReWrite语法
last – 基本上都用这个Flag。
break – 中止Rewirte,不在继续匹配
redirect – 返回临时重定向的HTTP状态302
permanent – 返回永久重定向的HTTP状态301
1、下面是可以用来判断的表达式:
-f和!-f用来判断是否存在文件
-d和!-d用来判断是否存在目录
-e和!-e用来判断是否存在文件或目录
-x和!-x用来判断文件是否可执行


2、下面是可以用作判断的全局变量
例: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


四、Redirect语法
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;
}
}
五、防盗链
location ~* \.(gif|jpg|swf)$ {
valid_referers none blocked start.igrow.cn sta.igrow.cn;
if ($invalid_referer) {
rewrite ^/ http://$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;
}
一些可用的全局变量:
$args        1.0.8        请求中的参数;

$binary_remote_addr        1.0.8        远程地址的二进制表示

$body_bytes_sent        1.0.8        已发送的消息体字节数

$content_length        1.0.8        HTTP请求信息里的"Content-Length";

$content_type        1.0.8        请求信息里的"Content-Type";

$document_root        1.0.8        针对当前请求的根路径设置值;

$document_uri        1.0.8        与$uri相同; 比如 /test1/test2/test.php

$host        1.0.8        请求信息中的"Host",如果请求中没有Host行,则等于设置的服务器名;

$hostname        1.0.8       

$http_cookie        1.0.8        cookie 信息

$http_post        1.0.8       

$http_referer        1.0.8        引用地址

$http_user_agent        1.0.8        客户端代理信息

$http_via        1.0.8        最后一个访问服务器的Ip地址。http://www.cnblogs.com/deng02/archive/2009/02/11/1387911.html

$http_x_forwarded_for        1.0.8        相当于网络访问路径。http://www.cnblogs.com/craig/archive/2008/11/18/1335809.html

$is_args        1.0.8       

$limit_rate        1.0.8        对连接速率的限制;

$nginx_version        1.0.8       

$pid        1.0.8       

$query_string        1.0.8        与$args相同;

$realpath_root        1.0.8       

$remote_addr        1.0.8        客户端地址;

$remote_port        1.0.8        客户端端口号;

$remote_user        1.0.8        客户端用户名,认证用;

$request        1.0.8        用户请求

$request_body        1.0.8       

$request_body_file        1.0.8        发往后端的本地文件名称

$request_completion        1.0.8       

$request_filename        1.0.8        当前请求的文件路径名,比如$request_filename:D:\nginx/html/test1/test2/test.php

$request_method        1.0.8        请求的方法,比如"GET"、"POST"等;

$request_uri        1.0.8        请求的URI,带参数; 比如http://localhost:88/test1/test2/test.php

$scheme        1.0.8        所用的协议,比如http或者是https,比如rewrite^(.+)$$scheme://example.com$1redirect;

$sent_http_cache_control        1.0.8       

$sent_http_connection        1.0.8       

$sent_http_content_length        1.0.8       

$sent_http_content_type        1.0.8       

$sent_http_keep_alive        1.0.8        

$sent_http_last_modified        1.0.8       

$sent_http_location        1.0.8       

$sent_http_transfer_encoding        1.0.8       

$server_addr        1.0.8        服务器地址,如果没有用listen指明服务器地址,使用这个变量将发起一次系统调用以取得地址(造成资源浪费);

$server_name        1.0.8        请求到达的服务器名;

$server_port        1.0.8        请求到达的服务器端口号;

$server_protocol        1.0.8        请求的协议版本,"HTTP/1.0"或"HTTP/1.1";

$uri        1.0.8        请求的URI,可能和最初的值有不同,比如经过重定向之类的。


来源地址:http://blog.sina.com.cn/s/blog_97688f8e0100zws5.html

0 0
原创粉丝点击