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

About the correct way to convert time in js when importing excel

Table of contents 1. Basics 2. Problem Descriptio...

Detailed tutorial on installing Nginx 1.16.0 under Linux

Because I have been tinkering with Linux recently...

Implementation of react loop data (list)

First, let's simulate the data coming from th...

Linux uses NetworkManager to randomly generate your MAC address

Nowadays, whether you are on the sofa at home or ...

How to use shell to perform batch operations on multiple servers

Table of contents SSH protocol SSH Connection pro...

A super detailed Vue-Router step-by-step tutorial

Table of contents 1. router-view 2. router-link 3...

Implementing carousel with native JavaScript

This article shares the specific code for impleme...

Implementation of MySQL custom list sorting by specified field

Problem Description As we all know, the SQL to so...

Linux CentOS MySQL database installation and configuration tutorial

Notes on installing MySQL database, share with ev...

MySQL 5.7.18 installation and configuration method graphic tutorial (CentOS7)

How to install MySQL 5.7.18 on Linux 1. Download ...

Let's talk about the problem of Vue integrating sweetalert2 prompt component

Table of contents 1. Project Integration 1. CDN i...