Nginx:阻止直接访问静态文件

时间:2014-07-06 00:36:40

标签: nginx

我一直在寻找一段时间,但却找不到符合我需要的东西。我不需要热链接保护,因为我想阻止人们直接访问我的文件。让我们说:

我的website.com请求website.com/assets/custom.js,这是有效的,但我希望直接访问此文件的访问者获得403 status code或其他内容。我真的不知道是否可能,而且我没有任何合乎逻辑的步骤......

问候!

3 个答案:

答案 0 :(得分:7)

您可以使用nginx referer模块:http://nginx.org/en/docs/http/ngx_http_referer_module.html。 像这样:

server {
    listen 80;
    server_name website.com;
    root /var/www/website.com/html ;
    location /assets/ {
        valid_referers website.com/ website.com/index.html website.com/some_other_good_page.html ;
        if $invalid_referer {
            deny all;
        }
    }
}

此配置保护assets目录。但请记住,这不能保证并且仅适用于浏览器 - 任何机构都可以使用curl或telnet模拟有效请求。为了真正的安全,您需要使用动态生成的页面和动态生成的链接。

答案 1 :(得分:1)

如果您在Google搜索结果中显示nginx支持的开发实例,则可以通过一种快速简便的方法阻止搜索引擎抓取您的网站。将以下行添加到要阻止爬网的块的virtualhost配置文件的位置块中。

add_header  X-Robots-Tag "noindex, nofollow, nosnippet, noarchive";

答案 2 :(得分:-1)

您只需将这些行与文件夹名称放在一起就可以拒绝访问任何文件夹或文件

location ~ /(no_access_folder|folder_2)
{
     deny all;
     return 403;
}
相关问题