解决html文件在Firefox中访问会自动弹出下载框

问题描述

在使用minio时,将静态html文件存储在minio上。通过minio的public权限设置可以通过http方式直接访问,不需要认证。
当使用Firefox访问minio上的html时,出现自动下载框,而不显示html页面。

在这里插入图片描述

问题分析

对比普通的html页面响应头信息,发现此请求的content-typeapplication/octet-stream,针对响应头content-typeapplication/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中,第一次可访问,第二次扔弹出下载框,查看请求后发现一下问题:

  1. Firefox中第一次请求,返回200,响应头中content-type值有两个,application/octet-streamtext/html
  2. Firefox中第二次请求,返回304,响应头中content-type值有两个,application/octet-streamtext/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

参考headers-more-nginx-module

重新配置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页面

问题总结

  1. 浏览器弹出自动下载,原因可能为content-type值是类似二进制类型,如application/octet-stream
  2. nginx可修改响应头,add_header指令只看追加header信息,不可修改
  3. content-type为多个值,浏览器不确定使用哪种
  4. nginx修改响应头信息header,可使用第三方库headers-more-nginx-module
  5. nginx在重新加载第三方库时,需要重启,不可直接使用nginx -s reload重新加载
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值