微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

nginx,从位置排除单个文件

如何从某个位置排除单个文件?以下块负责全局PHP处理:

location ~ \.PHP$ {
    fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
    fastcgi_pass   unix:/var/run/PHP5-fpm.sock;
    fastcgi_read_timeout 150;
    fastcgi_index index.PHP;
    include fastcgi_params;
}

我想在这里做的是排除一个名为piwik.PHP文件,因为它应该在一个单独的位置接受特殊处理.所以我的目标是让它看起来像这样

location ~ \.PHP$&& NOT /stats/piwik.PHP  {
    ...
}

怎么能实现这一目标?

解决方法:

当你看到答案时,你会踢自己.

What I want to do here is to exclude one single file called piwik.PHP as it should receive special treatment in a separate location.

好的,您应该将该路径设置为认路径之前的单独位置.例如

location ~ ^/stats/piwik.PHP${
    allow 127.0.0.1;
    deny all;

    fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
    fastcgi_pass   unix:/var/run/PHP5-fpm.sock;
    fastcgi_read_timeout 150;
    fastcgi_index index.PHP;
    include fastcgi_params;
}

location ~ \.PHP$ {
    fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
    fastcgi_pass   unix:/var/run/PHP5-fpm.sock;
    fastcgi_read_timeout 150;
    fastcgi_index index.PHP;
    include fastcgi_params;
}

因为它们都是正则表达式位置块,所以在匹配的Nginx conf中首先列出的块将具有优先级.

但是,您可能应该保护整个目录.使用匹配的前缀位置规则可以更轻松地完成此操作:

location ^~ /stats/ {
    allow 127.0.0.1;
    deny all;

    fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
    fastcgi_pass   unix:/var/run/PHP5-fpm.sock;
    fastcgi_read_timeout 150;
    fastcgi_index index.PHP;
    include fastcgi_params;
}

location ~ \.PHP$ {
    fastcgi_split_path_info ^(.+\.PHP)(/.+)$;
    fastcgi_pass   unix:/var/run/PHP5-fpm.sock;
    fastcgi_read_timeout 150;
    fastcgi_index index.PHP;
    include fastcgi_params;
}

因为匹配前缀具有正则表达式匹配的更高优先级,所以它们在您的Nginx conf中的顺序无关紧要.对priority for matches is here一个很好的解释

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐