阻止访问目录中的文件但允许index.html

问题描述 投票:4回答:4

我正在/ test /上托管一个网站,但如果用户知道文件名,可以通过访问网址来访问文件。例如:

domain.com/test/readmesample.txt

我有它像上面设置但现在当我去domain.com/test index.html文件不会加载,我得到403禁止。

如何进行设置,以便在进行/测试时允许加载html文件,同时仍然阻止该目录中的文件?这包括index.html以外的文件,文件夹和.files。

location ~ /test {
             deny all;
}

这是我的配置文件

server {
listen 80;
listen 443 ssl default_server;

root /config/www;
index index.html index.htm index.php;

server_name www.domain.com;

ssl_certificate /config/keys/letsencrypt/fullchain.pem;
ssl_certificate_key /config/keys/letsencrypt/privkey.pem;
ssl_dhparam /config/nginx/dhparams.pem;
ssl_ciphers 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
ssl_prefer_server_ciphers on;

client_max_body_size 0;

location / {
    try_files $uri $uri/ /index.html /index.php?$args =404;

}

location ~ /new {
    deny all;

}       


location ~ \.php$ {
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
    # With php5-cgi alone:
    fastcgi_pass 127.0.0.1:9000;
    # With php5-fpm:
    #fastcgi_pass unix:/var/run/php5-fpm.sock;
    fastcgi_index index.php;
    include /etc/nginx/fastcgi_params;
}

先感谢您

nginx
4个回答
4
投票

你可以用以下方法明确地打破/test/index.html

location = /test/index.html {
}
location ^~ /test {
    deny all;
}

精确匹配位置具有最高优先级,并且^~修饰符将前缀位置的优先级置于同一级别的正则表达式位置之上。

有关更多信息,请参阅this document


2
投票

我想我不知道这是不是正确的方式,但它的工作原理。随意纠正我

location /test {
location ~ \.(txt|gif|jpg|png)$ {
      deny all;
      }
      } 

使用它可以阻止访问/ test和所有子目录中的所有扩展。


0
投票

我更喜欢return 444;比deny all;拒绝所有显示服务器名称

Nginx configuration disallow or allow files access


0
投票

Richard Smith's answer is correct.这是另一个正确的答案:

location ^~ /test {
    try /test/index.html =404
}
© www.soinside.com 2019 - 2024. All rights reserved.