header modify (more_clear_headers)

最後更新: 2021-03-18

介紹

nginx 源碼沒有包含該模塊, 需要另行添加.

Code: https://github.com/openresty/headers-more-nginx-module

This is an enhanced version of the standard "headers" module

 * clearing "builtin headers"
    ("server_tokens off;" 拎唔走的 response header 也可拎走)

Feature

  • more_set_headers
  • more_clear_headers

 


Compile

 

cd /usr/src/nginx

wget https://github.com/openresty/headers-more-nginx-module/archive/refs/tags... \
 -O headers-more-nginx-module_v0.33.zip

unzip headers-more-nginx-module_v0.33.zip

...

Example

# Remore header

more_clear_headers 'Server';
more_clear_headers 'X-Powered-By';

# The wildcard character "*" can also be used at the end of the header name to specify a pattern.

more_clear_headers 'X-Hidden-*';

# Replaces (if any) or adds (if not any)

more_set_headers 'X-MyHeader: blah' 'X-MyHeader2: foo';

# 在特定 types 的檔案才 add/replace header ( -t <content-type list>)

more_set_headers -t 'text/plain text/css' 'Content-Type: text/foo';

# 在特定 status codes 時才 add/replace header (-s <status-code list>)

more_set_headers -s '400 404 500 503' -s 413 'Foo: Bar';