简体   繁体   中英

set not add header in nginx

Is there a way to set / replace / merge a header in nginx like its possible in apache?

regarding to this link http://nginx.org/en/docs/http/ngx_http_headers_module.html it seems, that its only possible to add a header. This brings up some problems, if the header has already been set (eg through the php code) and should be replaced / changed to correct values.

For apache one can set / append / merge and add, http://httpd.apache.org/docs/2.2/mod/mod_headers.html

this is kinda basic feature, so it should be possible in nginx somehow, but i cant find out.

Take a look at HttpHeadersMoreModule.

This module allows you to add, set, or clear any output or input header that you specify.

This is an enhanced version of the standard headers module because it provides more utilities like resetting or clearing "builtin headers" like Content-Type, Content-Length, and Server.

It also allows you to specify an optional HTTP status code criteria using the -s option and an optional content type criteria using the -t option while modifying the output headers with the more_set_headers and more_clear_headers directives.

Source: http://wiki.nginx.org/HttpHeadersMoreModule

Actually the Nginx "add_header" directive will not overwrite the header but add the value to it if it exists. It is not so clear from the docs however: http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header

But in the HttpHeadersMoreModule docs ( https://github.com/openresty/headers-more-nginx-module#more_set_headers ) it says: "If you want to add headers incrementally, use the standard add_header directive instead."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM