categories:nginx  tags:  author:


ngx_headers_more – Set and clear input and output headers…more than “add”!
设置和清除 http的输入和输出头

This module is not distributed with the Nginx source. See the installation instructions.
本模块不是标准模块, 安装需要参照最后的安装指令


This document describes headers-more-nginx-module v0.25 released on 10 January 2014.

# set the Server output header  设置server输出header
more_set_headers ‘Server: my-server';

# set and clear output headers       设置和清除输出header
location /bar {
more_set_headers ‘X-MyHeader: blah’ ‘X-MyHeader2: foo';
more_set_headers -t ‘text/plain text/css’ ‘Content-Type: text/foo';
more_set_headers -s ‘400 404 500 503′ -s 413 ‘Foo: Bar';
more_clear_headers ‘Content-Type';          清除header

# your proxy_pass/memcached_pass/or any other config goes here…

# set output headers
location /type {
more_set_headers ‘Content-Type: text/plain';
# …

# set input headers    设置输入 header, 由nginx传递给 应用服务器,例如tomcat
location /foo {
set $my_host ‘my dog';
more_set_input_headers ‘Host: $my_host';
more_set_input_headers -t ‘text/plain’ ‘X-Foo: bah';

# now $host and $http_host have their new values…
# …

# replace input header X-Foo *only* if it already exists
more_set_input_headers -r ‘X-Foo: howdy';


This module allows you to add, set, or clear any output or input header that you specify.
这个模块允许您 添加,设置,清除 任何输出(发给客户端浏览器的)和输入(有浏览器发过来的)流中的http头。

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. For example,
模块也允许你设置一个可选的http状态码 通过使用-s选项, 和-t选项改变输出头  下面这个功能的例子:

more_set_headers -s 404 -t ‘text/html’ ‘X-Foo: Bar';

Input headers can be modified as well. For example    输入的http头的例子

location /foo {
more_set_input_headers ‘Host: foo’ ‘User-Agent: faked';
# now $host, $http_host, $user_agent, and
#   $http_user_agent all have their new values.

The option -t is also available in the more_set_input_headers and more_clear_input_headers directives (for request header

filtering) while the -s option is not allowed.

-t选项也可以在more_set_input_headers 和 more_clear_input_headers指令中使用

Unlike the standard headers module, this module’s directives will by default apply to all the status codes, including 4xx and 5xx.


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

Replaces (if any) or adds (if not any) the specified output headers when the response status code matches the codes specified by

the -s option AND the response content type matches the types specified by the -t option.

If either -s or -t is not specified or has an empty list value, then no match is required. Therefore, the following directive set

the Server output header to the custom value for any status code and any content type:

more_set_headers    “Server: my_server”;

Existing response headers with the same name are always overridden. If you want to add headers incrementally, use the standard

add_header directive instead.

A single directive can set/add multiple output headers. For example

more_set_headers ‘Foo: bar’ ‘Baz: bah';

Multiple occurrences of the options are allowed in a single directive. Their values will be merged together. For instance

more_set_headers -s 404 -s ‘500 503′ ‘Foo: bar';

is equivalent to

more_set_headers -s ‘404 500 503′ ‘Foo: bar';

The new header should be the one of the forms:

Name: Value

The last two effectively clear the value of the header Name.

Nginx variables are allowed in header values. For example:

set $my_var “dog”;
more_set_headers “Server: $my_var”;

But variables won’t work in header keys due to performance considerations.

Multiple set/clear header directives are allowed in a single location, and they’re executed sequentially.

Directives inherited from an upper level scope (say, http block or server blocks) are executed before the directives in the

location block.

Note that although more_set_headers is allowed in location if blocks, it is not allowed in the server if blocks, as in

?  # This is NOT allowed!
?  server {
?      if ($args ~ ‘download’) {
?          more_set_headers ‘Foo: Bar';
?      }
?      …
?  }

Behind the scene, use of this directive and its friend more_clear_headers will (lazily) register an ouput header filter that

modifies r->headers_out the way you specify.

Back to TOC

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

Clears the specified output headers.

In fact,

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

is exactly equivalent to

more_set_headers -s 404 -t ‘text/plain’ “Foo: ” “Baz: “;


more_set_headers -s 404 -t ‘text/plain’ Foo Baz

See more_set_headers for more details.

Wildcard * can also be used to specify a header name pattern. For example, the following directive effectively clears any output

headers starting by “X-Hidden-“:

more_clear_headers ‘X-Hidden-*';

The * wildcard support was first introduced in v0.09.

Back to TOC

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

default: no

context: http, server, location, location if

phase: rewrite tail

Very much like more_set_headers except that it operates on input headers (or request headers) and it only supports the -t option.

Note that using the -t option in this directive means filtering by the Content-Type request header, rather than the response


Behind the scene, use of this directive and its friend more_clear_input_headers will (lazily) register a rewrite phase handler

that modifies r->headers_in the way you specify. Note that it always run at the end of the rewrite so that it runs after the

standard rewrite module and works in subrequests as well.

If the -r option is specified, then the headers will be replaced to the new values only if they already exist.

Back to TOC

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

default: no

context: http, server, location, location if

phase: rewrite tail

Clears the specified input headers.

In fact,

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

is exactly equivalent to

more_set_input_headers -s 404 -t ‘text/plain’ “Foo: ” “Baz: “;


more_set_input_headers -s 404 -t ‘text/plain’ Foo Baz

See more_set_input_headers for more details.

Back to TOC

Unlike the standard headers module, this module does not automatically take care of the constraint among the Expires, Cache-

Control, and Last-Modified headers. You have to get them right yourself or use the headers module together with this module.
You cannot remove the Connection response header using this module because the Connection response header is generated by the

standard ngx_http_header_filter_module in the Nginx core, whose output header filter runs always after the filter of this module.

The only way to actually remove the Connection header is to patch the Nginx core, that is, editing the C function

ngx_http_header_filter in the src/http/ngx_http_header_filter_module.c file.

Back to TOC

Grab the nginx source code from, for example, the version 1.7.7 (see nginx compatibility), and then build the source

with this module:

wget ‘’
tar -xzvf nginx-1.7.7.tar.gz
cd nginx-1.7.7/

# Here we assume you would install you nginx under /opt/nginx/.
./configure –prefix=/opt/nginx \

make install

Download the latest version of the release tarball of this module from headers-more-nginx-module file list.

Also, this module is included and enabled by default in the ngx_openresty bundle.

Back to TOC

快乐成长 每天进步一点点      京ICP备18032580号-1