add_header_inherit

The `add_header_inherit` directive allows inherited header directives to be applied at the specified context level in NGINX configuration.

Syntaxadd_header_inherit on | off;
Defaultoff
Contexthttp, server, location, if in location
Arguments1

Description

The add_header_inherit directive controls the inheritance of header directives set using add_header. When this directive is specified, it allows any add_header directives defined in a parent context (such as http or server) to be inherited by the child contexts (like location). This is particularly useful for ensuring consistency in headers across various levels of configuration without needing to redefine the headers in each context, thus simplifying the configuration management. The directive takes a single argument, which specifies whether to enable or disable this inheritance, with on allowing inheritance and off disallowing it.

By default, inheritance is disabled unless explicitly enabled. When inheritance is enabled, any headers added in a parent context will automatically be included in the responses from the child context, allowing the server to maintain consistent headers such as security headers, caching policies, or custom headers that you want to apply globally in a specific block of your configuration. This enhances both security and performance by reducing the redundancy of header definitions across multiple context blocks or locations.

Config Example

http {
    add_header X-Frame-Options "DENY";
    server {
        add_header_inherit on;
        location /api {
            # /api will inherit the X-Frame-Options header
        }
    }
}

If add_header_inherit is set to off, headers defined in parent contexts will not be inherited, potentially leading to missing headers in responses.

Ensure that add_header_inherit is defined at the correct context level to achieve the desired behavior of header inheritance.

← Back to all directives