Implementation of Nginx operation response header information

Implementation of Nginx operation response header information

Prerequisite: You need to compile the ngx_http_headers_module module to support header information operations

add_header

It means to add custom header information to the response header. The instruction is add_header name value [always];. It can be used in http {}, server {}, location {}, if in location {} context.
This only takes effect if the response status code is 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). If the always parameter is specified, the header will be forced back in all responses regardless of the status code.

Note: You can declare multiple add_header instructions. If and only if the current block does not have add_header, the add_header information declared by the parent will be inherited.

For example:

server {
 add_header a 123;

 location / {
  root /path;
 }

 location /download/ {
  add_header b 321;
 }
}

In the example above, when accessing the /download/ route, a header such as b 321 will be returned, while when accessing the / route, a header such as a 123 will be returned as specified by the parent.

add_trailer

It means to add custom header information to the end of the response header. The instruction is add_trailer name value [always];. It can be used in the http {}, server {}, location {}, if in location {} context. It will only take effect when the response status code is equal to 200, 201, 206, 301, 302, 303, 307, or 308. If the always parameter is specified, the header information will be ignored in all responses.

Note: You can declare multiple add_trailer instructions. If and only if there is no add_trailer in the current block, the add_trailer information declared by the parent will be inherited.

See add_header for an example.

expires

Enable or disable, add or modify the "Expires" and "Cache-Control" fields in the response headers. The directives are expires [modified] time; or expires epoch | max | off; which takes effect when the response status code is 200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 (1.13.0). The time parameter can be a positive or negative time. The time in the "Expires" field is calculated as the sum of the current time and the time specified in time. If the modified parameter is used (0.7.0, 0.6.32), the time is calculated as the sum of the file modification time and the time specified in the time directive.

Additionally, a time of day can be specified using the "@" prefix (0.7.9, 0.6.34): expires @15h30m;

The content of the "Cache-Control" field depends on the symbol at the specified time:

  • Negative time - "Cache-Control: no-cache"
  • The time is a positive number or 0 - "Cache-Control: max-age=t" t is the time specified in the directive, in seconds

The epoch parameter sets "Expires" to the value "Thu, 01 Jan 1970 00:00:01 GMT" and "Cache-Control" to "no-cache".

The max parameter sets "Expires" to the value "Thu, 31 Dec 2037 23:55:55 GMT" and "Cache-Control" to 10 years.

The off parameter disables adding or modifying the "Expires" and "Cache-Control" response header fields.

The last argument value can contain variables (1.7.9): For example:

map $sent_http_content_type $expires {
 default off;
 application/pdf 42d;
 ~image/ max;
}

expires $expires;

The above is the full content of this article. I hope it will be helpful for everyone’s study. I also hope that everyone will support 123WORDPRESS.COM.

You may also be interested in:
  • Golang sets http response header and fills in the record
  • PHP download file, simple example of adding response header
  • How to obtain the real address and response header information after the short link jump in PHP

<<:  Detailed explanation of JavaScript progress management

>>:  MySQL 5.7.21 decompression version installation Navicat database operation tool installation

Recommend

Vue component communication method case summary

Table of contents 1. Parent component passes valu...

Analysis of the causes of accidents caused by Unicode signature BOM

Maybe you are using include files here, which is u...

Analysis of MySQL's planned tasks and event scheduling examples

This article uses examples to describe MySQL'...

The iframe frame sets the white background to transparent in IE browser

Recently, I need to frequently use iframe to draw ...

Solution for Docker Swarm external verification load balancing not taking effect

Problem Description I created three virtual machi...

Simple usage of MySQL temporary tables

MySQL temporary tables are very useful when we ne...

vue front-end HbuliderEslint real-time verification automatic repair settings

Table of contents ESLint plugin installation in H...

Detailed explanation of the spacing problem between img tags

IMG tag basic analysis In HTML5, the img tag has ...

Summary of data interaction between Docker container and host

Preface When using Docker in a production environ...

JavaScript to implement input box content prompt and hidden function

Sometimes the input box is small, and you want to...