语法规则: location [=|~|~*|^~] /uri/ { … }
= 表示精确匹配,这个优先级也是最高的
^~ 表示uri以某个常规字符串开头,理解为匹配 url路径即可。nginx不对url做编码,因此请求为/static/20%/aa,可以被规则^~ /static/ /aa匹配到(注意是空格)。
~  表示区分大小写的正则匹配
~* 表示不区分大小写的正则匹配(和上面的唯一区别就是大小写)
!~和!~*分别为区分大小写不匹配及不区分大小写不匹配的正则
/ 通用匹配,任何请求都会匹配到,默认匹配.

下面讲讲这些语法的一些规则和优先级
多个location配置的情况下匹配顺序为(参考资料而来,还未实际验证,试试就知道了,不必拘泥,仅供参考):

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

例子,有如下匹配规则:

location / {
echo "/"; //需要安装echo模块才行,这边大家可以改成各自的规则
}
location = / {
echo "=/";
}
location = /nginx {
echo "=/nginx";
}
location ~ \.(gif|jpg|png|js|css)$ {
echo "small-gif/jpg/png";
}
location ~* \.png$ {
echo "all-png";
}
location ^~ /static/ {
echo "static";
}

以下是各种的访问情况
访问http://a.losbyda.com/.因为/是完全匹配的
如下:

[root@dep5 conf] curl http://a.losbyday.com/
# =/

访问http://a.losbyday.com/nginx,因为完全匹配了"=/nginx"

[root@dep5 conf] curl http://a.losbyday.com/nginx
#=/nginx

访问http://a.losbyday.com/nginx,从第一个开始尝试匹配,最后匹配到了~* \.png$ .

[root@dep5 conf] curl http://a.losbyday.com/xxx/1111.PNG (注意,这是大写)
all-png   

访问http://a.losbyday.com/static/1111.png,虽然static放在最后面,但是因为有^的缘故,他是最匹配的.

[root@dep5 conf] curl http://a.losbyday.com/static/1111.png
static

最后给出我们先上环境的静态文件的匹配规则
location ~* .*\.(js|css)?$
{
        expires 7d; //7天过期
        access_log off; //不保存日志
}
location ~* .*\.(png|jpg|gif|jpeg|bmp|ico)?$
{        
        expires 7d;
        access_log off;
}
location ~* .*\.(zip|rar|exe|msi|iso|gho|mp3|rmvb|mp4|wma|wmv|rm)?$
{
        deny all; //禁止这些文件下载,可以根据自己的环境来配置
}

nginx location配置(URL)的更多相关文章

  1. Nginx Location配置总结

    Nginx Location配置总结 语法规则: location [=|~|~*|^~] /uri/ { - }= 开头表示精确匹配^~ 开头表示uri以某个常规字符串开头,理解为匹配 url路径即 ...

  2. Nginx location配置详细解释

    nginx location配置详细解释 语法规则: location [=|~|~*|^~] /uri/ { - } = 开头表示精确匹配 ^~ 开头表示uri以某个常规字符串开头,理解为匹配 ur ...

  3. Nginx – rewrite 配置 URL重写及301跳转原理图

    Nginx – rewrite 配置 URL重写 官网:http://nginx.org/en/docs/http/ngx_http_rewrite_module.html 语法:rewrite re ...

  4. [转帖]nginx location配置详细解释

    nginx location配置详细解释 http://outofmemory.cn/code-snippet/742/nginx-location-configuration-xiangxi-exp ...

  5. nginx location配置

    nginx location配置   location在nginx中起着重要作用,对nginx接收到的请求字符串进行处理,如地址定向.数据缓存.应答控制.代理转发等location语法location ...

  6. nginx location 配置详解 【转载,整理】

    http://www.nginx.cn/115.html NGINX location 配置参考:http://www.cnblogs.com/zlingh/p/6288994.html https: ...

  7. Nginx location 配置用法及正则例子

    Nginx location 配置语法     1. location [ = | ~ | ~* | ^~ ] uri { ... }     2. location @name { ... }   ...

  8. Nginx Location配置语法介绍、优先级说明

    nginx 语法规则:location   [=|~|~*|^~|!~|!~*]    /uri/   { … } location匹配的是$document_uri,$document_uri 会随 ...

  9. Nginx Location配置总结及基础最佳实践

    参考来源: http://blog.zol.com.cn/1067/article_1066186.html,http://flandycheng.blog.51cto.com/855176/2801 ...

随机推荐

  1. 转:12C CDB and pdb with sql developer

    How to install the 12c DB and use the Pluggable DB with SQL DeveloperGoal To give a path to install ...

  2. linux sort 用法

    sort命令是帮我们依据不同的数据类型进行排序,其语法及常用参数格式: sort [-bcfMnrtk][源文件][-o 输出文件]补充说明:sort可针对文本文件的内容,以行为单位来排序. 参 数: ...

  3. 【二分图】 poj 1466

    #include <iostream> #include <memory.h> #include <cstdio> using namespace std; int ...

  4. c语言scanf详解

    函数名: scanf 功 能: 执行格式化输入 用 法: int scanf(char *format[,argument,...]);scanf()函数是通用终端格式化输入函数,它从标准输入设备(键 ...

  5. ubuntu服务器移植步骤

    1.安装LAMP套件 1 tasksel 2.安装FTP工具 http://www.cnblogs.com/esin/p/3483646.html 3.安装PHPMyAdmin 1)安装 1 apt- ...

  6. 转:Jmeter--google plugin插件监控被测系统资源方法

    一.插件准备 1.插件下载地址 http://jmeter-plugins.org/downloads/all/ 以下有两个版本的,1.1.2和1.1.3,注意Jmeter版本 1.1.2支持Jmet ...

  7. 转:Loadrunner学习知多少--脚本录制下载操作

    在很多时候我们可能需要对系统进行这样的脚本开发,模拟用户点击一个下载链接,然后弹出下载框,选择保存,用来测试在大量用户下载时服务器的性能.但是现在大家对于这种脚本的处理方式往往是通过关联和C 语言的文 ...

  8. boost之词法解析器spirit

    摘要:解析器就是编译原理中的语言的词法分析器,可以按照文法规则提取字符或者单词.功能:接受扫描器的输入,并根据语法规则对输入流进行匹配,匹配成功后执行语义动作,进行输入数据的处理. C++ 程序员需要 ...

  9. 区间的关系的计数 HDU 4638 离线+树状数组

    题目大意:给你n个人,每个人都有一个id,有m个询问,每次询问一个区间[l,r],问该区间内部有多少的id是连续的(单独的也算是一个) 思路:做了那么多离线+树状数组的题目,感觉这种东西就是一个模板了 ...

  10. android 权限管理和签名 实现静默卸载

    为了实现静默卸载, 学了下android的安全体系,记录如下 最近在做个东西,巧合碰到了sharedUserId的问题,所以收集了一些资料,存存档备份. 安装在设备中的每一个apk文件,Android ...