【隨筆】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"。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM