Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Set, add, and clear arbitrary output headers in NGINX http servers

License

NotificationsYou must be signed in to change notification settings

openresty/headers-more-nginx-module

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ngx_headers_more - Set and clear input and output headers...more than "add"!

This module is not distributed with the Nginx source. Seethe installation instructions.

Table of Contents

Version

This document describes headers-more-nginx-modulev0.34 released on 17 July 2022.

Synopsis

 # set the Server output header more_set_headers'Server: my-server'; # set and clear output headerslocation /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';     # your proxy_pass/memcached_pass/or any other config goes here...} # set output headerslocation /type{     more_set_headers'Content-Type: text/plain';     # ...} # set input headerslocation /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';

Description

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

This is an enhanced version of the standardheaders module because it provides more utilities likeresetting or clearing "builtin headers" likeContent-Type,Content-Length, andServer.

It also allows you to specify an optional HTTP status codecriteria using the-s option and an optional contenttype criteria using the-t option while modifying theoutput headers with themore_set_headers andmore_clear_headers directives. For example,

 more_set_headers -s404 -t'text/html' 'X-Foo: Bar';

You can also specify multiple MIME types to filter out in a single-t option.For example,

more_set_headers -t'text/html text/plain' 'X-Foo: Bar';

Never use other parameters likecharset=utf-8 in the-t option values; they will notwork as you would expect.

Input headers can be modified as well. For example

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 themore_set_input_headers andmore_clear_input_headers directives (for request header filtering) while the-s optionis not allowed.

Unlike the standardheaders module, this module's directives will bydefault apply to all the status codes, including4xx and5xx.

Back to TOC

Directives

Back to TOC

more_set_headers

syntax:more_set_headers [-t <content-type list>]... [-s <status-code list>]... [-a] <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 optionAND the response content type matches the types specified by the-t option.

If the "-a" option is specified, the specified output headers can be appended directly without clearing the old fields. The behavior of builtin headers such as "Content-Type", "Content-Length", "Server", etc. cannot be changed.

If either-s or-t is not specified or has an empty list value, then no match is required. Therefore, the following directive set theServer output header to the custom value forany status code andany 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 standardadd_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 -s404 -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:

  1. Name: Value
  2. Name:
  3. Name

The last two effectively clear the value of the headerName.

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 althoughmore_set_headers is allowed inlocation if blocks, it isnot allowed in theserver 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 friendmore_clear_headers will (lazily) register an ouput header filter that modifiesr->headers_out the way you specify.

Back to TOC

more_clear_headers

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 -s404 -t'text/plain' Foo Baz;

is exactly equivalent to

    more_set_headers -s404 -t'text/plain'"Foo: ""Baz: ";

or

    more_set_headers -s404 -t'text/plain' Foo Baz;

Seemore_set_headers for more details.

The wildcard character,*, can also be used at the end of the header name to specify a pattern. For example, the following directiveeffectively clearsany output headers starting by "X-Hidden-":

 more_clear_headers'X-Hidden-*';

The* wildcard support was first introduced inv0.09.

Back to TOC

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

Very much likemore_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 theContent-Typerequest header, rather than the response header.

Behind the scene, use of this directive and its friendmore_clear_input_headers will (lazily)register arewrite phase handler that modifiesr->headers_in the way you specify. Note that it always run at theend oftherewrite phase so that it runsafter the standardrewrite moduleand works in subrequests as well.

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

Back to TOC

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

Clears the specified input headers.

In fact,

    more_clear_input_headers -t'text/plain' Foo Baz;

is exactly equivalent to

    more_set_input_headers -t'text/plain'"Foo: ""Baz: ";

or

    more_set_input_headers -t'text/plain' Foo Baz

To remove request headers "Foo" and "Baz" for all incoming requests regardless of the content type, we can write

    more_clear_input_headers"Foo""Baz";

Seemore_set_input_headers for more details.

The wildcard character,*, can also be used at the end of the header name to specify a pattern. For example, the following directiveeffectively clearsany input headers starting by "X-Hidden-":

     more_clear_input_headers'X-Hidden-*';

Back to TOC

Limitations

  • Unlike the standardheaders module, this module does not automatically take care of the constraint among theExpires,Cache-Control, andLast-Modified headers. You have to get them right yourself or use theheaders module together with this module.
  • You cannot remove theConnection response header using this module because theConnection response header is generated by the standardngx_http_header_filter_module in the Nginx core, whose output header filter runs alwaysafter the filter of this module. The only way to actually remove theConnection header is to patch the Nginx core, that is, editing the C functionngx_http_header_filter in thesrc/http/ngx_http_header_filter_module.c file.

Back to TOC

Installation

Grab the nginx source code fromnginx.org, for example,the version 1.17.8 (seenginx compatibility), and then build the source with this module:

 wget'http://nginx.org/download/nginx-1.17.8.tar.gz' tar -xzvf nginx-1.17.8.tar.gzcd nginx-1.17.8/# Here we assume you would install you nginx under /opt/nginx/. ./configure --prefix=/opt/nginx \     --add-module=/path/to/headers-more-nginx-module make make install

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

Starting from NGINX 1.9.11, you can also compile this module as a dynamic module, by using the--add-dynamic-module=PATH option instead of--add-module=PATH on the./configure command line above. And then you can explicitly load the module in yournginx.conf via theload_moduledirective, for example,

load_module /path/to/modules/ngx_http_headers_more_filter_module.so;

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

Back to TOC

Compatibility

The following versions of Nginx should work with this module:

  • 1.29.x (last tested: 1.29.2)
  • 1.27.x (last tested: 1.27.1)
  • 1.25.x (last tested: 1.25.3)
  • 1.21.x (last tested: 1.21.4)
  • 1.19.x (last tested: 1.19.9)
  • 1.17.x (last tested: 1.17.8)
  • 1.16.x
  • 1.15.x (last tested: 1.15.8)
  • 1.14.x
  • 1.13.x (last tested: 1.13.6)
  • 1.12.x
  • 1.11.x (last tested: 1.11.2)
  • 1.10.x
  • 1.9.x (last tested: 1.9.15)
  • 1.8.x
  • 1.7.x (last tested: 1.7.10)
  • 1.6.x (last tested: 1.6.2)
  • 1.5.x (last tested: 1.5.8)
  • 1.4.x (last tested: 1.4.4)
  • 1.3.x (last tested: 1.3.7)
  • 1.2.x (last tested: 1.2.9)
  • 1.1.x (last tested: 1.1.5)
  • 1.0.x (last tested: 1.0.11)
  • 0.9.x (last tested: 0.9.4)
  • 0.8.x (last tested: 0.8.54)
  • 0.7.x >= 0.7.44 (last tested: 0.7.68)

Earlier versions of Nginx like 0.6.x and 0.5.x willnot work.

If you find that any particular version of Nginx above 0.7.44 does not work with this module, please considerreporting a bug.

Back to TOC

Community

Back to TOC

English Mailing List

Theopenresty-en mailing list is for English speakers.

Back to TOC

Chinese Mailing List

Theopenresty mailing list is for Chinese speakers.

Back to TOC

Bugs and Patches

Please submit bug reports, wishlists, or patches by

  1. creating a ticket on theGitHub Issue Tracker,
  2. or posting to theOpenResty community.

Back to TOC

Source Repository

Available on github atopenresty/headers-more-nginx-module.

Back to TOC

Changes

The changes of every release of this module can be obtained from the OpenResty bundle's change logs:

http://openresty.org/#Changes

Back to TOC

Test Suite

This module comes with a Perl-driven test suite. Thetest cases aredeclarative too. Thanks to theTest::Nginx module in the Perl world.

To run it on your side:

 $ PATH=/path/to/your/nginx-with-headers-more-module:$PATH prove -r t

To run the test suite with valgrind's memcheck, use the following commands:

 $export PATH=/path/to/your/nginx-with-headers-more-module:$PATH $ TEST_NGINX_USE_VALGRIND=1 prove -r t

You need to terminate any Nginx processes before running the test suite if you have changed the Nginx server binary.

Because a single nginx server (by default,localhost:1984) is used across all the test scripts (.t files), it's meaningless to run the test suite in parallel by specifying-jN when invoking theprove utility.

Some parts of the test suite requires modulesproxy,rewrite, andecho to be enabled as well when building Nginx.

Back to TOC

TODO

  • Support variables in new headers' keys.

Back to TOC

Getting involved

You'll be very welcomed to submit patches to theauthor or just ask for a commit bit to thesource repository on GitHub.

Back to TOC

Authors

This wiki page is also maintained by the author himself, and everybody is encouraged to improve this page as well.

Back to TOC

Copyright & License

The code base is borrowed directly from the standardheaders module in Nginx 0.8.24. This part of code is copyrighted by Igor Sysoev.

Copyright (c) 2009-2025, Yichun "agentzh" Zhang (章亦春)agentzh@gmail.com, OpenResty Inc.

Copyright (c) 2010-2013, Bernd Dorn.

The license text is available in theLICENSE file located in the root directory of the project.

Back to TOC

See Also

Back to TOC

About

Set, add, and clear arbitrary output headers in NGINX http servers

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors16


[8]ページ先頭

©2009-2025 Movatter.jp