nginx 下 location 某个文件夹下某类文件

来源:互联网 发布:手机淘宝与支付宝绑定 编辑:程序博客网 时间:2024/06/05 20:47

自己的解释:

~ ^/prefix/.*\.html$ 

解释:~ 表示后面跟的是正则,而且是区分大小写的( “~ ”区分大小写,“~* ”不区分大小写)。

^/prefix/.*\.html$  就是正则表达式了,^在正则里表示 以什么开始,/prefix/ 表示符合这个文件夹路径的。 ".*" 表示匹配单个字符多次。"\." 表示转义 "."  采用 "." 本身,而非他在正则里的意思(非\r\n的单个字符)。 $ 表示已什么结尾。

//自己参考写的配置:
         location ~ ^/test/.*\.(json|gif|jpg|png|jpeg|html)$ {

        root   /data/test/;
        autoindex off;
        }

转自:http://www.cnblogs.com/lidabo/p/4169396.html



配置 3.2

server {

       listen       9090;

       server_name  localhost;

 

      

       location ~ ^/prefix/.*\.html$ {

           deny all;  

       }  

                 

                  location ~ \.html$ {

           allow all; 

       } 

 

}

 

测试结果:

URI 请求配置 3.1配置 3.2curl http://localhost:9090/regextest.html404 Not Found404 Not Foundcurl http://localhost:9090/prefix/regextest.html404 Not Found403 Forbidden

 

解释:

Location ~ ^/prefix/.*\.html$ {deny all;} 表示正则 location 对于以 /prefix/ 开头, .html 结尾的所有 URI 请求,都拒绝访问;   location ~\.html${allow all;} 表示正则 location 对于以 .html 结尾的 URI 请求,都允许访问。 实际上,prefix 的是 ~\.html$ 的子集。

在“配置 3.1 ”下,两个请求都匹配上 location ~\.html$ {allow all;} ,并且停止后面的搜索,于是都允许访问, 404 Not Found ;在“配置 3.2 ”下, /regextest.html 无法匹配 prefix ,于是继续搜索 ~\.html$ ,允许访问,于是 404 Not Found ;然而 /prefix/regextest.html 匹配到 prefix ,于是 deny all , 403 Forbidden 。

 

0 0