Nginx解决下载json静态文件乱码

Nginx解决下载json静态文件乱码

2017-10-20 / 0 评论 / 675 阅读 / 正在检测是否收录...
温馨提示:
本文最后更新于2021年10月27日,已超过911天没有更新,若内容或图片失效,请留言反馈。

Linux下采用的是utf-8的字符编码,默认情况下我们的浏览器在服务器没有指定编码或者静态页面没有声明编码的情况下会以GBK的编码去渲染页面,这样默认情况下返回中文的话浏览器用gbk来解析utf-8编码,显然会出现乱码,这时要在nginx location块中主动添加header来输出正确编码

添加配置

add_header Content-Type 'text/html; charset=utf-8';

示例

location ~ .*\.(gif|jpg|jpeg|png|bmp|swf|flv|ico)$ {
        expires 30d;
        }
location ~ .*\.(js|css)?$ {
        expires 7d;
        }
location ~ {
    proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        add_header Content-Type 'text/html; charset=utf-8';
        }
}

0

评论 (0)

取消