• Home
  • History
  • Annotate
Name Date Size #Lines LOC

..03-May-2022-

src/H01-Oct-2020-2,6601,876

t/H01-Oct-2020-3,9733,188

util/H01-Oct-2020-3423

.gitattributesH A D01-Oct-202027 21

.gitignoreH A D01-Oct-2020451 5554

.travis.ymlH A D01-Oct-20201.7 KiB5343

README.markdownH A D01-Oct-202018.2 KiB554366

configH A D01-Oct-20201.4 KiB3328

valgrind.suppressH A D01-Oct-20202.4 KiB136135

README.markdown

1Name
2====
3
4**ngx_headers_more** - Set and clear input and output headers...more than "add"!
5
6*This module is not distributed with the Nginx source.* See [the installation instructions](#installation).
7
8Table of Contents
9=================
10
11* [Name](#name)
12* [Version](#version)
13* [Synopsis](#synopsis)
14* [Description](#description)
15* [Directives](#directives)
16    * [more_set_headers](#more_set_headers)
17    * [more_clear_headers](#more_clear_headers)
18    * [more_set_input_headers](#more_set_input_headers)
19    * [more_clear_input_headers](#more_clear_input_headers)
20* [Limitations](#limitations)
21* [Installation](#installation)
22* [Compatibility](#compatibility)
23* [Community](#community)
24    * [English Mailing List](#english-mailing-list)
25    * [Chinese Mailing List](#chinese-mailing-list)
26* [Bugs and Patches](#bugs-and-patches)
27* [Source Repository](#source-repository)
28* [Changes](#changes)
29* [Test Suite](#test-suite)
30* [TODO](#todo)
31* [Getting involved](#getting-involved)
32* [Authors](#authors)
33* [Copyright & License](#copyright--license)
34* [See Also](#see-also)
35
36Version
37=======
38
39This document describes headers-more-nginx-module [v0.33](https://github.com/openresty/headers-more-nginx-module/tags) released on 3 November 2017.
40
41Synopsis
42========
43
44```nginx
45
46 # set the Server output header
47 more_set_headers 'Server: my-server';
48
49 # set and clear output headers
50 location /bar {
51     more_set_headers 'X-MyHeader: blah' 'X-MyHeader2: foo';
52     more_set_headers -t 'text/plain text/css' 'Content-Type: text/foo';
53     more_set_headers -s '400 404 500 503' -s 413 'Foo: Bar';
54     more_clear_headers 'Content-Type';
55
56     # your proxy_pass/memcached_pass/or any other config goes here...
57 }
58
59 # set output headers
60 location /type {
61     more_set_headers 'Content-Type: text/plain';
62     # ...
63 }
64
65 # set input headers
66 location /foo {
67     set $my_host 'my dog';
68     more_set_input_headers 'Host: $my_host';
69     more_set_input_headers -t 'text/plain' 'X-Foo: bah';
70
71     # now $host and $http_host have their new values...
72     # ...
73 }
74
75 # replace input header X-Foo *only* if it already exists
76 more_set_input_headers -r 'X-Foo: howdy';
77```
78
79Description
80===========
81
82This module allows you to add, set, or clear any output
83or input header that you specify.
84
85This is an enhanced version of the standard
86[headers](http://nginx.org/en/docs/http/ngx_http_headers_module.html) module because it provides more utilities like
87resetting or clearing "builtin headers" like `Content-Type`,
88`Content-Length`, and `Server`.
89
90It also allows you to specify an optional HTTP status code
91criteria using the `-s` option and an optional content
92type criteria using the `-t` option while modifying the
93output headers with the [more_set_headers](#more_set_headers) and
94[more_clear_headers](#more_clear_headers) directives. For example,
95
96```nginx
97 more_set_headers -s 404 -t 'text/html' 'X-Foo: Bar';
98```
99
100You can also specify multiple MIME types to filter out in a single `-t` option.
101For example,
102
103```nginx
104more_set_headers -t 'text/html text/plain' 'X-Foo: Bar';
105```
106
107Never use other paramemters like `charset=utf-8` in the `-t` option values; they will not
108work as you would expect.
109
110Input headers can be modified as well. For example
111
112```nginx
113 location /foo {
114     more_set_input_headers 'Host: foo' 'User-Agent: faked';
115     # now $host, $http_host, $user_agent, and
116     #   $http_user_agent all have their new values.
117 }
118```
119
120The option `-t` is also available in the
121[more_set_input_headers](#more_set_input_headers) and
122[more_clear_input_headers](#more_clear_input_headers) directives (for request header filtering) while the `-s` option
123is not allowed.
124
125Unlike the standard [headers](http://nginx.org/en/docs/http/ngx_http_headers_module.html) module, this module's directives will by
126default apply to all the status codes, including `4xx` and `5xx`.
127
128[Back to TOC](#table-of-contents)
129
130Directives
131==========
132
133[Back to TOC](#table-of-contents)
134
135more_set_headers
136----------------
137**syntax:** *more_set_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...*
138
139**default:** *no*
140
141**context:** *http, server, location, location if*
142
143**phase:** *output-header-filter*
144
145Replaces (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.
146
147If 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:
148
149```nginx
150
151   more_set_headers    "Server: my_server";
152```
153
154Existing response headers with the same name are always overridden. If you want to add headers incrementally, use the standard [add_header](http://nginx.org/en/docs/http/ngx_http_headers_module.html#add_header) directive instead.
155
156A single directive can set/add multiple output headers. For example
157
158```nginx
159
160   more_set_headers 'Foo: bar' 'Baz: bah';
161```
162
163Multiple occurrences of the options are allowed in a single directive. Their values will be merged together. For instance
164
165```nginx
166
167   more_set_headers -s 404 -s '500 503' 'Foo: bar';
168```
169
170is equivalent to
171
172```nginx
173
174   more_set_headers -s '404 500 503' 'Foo: bar';
175```
176
177The new header should be the one of the forms:
178
1791. `Name: Value`
1801. `Name: `
1811. `Name`
182
183The last two effectively clear the value of the header `Name`.
184
185Nginx variables are allowed in header values. For example:
186
187```nginx
188
189    set $my_var "dog";
190    more_set_headers "Server: $my_var";
191```
192
193But variables won't work in header keys due to performance considerations.
194
195Multiple set/clear header directives are allowed in a single location, and they're executed sequentially.
196
197Directives inherited from an upper level scope (say, http block or server blocks) are executed before the directives in the location block.
198
199Note that although `more_set_headers` is allowed in *location* if blocks, it is *not* allowed in the *server* if blocks, as in
200
201```nginx
202
203   ?  # This is NOT allowed!
204   ?  server {
205   ?      if ($args ~ 'download') {
206   ?          more_set_headers 'Foo: Bar';
207   ?      }
208   ?      ...
209   ?  }
210```
211
212Behind the scene, use of this directive and its friend [more_clear_headers](#more_clear_headers) will (lazily) register an ouput header filter that modifies `r->headers_out` the way you specify.
213
214[Back to TOC](#table-of-contents)
215
216more_clear_headers
217------------------
218**syntax:** *more_clear_headers [-t <content-type list>]... [-s <status-code list>]... <new-header>...*
219
220**default:** *no*
221
222**context:** *http, server, location, location if*
223
224**phase:** *output-header-filter*
225
226Clears the specified output headers.
227
228In fact,
229
230```nginx
231
232    more_clear_headers -s 404 -t 'text/plain' Foo Baz;
233```
234
235is exactly equivalent to
236
237```nginx
238
239    more_set_headers -s 404 -t 'text/plain' "Foo: " "Baz: ";
240```
241
242or
243
244```nginx
245
246    more_set_headers -s 404 -t 'text/plain' Foo Baz
247```
248
249See [more_set_headers](#more_set_headers) for more details.
250
251The wildcard character, `*`, can also be used at the end of the header name to specify a pattern. For example, the following directive
252effectively clears *any* output headers starting by "`X-Hidden-`":
253
254```nginx
255
256 more_clear_headers 'X-Hidden-*';
257```
258
259The `*` wildcard support was first introduced in [v0.09](#v009).
260
261[Back to TOC](#table-of-contents)
262
263more_set_input_headers
264----------------------
265**syntax:** *more_set_input_headers [-r] [-t <content-type list>]... <new-header>...*
266
267**default:** *no*
268
269**context:** *http, server, location, location if*
270
271**phase:** *rewrite tail*
272
273Very much like [more_set_headers](#more_set_headers) except that it operates on input headers (or request headers) and it only supports the `-t` option.
274
275Note that using the `-t` option in this directive means filtering by the `Content-Type` *request* header, rather than the response header.
276
277Behind the scene, use of this directive and its friend [more_clear_input_headers](#more_clear_input_headers) will (lazily)
278register a `rewrite phase` handler that modifies `r->headers_in` the way you specify. Note that it always run at the *end* of
279the `rewrite` phase so that it runs *after* the standard [rewrite module](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html)
280and works in subrequests as well.
281
282If the `-r` option is specified, then the headers will be replaced to the new values *only if* they already exist.
283
284[Back to TOC](#table-of-contents)
285
286more_clear_input_headers
287------------------------
288**syntax:** *more_clear_input_headers [-t <content-type list>]... <new-header>...*
289
290**default:** *no*
291
292**context:** *http, server, location, location if*
293
294**phase:** *rewrite tail*
295
296Clears the specified input headers.
297
298In fact,
299
300```nginx
301
302    more_clear_input_headers -t 'text/plain' Foo Baz;
303```
304
305is exactly equivalent to
306
307```nginx
308
309    more_set_input_headers -t 'text/plain' "Foo: " "Baz: ";
310```
311
312or
313
314```nginx
315
316    more_set_input_headers -t 'text/plain' Foo Baz
317```
318
319To remove request headers "Foo" and "Baz" for all incoming requests regardless of the content type, we can write
320
321```nginx
322
323    more_clear_input_headers "Foo" "Baz";
324```
325
326See [more_set_input_headers](#more_set_input_headers) for more details.
327
328The wildcard character, `*`, can also be used at the end of the header name to specify a pattern. For example, the following directive
329effectively clears *any* input headers starting by "`X-Hidden-`":
330
331```nginx
332
333     more_clear_input_headers 'X-Hidden-*';
334```
335
336[Back to TOC](#table-of-contents)
337
338Limitations
339===========
340
341* Unlike the standard [headers](http://nginx.org/en/docs/http/ngx_http_headers_module.html) 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](http://nginx.org/en/docs/http/ngx_http_headers_module.html) module together with this module.
342* 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.
343
344[Back to TOC](#table-of-contents)
345
346Installation
347============
348
349Grab the nginx source code from [nginx.org](http://nginx.org/), for example,
350the version 1.17.8 (see [nginx compatibility](#compatibility)), and then build the source with this module:
351
352```bash
353
354 wget 'http://nginx.org/download/nginx-1.17.8.tar.gz'
355 tar -xzvf nginx-1.17.8.tar.gz
356 cd nginx-1.17.8/
357
358 # Here we assume you would install you nginx under /opt/nginx/.
359 ./configure --prefix=/opt/nginx \
360     --add-module=/path/to/headers-more-nginx-module
361
362 make
363 make install
364```
365
366Download the latest version of the release tarball of this module from [headers-more-nginx-module file list](https://github.com/openresty/headers-more-nginx-module/tags).
367
368Starting 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
369`./configure` command line above. And then you can explicitly load the module in your `nginx.conf` via the [load_module](http://nginx.org/en/docs/ngx_core_module.html#load_module)
370directive, for example,
371
372```nginx
373load_module /path/to/modules/ngx_http_headers_more_filter_module.so;
374```
375
376Also, this module is included and enabled by default in the [OpenResty bundle](http://openresty.org).
377
378[Back to TOC](#table-of-contents)
379
380Compatibility
381=============
382
383The following versions of Nginx should work with this module:
384
385* **1.17.x**                      (last tested: 1.17.8)
386* **1.16.x**
387* **1.15.x**                      (last tested: 1.15.8)
388* **1.14.x**
389* **1.13.x**                      (last tested: 1.13.6)
390* **1.12.x**
391* **1.11.x**                      (last tested: 1.11.2)
392* **1.10.x**
393* **1.9.x**                       (last tested: 1.9.15)
394* **1.8.x**
395* **1.7.x**                       (last tested: 1.7.10)
396* **1.6.x**                       (last tested: 1.6.2)
397* **1.5.x**                       (last tested: 1.5.8)
398* **1.4.x**                       (last tested: 1.4.4)
399* **1.3.x**                       (last tested: 1.3.7)
400* **1.2.x**                       (last tested: 1.2.9)
401* **1.1.x**                       (last tested: 1.1.5)
402* **1.0.x**                       (last tested: 1.0.11)
403* **0.9.x**                       (last tested: 0.9.4)
404* **0.8.x**                       (last tested: 0.8.54)
405* **0.7.x >= 0.7.44**             (last tested: 0.7.68)
406
407Earlier versions of Nginx like 0.6.x and 0.5.x will *not* work.
408
409If you find that any particular version of Nginx above 0.7.44 does not work with this module, please consider [reporting a bug](#report-bugs).
410
411[Back to TOC](#table-of-contents)
412
413Community
414=========
415
416[Back to TOC](#table-of-contents)
417
418English Mailing List
419--------------------
420
421The [openresty-en](https://groups.google.com/group/openresty-en) mailing list is for English speakers.
422
423[Back to TOC](#table-of-contents)
424
425Chinese Mailing List
426--------------------
427
428The [openresty](https://groups.google.com/group/openresty) mailing list is for Chinese speakers.
429
430[Back to TOC](#table-of-contents)
431
432Bugs and Patches
433================
434
435Please submit bug reports, wishlists, or patches by
436
4371. creating a ticket on the [GitHub Issue Tracker](https://github.com/chaoslawful/lua-nginx-module/issues),
4381. or posting to the [OpenResty community](#community).
439
440[Back to TOC](#table-of-contents)
441
442Source Repository
443=================
444
445Available on github at [openresty/headers-more-nginx-module](https://github.com/openresty/headers-more-nginx-module).
446
447[Back to TOC](#table-of-contents)
448
449Changes
450=======
451
452The changes of every release of this module can be obtained from the OpenResty bundle's change logs:
453
454<http://openresty.org/#Changes>
455
456[Back to TOC](#table-of-contents)
457
458Test Suite
459==========
460
461This module comes with a Perl-driven test suite. The [test cases](https://github.com/openresty/headers-more-nginx-module/tree/master/t/) are
462[declarative](https://github.com/openresty/headers-more-nginx-module/blob/master/t/sanity.t) too. Thanks to the [Test::Nginx](http://search.cpan.org/perldoc?Test::Nginx) module in the Perl world.
463
464To run it on your side:
465
466```bash
467
468 $ PATH=/path/to/your/nginx-with-headers-more-module:$PATH prove -r t
469```
470
471To run the test suite with valgrind's memcheck, use the following commands:
472
473```bash
474
475 $ export PATH=/path/to/your/nginx-with-headers-more-module:$PATH
476 $ TEST_NGINX_USE_VALGRIND=1 prove -r t
477```
478
479You need to terminate any Nginx processes before running the test suite if you have changed the Nginx server binary.
480
481Because 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 the `prove` utility.
482
483Some parts of the test suite requires modules [proxy](http://nginx.org/en/docs/http/ngx_http_proxy_module.html), [rewrite](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html), and [echo](https://github.com/openresty/echo-nginx-module) to be enabled as well when building Nginx.
484
485[Back to TOC](#table-of-contents)
486
487TODO
488====
489
490* Support variables in new headers' keys.
491
492[Back to TOC](#table-of-contents)
493
494Getting involved
495================
496
497You'll be very welcomed to submit patches to the [author](#author) or just ask for a commit bit to the [source repository](#source-repository) on GitHub.
498
499[Back to TOC](#table-of-contents)
500
501Authors
502=======
503
504* Yichun "agentzh" Zhang (章亦春) *&lt;agentzh@gmail.com&gt;*, OpenResty Inc.
505* Bernd Dorn ( <http://www.lovelysystems.com/> )
506
507This wiki page is also maintained by the author himself, and everybody is encouraged to improve this page as well.
508
509[Back to TOC](#table-of-contents)
510
511Copyright & License
512===================
513
514The code base is borrowed directly from the standard [headers](http://nginx.org/en/docs/http/ngx_http_headers_module.html) module in Nginx 0.8.24. This part of code is copyrighted by Igor Sysoev.
515
516Copyright (c) 2009-2017, Yichun "agentzh" Zhang (章亦春) <agentzh@gmail.com>, OpenResty Inc.
517
518Copyright (c) 2010-2013, Bernd Dorn.
519
520This module is licensed under the terms of the BSD license.
521
522Redistribution and use in source and binary forms, with or without
523modification, are permitted provided that the following conditions
524are met:
525
526* Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
527* Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
528
529THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
530"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
531LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
532A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
533HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
534SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
535TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
536PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
537LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
538NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
539SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
540
541[Back to TOC](#table-of-contents)
542
543See Also
544========
545
546* The original thread on the Nginx mailing list that inspires this module's development: ["A question about add_header replication"](http://forum.nginx.org/read.php?2,11206,11738).
547* The orginal announcement thread on the Nginx mailing list: ["The "headers_more" module: Set and clear output headers...more than 'add'!"](http://forum.nginx.org/read.php?2,23460).
548* The original [blog post](http://agentzh.blogspot.com/2009/11/headers-more-module-scripting-input-and.html) about this module's initial development.
549* The [echo module](https://github.com/openresty/echo-nginx-module) for Nginx module's automated testing.
550* The standard [headers](http://nginx.org/en/docs/http/ngx_http_headers_module.html) module.
551
552[Back to TOC](#table-of-contents)
553
554