In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces the relevant knowledge of how to configure the module of HTTP Headers in the Nginx server, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this article on how to configure the module of HTTP Headers in the Nginx server. Let's take a look.
one。 Preface
The ngx_http_headers_module module provides two important instructions, add_header and expires, to add the "expires" and "cache-control" header fields and any field fields to the response header. Add_header can be used to indicate which server the request is accessed to, and this can also be achieved through the nginx module nginx-http-footer-filter research. The expires instruction is used to control the browser's local cache.
two。 Add_header instruction
Syntax: add_header name value
Default value:-
Configuration segment: http, server, location, if in location
Add any field to the header field of the response message whose response code is 2005201pint 204pr 206ju 301pr 302pr 303pr 304, or 307. Such as:
Add_header from jb51.net
three。 Expires instruction
Syntax: expires [modified] time
Expires epoch | max | off
Default value: expires off
Configuration segment: http, server, location, if in location
Whether or not the addition and modification of "expires" and "cache-control" are enabled in the response code 2005201pint 204jpg 206jpg 301pr 302jpg 303j304, or 307in the header.
You can specify a positive or negative time value, and the time in the expires header is obtained according to the sum of the current time and the time specified in the instruction.
Epoch represents the absolute time since 00:00:01 gmt on January 1, 1970, and max specifies that the value of expires is 23 years, December 31, 2037.
The content of the cache-control header is specified with the preset time identifier:
Time value set to a negative number: cache-control: no-cache.
The time value set to a positive number or 0: cache-control: max-age = #, where the unit of # is seconds, specified in the instruction.
The parameter off forbids the modification of "expires" and "cache-control" in the reply header.
Example 1: for pictures, the flash file is cached locally in the browser for 30 days.
Location ~. *\. (gif | jpg | jpeg | png | bmp | swf) ${expires 30d;}
Example 2: cache js,css files locally in the browser for 1 hour
Location ~. *\. (js | css) ${expires 1h;}
Ngx_headers_more module
one。 Introduction to 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 include this module and needs to be added separately.
This module is an enhanced version of the ngx_http_headers_module module and provides more utilities, such as resetting or clearing built-in header information such as content-type, content-length, and server.
It allows you to use the-s option to specify the http status code, the-t option to specify the content type, and the more_set_headers and more_clear_headers instructions to modify the output header information. Such as:
More_set_headers-s 404-t 'text/html'' x-foo: bar'
The input header information can also be modified, such as:
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-t option can also be used in more_set_input_headers and more_clear_input_headers instructions.
Unlike standard header modules, the instructions of this module apply to all status codes, including 4xx and 5xx. Add_header is only applicable 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.gzcd nginx-1.5.8/ # here we assume you would install you nginx under / opt/nginx/../configure-- prefix=/opt/nginx\-- add-module=/path/to/headers-more-nginx-module
Makemake install
Download address of ngx_headers_more package: http://github.com/agentzh/headers-more-nginx-module/tags
Ngx_openresty includes this module.
three。 Instruction description
More_set_headers
Syntax: more_set_headers [- t] [- s]. ...
Default value: no
Configuration segment: http, server, location, location if
Phase: output header filter
When replacing (if any) or adding (if not all) the specified output header, the response status code matches the-s option and the type specified by the-t option of the content type of the response.
If no-s or-t is specified, or if there is an empty table value, there is no need to match. Therefore, for the following specification, any status code and any content type are set.
More_set_headers "server: my_server"
Response headers with the same name are always overridden. If you want to add headers, use the standard add_header directive instead.
Multiple output headers can be set / added by a single instruction. Such as:
More_set_headers' foo: bar' 'baz: bah'
Options can appear multiple times in a single instruction, such as:
More_set_headers-s 404-s' 500 503 'foo: bar'
Equivalent to:
More_set_headers-s' 404 500 503 'foo: bar'
The new head is one of the following forms:
Name: value
Name:
Name
The value of the last two header names that are effectively cleared. Variables for nginx are allowed to be header values, 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 server. The following configuration reports a syntax error:
# this is not allowed! Server {if ($args ~ 'download') {more_set_headers' foo: bar';}...} more_clear_headers
Syntax: more_clear_headers [- t] [- s]. ...
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
Equivalent to
More_set_headers-s 404-t 'text/plain' "foo:"baz:"
Or
More_clear_headers-s 404-t 'text/plain' foo baz
Equivalent 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 'xMushuddin'
Clearing starts with any output head by "xMuthhidden -".
More_set_input_headers
Syntax: more_set_input_headers [- r] [- t]. ...
Default value: no
Configuration segment: http, server, location, location if
Phase: rewrite tail
Very similar to more_set_headers, except that it works on the input header (or request header) and only supports the-t option.
Note: the-t option is used to filter the content-type of the request header, not the response header.
More_clear_input_headers
Syntax: more_clear_input_headers [- t] ...
Default value: no
Configuration segment: http, server, location, location if
Phase: rewrite tail
Clears the specified input header. Such as:
More_clear_input_headers-s 404-t 'text/plain' foo baz
Equivalent to
More_set_input_headers-s 404-t 'text/plain' "foo:"baz:"
Or
More_clear_input_headers-s 404-t 'text/plain' foo baz
Equivalent 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。 Limitations of ngx_headers_more
1. Unlike standard header modules, this module is not valid for the following headers: expires, cache-control, and last-modified.
two。 The response header for connection cannot be deleted using this module. The only way is to change the src/ http/ ngx_http_header_filter_module.c file.
five。 Use ngx_headers_more
# set the server output headermore_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 existsmore_set_input_headers-r 'x-foo: howdy'
six。 Apply ngx_headers_more
Modify what software and version of web server is, and hide centent-type, accept-range, content-length header information at the same time.
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:"
This is the end of the article on "how to configure HTTP Headers modules in the Nginx server". Thank you for reading! I believe that everyone has a certain understanding of the knowledge of "how to configure the module of HTTP Headers in the Nginx server". If you want to learn more knowledge, you are welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.