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

2018-08-24 09:18:18 +08:00
 alwayshere

网站对下载文件目录“ 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 该怎么配置?

1900 次点击
所在节点    程序员
3 条回复
awker
2018-08-24 09:50:58 +08:00
location 匹配有优先级
location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|svg)$ 的优先级 高于 location /protectedFiles
awker
2018-08-24 09:52:12 +08:00
改成这个
location ^~ /protectedFiles
{
internal;
alias /wwwroot/protectedFiles/;
}
oott123
2018-08-24 11:20:57 +08:00
也可以选择不把被保护的文件放到 /wwwroot 下。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/482736

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX