Nginx通常根据/etc/nginx/mime.types文件中类型设置content-type
有时需要根据实际需要指定content-type,比如对于下载,如果按照mime.types里面的定义:
image/jpeg jpeg jpg;
那么当下载图片时,浏览器会在窗口内直接显示图片,而不是另存为文件 。
通过设置add_header:
location /download {
add_header Content-Type application/octet-stream;
}
会导致响应中有两个content-type,一个是image/jpeg,另一个是application/octet-stream
其实可以通过types{ }取消默认content-type,然后再指定需要的content-type:
location /download {
types { }
default_type application/octet-stream;
}
如果需要可以对location进行正则匹配,这样可以根据需要返回响应头Content-Type

Nginx默认依据mime.types文件设定content-type,但有时需按需指定,如下载场景。若直接使用add_header设置Content-Type,可能会导致响应中出现多个content-type。解决方法是使用types块清除默认值,并通过default_type设定所需content-type,如在/download路径下,设置default_type application/octet-stream确保文件被作为二进制流下载。

1203

被折叠的 条评论
为什么被折叠?



