What is the usage of add_header in nginx?

In nginx, the add_header directive is used to add custom HTTP headers to the response header. Its syntax is as follows:

Include the header with the specified name and value in all cases.

In this case, “name” represents the name of the HTTP header to be added, while “value” represents the corresponding value. The “always” parameter is optional and indicates whether the HTTP header should be added to all responses unconditionally.

The directive “add_header” can be used in various places such as within the http block, server block, and location block. Its scope of impact will vary depending on where it is used.

Use the add_header directive in the http block to add a specified HTTP header to the response of all requests.

In the server block, use the add_header directive to add the specified HTTP header to the responses of all requests under that server block.

In the location block, use the add_header directive to specify that the specified HTTP header should be added to the response of the requests under that location block.

For example, the Cache-Control header can be added using the add_header directive, as shown below:

This will add the Cache-Control header to the response of all requests.

bannerAds