To explain the use of HTTP headers-related module configuration in Nginx server _nginx

Source: Internet
Author: User
Tags current time ranges response code nginx server

The

ngx_http_headers_module module
I. Preface
ngx_http_headers_module module provides two important instructions Add_header and expires To add the "Expires" and "Cache-control" header fields, add any field fields to the response header. Add_header can be used to indicate which server the request accesses, and this can also be done through the Nginx module Nginx-http-footer-filter research use. The expires instruction is used to control the browser's local cache.
two. add_header directive
Syntax: add_header name value;
Default:-
Configuration segment: HTTP, server, location, if in Loc ation
Adds an arbitrary field to the response Message header field of response code 200,201,204,206,301,302,303,304, or 307. For example,

Add_header from Jb51.net
three. Expires directive
syntax: Expires [modified] time;
Expires Epoch | Max | Off
Default value: Expires off;
Configuration segment: HTTP, server, location, if in location
Add and modify actions for "Expires" and "Cache-control" in response code 200,201,204,206,301,302,303,304, or 307 headers.
You can specify a positive or negative time value, and the time in the expires header is obtained based on the current time and the time specified in the instruction.
Epoch represents the absolute time of the 00:00:01 GMT from January 1, 1970, and Max specifies that the value of expires is years for December 31, 2037 23:59:59,cache-control.
The contents of the Cache-control header are specified with the preset time ID:
• Time value set to negative: Cache-control:no-cache.
• Time value set to positive or 0: Cache-control:max-age = #, where # is a unit of seconds, specified in the instruction.
Parameter off disables the modification of "Expires" and "Cache-control" in the answer header.
Example one: For pictures, flash files in the browser local cache 30 days

Location ~. *\. (gif|jpg|jpeg|png|bmp|swf) $
 {
      expires 30d;
 }

Instance two: 1 hours cached for js,css files in the browser

Location ~. *\. (JS|CSS) $
 {
      expires 1h;
 }

Ngx_headers_more Module
I. Introduction of Ngx_headers_more
Ngx_headers_more is used to add, set, and clear header information for input and output. The Nginx source code does not contain the module and needs to be added separately.
The module is an enhanced version of the Ngx_http_headers_module module, providing additional utilities such as resetting or removing built-in header information, such as Content-type, Content-length, and server.
You can allow you to specify the HTTP status code using the-S option, specify the content type with the-t option, and modify the output header information by more_set_headers and more_clear_headers directives. Such as:

More_set_headers-s 404-t ' text/html ' X-foo:bar ';

The input header information can also be modified like this:

Location/foo {
  more_set_input_headers ' host:foo ' user-agent:faked ';
  # now $host, the $http _host, $user _agent, and
  #  $http _user_agent all have their new values.
}

The-t option can also be used in more_set_input_headers and more_clear_input_headers directives.
Unlike standard header modules, the module's instructions apply to all status codes, including 4xx and 5xx. Add_header only applies to 200,201,204,206,301,302,303,304, or 307.

Two. Install Ngx_headers_more

wget ' http://nginx.org/download/nginx-1.5.8.tar.gz '
tar-xzvf nginx-1.5.8.tar.gz
cd nginx-1.5.8/
 
# here We assume you to would install you nginx under/opt/nginx/.
/configure--prefix=/opt/nginx \
  --add-module=/path/to/headers-more-nginx-module


Make make
Install

Ngx_headers_more Package Download Address: http://github.com/agentzh/headers-more-nginx-module/tags
Ngx_openresty contains the module.
three. Instruction description
more_set_headers
Syntax: more_set_headers [-t <content-type List>] [-S <status-code list>] ... <new-header>
Default value: No
Configuration segment: HTTP, server, location, location if
Phase: Output Header Filter
The response status code matches the type specified by the-t option of the content type that matches and responds with the-s option, if any, or if the specified output header is incremented (if not all).
If-s or-t is not specified, or there is an empty table value, there is no need to match. Therefore, for the following designation, any status code and any content type are set.

More_set_headers  "Server:my_server";

A response header with the same name is always overwritten. If you want to add headers, you can use the standard Add_header directives instead.
A single instruction can set/Add multiple output headers. Such as:

More_set_headers ' Foo:bar ' Baz:bah ';

In a single instruction, options can appear multiple times, such as:

 More_set_headers-s 404-s ' 503 ' foo:bar ';

Equivalent to:

 More_set_headers-s ' 404 503 ' foo:bar ';

The new head is one of the following forms:

    • Name:value
    • Name:
    • Name

The last two values of the header names that are effectively purged. The Nginx variable allows a header value, such as:

Set $my _var "Dog";
More_set_headers "Server: $my _var";

Note: More_set_headers is allowed in the If block of location, but not in the if block of the server. The following configuration reports syntax errors:

 # This are not allowed!
 server {
    if ($args ~ ' Download ') {
      more_set_headers ' Foo:bar ';
    }
    ...
  }
More_clear_headers

Syntax: more_clear_headers [-t <content-type List>] [-S <status-code list>] ... <new-header>
Default value: No
Configuration segment: HTTP, server, location, location if
Phase: Output Header Filter
Clears the specified output header.

More_clear_headers-s 404-t ' Text/plain ' Foo Baz;

Equal to

More_set_headers-s 404-t ' Text/plain ' "Foo:" "Baz:";

Or

More_clear_headers-s 404-t ' Text/plain ' Foo Baz;

Equal to

More_set_headers-s 404-t ' Text/plain ' "Foo:" "Baz:";

Or

More_set_headers-s 404-t ' Text/plain ' Foo Baz

You can also use the wildcard character *, such as:

More_clear_headers ' x-hidden-* ';

Purge starts with any output header from "x-hidden-".
More_set_input_headers
Syntax: More_set_input_headers [-R] [-t <content-type List>] ... <new-header>
Default value: No
Configuration segment: HTTP, server, location, location if
Stage: Rewrite tail
Very similar to more_set_headers, the difference is that it works on the input header (or the request header), and it supports the-t option only.
Note: the-t option is used to filter the content-type of the request headers, not the response headers.
More_clear_input_headers
Syntax: more_clear_input_headers [-t <content-type List>] ... <new-header>
Default value: No
Configuration segment: HTTP, server, location, location if
Stage: Rewrite tail
Clears the specified input header. Such as:

More_clear_input_headers-s 404-t ' Text/plain ' Foo Baz;

Equal to

More_set_input_headers-s 404-t ' Text/plain ' "Foo:" "Baz:";

Or

More_clear_input_headers-s 404-t ' Text/plain ' Foo Baz;

Equal to

More_set_input_headers-s 404-t ' Text/plain ' "Foo:" "Baz:";

Or

More_set_input_headers-s 404-t ' Text/plain ' Foo Baz

Four. Ngx_headers_more Limitations
1. Unlike the standard header module, the module does not work on the following headers: Expires, Cache-control, and last-modified.
2. You cannot delete the connection response header using this module. The only way to do this is to change the src/http/ngx_http_header_filter_module.c file.
Five. Use of Ngx_headers_more

 # Set the Server output header more_set_headers ' server:my-server ';
  # set and clear output headers 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 ' 404 503 ' s 413 ' foo:bar ';
 
  More_clear_headers ' Content-type ';
 
# your proxy_pass/memcached_pass/or any other config goes ...}
  # Set Output headers Location/type {more_set_headers ' content-type:text/plain ';
 
# ...
}
  # Set Input headers 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 '; 

Six. Application Ngx_headers_more
Modify the Web server what software, what version, while hiding Centent-type, Accept-range, content-length header information.

More_set_headers "Server:jb51.net Web Server";
More_clear_headers "Content-type:";
More_clear_headers "Accept-ranges:";
More_clear_headers "Content-length:";

404 Status Code Add header
The configuration is as follows:

More_set_headers "Server:jb51.net Web Server";
More_set_headers-s 404 "Error:not found";
More_clear_headers "Content-type:";
More_clear_headers "Accept-ranges:";
More_clear_headers "Content-length:";

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.