nginx、x-accel-redirect 和 mime 类型

Tom*_*ohl 9 nginx mime mime-type

在我的 nginx 0.8.34 设置中,我使用 X-Accel-Redirect 功能来控制应用程序代码中的文件下载,同时不让应用程序自行处理下载。

在经历了很多痛苦之后,现在基本上可以工作了,除了 nginx 总是返回带有text/html内容类型的文件。

默认内容类型是 application/octet-stream,在http块中指定。

server 块包含文件存储目录的定义,其中包括:

location /files {
  default_type  application/octet-stream;
  alias /srv/www/uploads;
  internal;  
}
Run Code Online (Sandbox Code Playgroud)

所以我甚至在这里指定了内容类型,但没有任何改变。

我不想通过应用程序设置 Content-Type 因为那样我会减慢我的速度(我首先必须确定它)。所以理想情况下,nginx 会根据文件扩展名返回正确的 mimetype(我确实在http块中包含了 mime.types )。

小智 15

如果您想让 nginx 猜测正确的 MIME 类型,您必须确保没有从后端服务器返回内容类型。

With django:
    response = HttpResponse()
    response['Content-Type'] = ''
    response['X-Accel-Redirect'] ='/my/file.jpg'
    return response
Run Code Online (Sandbox Code Playgroud)