Nginx限制对文件的访问

时间:2018-07-06 05:20:19

标签: ubuntu nginx access

当前我的配置文件(/ etc / nginx / sites-available / default)显示

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    root /var/www/html;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html;

    server_name _;

    location /credentials.js {
                deny all;
                return 404;
        }

    location / {
        # First attempt to serve request as file, then
        # as directory, then fall back to displaying a 404.
        try_files $uri $uri/ =404;
    }

}

,但是我仍然可以通过网络上的example.com/credentials.js访问certificate.js。有什么建议么?

1 个答案:

答案 0 :(得分:0)

尝试将=添加到您的位置,这将完全匹配:

server {
    server_name _;
    listen 80 default_server;

    location = /credentials.js {
        deny all;
        return 404;
    }

    location / {
        add_header Content-Type text/plain;
        return 200 "hello world\n\n";
    }
}

nginx location docs:

  

如果找到完全匹配的内容,搜索将终止。例如,如果“ /”请求频繁发生,则定义“ location = /”将加快这些请求的处理速度,因为搜索将在第一次比较后立即终止。这样的位置显然不能包含嵌套的位置。