系统相关
首页 > 系统相关> > php – Nginx:X-Accel-Redirect无法在具有MIME扩展名的文件中工作

php – Nginx:X-Accel-Redirect无法在具有MIME扩展名的文件中工作

作者:互联网

我正在开发一个webapp,X-Accel-Redirect标头只能在没有扩展名的文件中正常工作.出于某种原因,如果我在文件名中添加扩展名,则X-Accel-Redirect不起作用.

工作范例:

X-Accel-Redirect: /protected_files/myfile01.z

非工作示例:

X-Accel-Redirect: /protected_files/myfile01.zip

我正在使用nginx 1.7.1.

最初,奇怪的部分是,如果我用mime.types文件中未注册的内容更改扩展部分(在本例中为“.zip”),它工作正常(显然我相应地重命名文件),但扩展指向知道mime类型(类似“zip”,“jpg”,“html”)将生成“404 Not found”错误.

更新:
似乎问题是由于我在conf文件中的这个规则:

location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)${
    try_files $uri =404;
}

出于某种原因,似乎nginx首先在文件系统中测试文件的存在,然后它尝试“内部/别名”路径.

关于如何让nginx将所有来自X-Accel-Redirect的“/ protected_files”直接过滤到“内部”而不是先尝试在其他路径中查找的想法?

提前致谢.

解决方法:

该错误是由于nginx配置文件中的规则冲突造成的.

所以,解决方案是:

location ^~ /protected_files { # ^~ needed according to the [nginx docs][1] to avoid nginx to check more locations
    internal;
    alias /path/to/static/files/directory;
}

#avoid processing of calls to unexisting static files by my app
location ~ \.(js|css|png|jpg|gif|swf|ico|pdf|mov|fla|zip|rar)${
    try_files $uri =404;
}

希望这有助于你们中的许多人.

标签:nginx,php,mime-types,x-sendfile,x-accel-redirect
来源: https://codeday.me/bug/20190612/1225367.html