V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
alwayshere
V2EX  ›  程序员

搞不懂 Nginx 的 X-sendfile 设置了 internal 访问,为啥浏览器还是可以直接访问?

  •  
  •   alwayshere · 2018-08-24 09:18:18 +08:00 · 1897 次点击
    这是一个创建于 2064 天前的主题,其中的信息可能已经有所发展或是发生改变。

    网站对下载文件目录“ protectedFiles ”设置了权限,通过 Nginx 的 X-sendfile 设置了只允许内部访问,外部没法访问,下面是 Nginx 的配置文件:

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|svg)$
    {
        expires      30d;
        add_header Access-Control-Allow-Origin *;
        error_log off;
        access_log off;
    }
    
    location /protectedFiles
    {
        internal;
        alias   /wwwroot/protectedFiles/;
    }
    

    现在对于一些 protectedFiles 目录下的 zip 或者 rar 的文件,外部访问都正常的返回 404,但是对于 protectedFiles 目录下的图片文件,外部全都能正常访问,我尝试改了下:

    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|svg|zip|rar)$
    {
        expires      30d;
        add_header Access-Control-Allow-Origin *;
        error_log off;
        access_log off;
    }
    

    发现 zip 和 rar 文件也可以外部访问了,这是怎么回事?

    如果我想实现 protectedFiles 目录下的图片文件的设置为 expires 30d;add_header Access-Control-Allow-Origin *; 同时禁止 protectedFiles 目录下的所有文件禁止外部访问,nginx 该怎么配置?

    3 条回复    2018-08-24 11:20:57 +08:00
    awker
        1
    awker  
       2018-08-24 09:50:58 +08:00
    location 匹配有优先级
    location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|svg)$ 的优先级 高于 location /protectedFiles
    awker
        2
    awker  
       2018-08-24 09:52:12 +08:00   ❤️ 1
    改成这个
    location ^~ /protectedFiles
    {
    internal;
    alias /wwwroot/protectedFiles/;
    }
    oott123
        3
    oott123  
       2018-08-24 11:20:57 +08:00
    也可以选择不把被保护的文件放到 /wwwroot 下。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1321 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 23:38 · PVG 07:38 · LAX 16:38 · JFK 19:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.