阻止隐藏的(点)php 文件

Far*_* Rn 2 nginx

我想阻止访问隐藏.dot文件,特别php是 Nginx 中的文件,但我找不到解决方案。
.ht文件默认是隐藏的,具体如下:

location ~ \.ht {
        deny all;
}
Run Code Online (Sandbox Code Playgroud)

我写了这个规则来匹配所有以 开头的文件,dot但它不起作用:

location \.(.+\.*)(.*)\.(.+\.*)(.*)$ {
        deny all;
}
Run Code Online (Sandbox Code Playgroud)

以下是一些应该隐藏的文件示例

/.foo.php
/path/to/.foo.php
Run Code Online (Sandbox Code Playgroud)

我该怎么办?

Ric*_*ith 5

您需要仔细查看location块的顺序。这个位置:

location ~ /\. {
    deny all;
}
Run Code Online (Sandbox Code Playgroud)

应该匹配任何路径元素以..

但是,它需要放置任何其他正则表达式位置块之上。此外,任何使用^~修饰符的前缀位置块都将优先。

有关详细信息,请参阅此文档

另请注意:您问题中的位置块都是错误的。第一个也将匹配index.html,第二个缺少~正则表达式位置的修饰符。