问题描述
在使用minio时,将静态html文件存储在minio上。通过minio的public权限设置可以通过http方式直接访问,不需要认证。
当使用Firefox访问minio上的html时,出现自动下载框,而不显示html页面。
问题分析
对比普通的html页面响应头信息,发现此请求的content-type
为application/octet-stream
,针对响应头content-type
为application/octet-stream
时,浏览器会认为是一个下载请求,所以浏览器弹出下载框
问题解决
问题确定后,解决方案也可确认,只需将content-type
改为text/html
即可
查看nginx方案后,决定在使用nginx作为转发,修改响应头信息
可在nginx配置的location
域中添加add_header指令
location /publish/ {
add_header Content-Type "text/html;charset=utf-8";
proxy_pass http://10.0.54.235:9003;
}
配置nginx后,在Chrome浏览器中,可正常访问。但是在Firefox中,第一次可访问,第二次扔弹出下载框,查看请求后发现一下问题:
- Firefox中第一次请求,返回200,响应头中
content-type
值有两个,application/octet-stream
和text/html
- Firefox中第二次请求,返回304,响应头中
content-type
值有两个,application/octet-stream
和text/html
在此可以猜测Firefox返回200和304时,识别的content-type
不同,同时在Firefox中得到验证
对于这个结果,可以得出nginx的add_header
指令是对header
作为追加,而非修改,那么我们需要一种修改header的方法
经百度一番后,发现nginx第三方模块headers-more-nginx-module
可进行修改header
下面安装headers-more-nginx-module
模块
wget 'http://nginx.org/download/nginx-1.13.6.tar.gz'
tar -xzvf nginx-1.13.6.tar.gz
cd nginx-1.13.6/
# Here we assume you would install you nginx under /opt/nginx/.
./configure --prefix=/opt/nginx \
--add-module=/path/to/headers-more-nginx-module
make
make install
重新配置nginx
location /publish/ {
more_set_headers "Content-Type:text/html; charset=utf-8";
proxy_pass http://10.0.54.235:9003;
}
这里配置完成后,一定要重启nginx,而非reload,否则第三方模块不会被加载
sbin/nginx -s stop
sbin/nginx
再次使用Firefox,可以正常访问html页面
问题总结
- 浏览器弹出自动下载,原因可能为
content-type
值是类似二进制类型,如application/octet-stream
- nginx可修改响应头,
add_header
指令只看追加header信息,不可修改 - 若
content-type
为多个值,浏览器不确定使用哪种 - nginx修改响应头信息
header
,可使用第三方库headers-more-nginx-module
- nginx在重新加载第三方库时,需要重启,不可直接使用
nginx -s reload
重新加载