$sent_http_*

The $sent_http_ prefix variable returns HTTP response headers sent to the client. — NGINX Core (HTTP)

$sent_http_* NGINX Core (HTTP)

Description

In NGINX, the $sent_http_ prefix variable is automatically generated when certain response headers are set in a server or location block. For each HTTP response header that is sent to the client, there exists a corresponding variable prefixed with $sent_http_. For example, if you send an HTTP header 'X-My-Header' from your server configuration, the corresponding variable will be $sent_http_x_my_header. These variables allow you to access the values of HTTP headers that have been sent in the response directly within NGINX configuration or log formats. This variable is particularly useful in scenarios where you need to perform conditional logging or when customizing output based on the values of specific headers. The $sent_http_ variables are only populated if the corresponding headers are actually set in the response. If a header has not been set, the corresponding variable will be empty. This mechanism helps with tracking and managing various header information that is crucial in scenarios such as debugging or verification of server behavior, especially under testing.

Config Example

location /example {
    add_header X-My-Header "MyValue";
    add_header X-Another-Header "AnotherValue";

    # Log sent headers
    access_log /var/log/nginx/example.log custom_format;
}

log_format custom_format 'Sent HTTP Headers: $sent_http_x_my_header, $sent_http_x_another_header';

Subsystem

http

Cacheable

Yes

Type

Prefix variable

Contexts

http, server, location, if

Ensure that the corresponding HTTP headers are actually set; otherwise, the variable will be empty.

The variable names must be in lowercase and underscores instead of hyphens, as NGINX transforms header names accordingly (e.g., X-My-Header becomes x_my_header).