Nginx Header,实现对HTTP/S请求、响应进行添加、修改、删除等操作

删除Header

来源库:ngx_http_fastcgi_module、ngx_http_proxy_module

fastcgi_hide_header ‘Key’;

Syntax: fastcgi_hide_header field;
Default:
Context: httpserverlocation

proxy_hide_header ‘Key’;

Syntax: proxy_hide_header field;
Default:
Context: httpserverlocation

例如:反向代理和fastcgi区分不同的场景使用。

fastcgi_hide_header X-Powered-By;

proxy_hide_header X-Powered-By;

 

修改Header

通过内置的操作,修改header分为两步,先将其删除再增加。

例如:

fastcgi_hide_header Content-Type;

proxy_hide_header Content-Type;

add_header ‘Content-Type’ ‘text/css’;

 

添加请求Header

proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

fastcgi_param  ‘HTTP-X-Forwarded-For’ $remote_addr;

删除请求Header

proxy_set_header X-Forwarded-For ”;

fastcgi_param  ‘HTTP-X-Forwarded-For’ ”;

 

通过第三方模块

headers-more-nginx-module

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

添加/设置Header

syntax: more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...

default: no

context: http, server, location, location if

phase: output-header-filter

more_set_headers “Server: yunjiasu-nginx”;

清除Header

syntax: more_clear_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...

default: no

context: http, server, location, location if

phase: output-header-filter

more_clear_headers -s 404 -t ‘text/plain’ Foo Baz;

more_clear_headers ‘X-Hidden-*’;

 

添加请求Header

more_set_input_headers

syntax: more_set_input_headers [-r] [-t <content-type list>]... <new-header>...

default: no

context: http, server, location, location if

phase: rewrite tail

删除请求Header

more_clear_input_headers

syntax: more_clear_input_headers [-t <content-type list>]... <new-header>...

default: no

context: http, server, location, location if

phase: rewrite tail

例子:

more_clear_input_headers -t ‘text/plain’ Foo Baz;

more_clear_input_headers “Foo” “Baz”;
more_clear_input_headers ‘X-Hidden-*’;

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注