Nginx 限制对文件的访问

Sam*_*rma 4 ubuntu nginx

目前我的配置文件(/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;
    }

}
Run Code Online (Sandbox Code Playgroud)

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

nba*_*ari 10

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

\n\n
server {\n    server_name _;\n    listen 80 default_server;\n\n    location = /credentials.js {\n        deny all;\n        return 404;\n    }\n\n    location / {\n        add_header Content-Type text/plain;\n        return 200 "hello world\\n\\n";\n    }\n}\n
Run Code Online (Sandbox Code Playgroud)\n\n

nginx 位置文档:

\n\n
\n

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

\n
\n