【随笔】nginx add_header指令的使用

  nginx配置文件通过使用add_header指令来设置response header。

  具体方法如下:

  add_header key value

add_header Cache-Control no-store
add_header Content-Encoding gzip

  用以改变Content-Type时比较特殊,可以在它设置了类型的同时还会指定charset,不过Content-Type和charset之间有分号,所以我们需要这样来添加。

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

  这样就可以给responent header的Content-Type添加指定的属性了。

例如:

  nginx设置:

 1 http {
 2     include       mime.types;
 3        default_type  application/octet-stream ;
 4     sendfile        on;
 5     keepalive_timeout  65;
 6 
 7     server {
 8         listen       8080;
 9         server_name  localhost;
10         index index.html index.htm;
11         root /usr/local/openresty/nginx/work;
12         
13         location ~* .(js)$ {
14             add_header  Content-Type 'text/javascript;charset=utf-8';
15         }
16         
17         location /dir {
18             default_type application/javascript;
19             content_by_lua_file /usr/local/openresty/nginx/work/resources/lua/index.lua;
20         }
21     }
22 }

  其中匹配所有.js文件,给其responent header的Content-Type值赋值为"text/javascript;charset=utf-8"。

 

转载于:https://www.cnblogs.com/linxiong945/p/4174262.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值