Nginx: X-Accel-Redirect在已知MIME扩展名的文件中不起作用


Nginx: X-Accel-Redirect not working in files with know MIME extension

我正在开发一个web应用程序和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中未注册的东西更改扩展名部分(在本例中为".zip")。类型文件,它工作得很好(显然我相应地重命名文件),但扩展名指向一个已知的mime类型(如"zip","jpg","html")将生成一个"404 Not found"错误。

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

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

由于某些原因,nginx似乎先测试文件在文件系统中的存在,然后再尝试"内部/别名"路径。

关于如何让nginx过滤所有的"/protected_files"来自X-Accel-Redirect直接到"内部",而不是试图在其他路径先找到的想法吗?

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;
}