IP允许子目录访问NGINX

IP允许子目录访问NGINX,nginx,Nginx,我实际上使用nginx作为我的Web服务器,我试图使用以下方法拒绝所有子目录访问: location / { root /usr/share/nginx/html/project; index index.html index.htm index.php; } location ~ ^/subdir/ { allow 192.168.1.0/24; deny all; try_files $uri $uri/ /index_subdir.

我实际上使用nginx作为我的Web服务器,我试图使用以下方法拒绝所有子目录访问:

location / {
    root   /usr/share/nginx/html/project;
    index  index.html index.htm index.php;
}



location ~ ^/subdir/ { 
    allow 192.168.1.0/24; 
    deny all; 
    try_files $uri $uri/ /index_subdir.php?$query_string;
}
但是nginx试图找出项目文件夹中的index_subdir.php

我希望你能帮我一把


亲切的问候

您可能需要做一些更改:

server {
    root   /usr/share/nginx/html/project;
    index  index.html index.htm index.php;

    location / {
    }
    location ~ \.php$ {
        ...
    }

    location ^~ /subdir { 
        allow 192.168.1.0/24; 
        deny all; 

        try_files $uri $uri/ /subdir/index_subdir.php?$query_string;

        location ~ \.php$ {
            ...
        }
    }
}
通常,
索引
指令放置在
服务器
块级别,以便所有
位置
块继承相同的值。有关详细信息,请参阅

我假设您有一个
location~\.php$
块,该块当前执行所有php脚本

我的示例使用带有
^ ~
修饰符的前缀位置,而不是正则表达式位置。修改器使其优先于
服务器
块中的任何位置。有关详细信息,请参阅

try\u files
指令的最后一个元素是URI,这意味着如果您特别希望文件位于该文件夹中,它必须包含
/subdir
前缀。有关详细信息,请参阅


您需要在
location^~/subdir
块中复制另一个
location~\.php$
,以便php脚本受到相同访问规则的保护。

您好Richard Smith,我已经按照您的建议应用了更改,并且效果良好!!!。非常感谢你的帮助。亲切的问候!!!