$sent_http_content_type

The $sent_http_content_type variable holds the Content-Type header sent in the HTTP response from NGINX. — NGINX Core (HTTP)

$sent_http_content_type NGINX Core (HTTP)

Description

The $sent_http_content_type variable is a built-in NGINX variable that captures the value of the Content-Type HTTP header sent in the server's response. This variable is often used after a response has been generated, allowing you to access the Content-Type that was determined or set within the server's configuration or during request processing. When a request is processed, NGINX determines the appropriate Content-Type based on various factors such as the file extension of requested resources, content negotiation, or explicit settings in the configuration files. After the response is created and just before it is sent to the client, the $sent_http_content_type variable can be used to evaluate or log the outgoing Content-Type. It is not defined until the response header has been generated, and hence it might not be available for every request if the response is not valid or has not been set. Typical values for this variable could include standard MIME types such as "text/html", "application/json", "image/png", etc., depending on the content that NGINX serves. If no response is sent, or if the Content-Type is not specified, this variable would be empty. Therefore, it is recommended to check if it's set before using it in logging or decision-making.

Config Example

location / {
    add_header Custom-Header "$sent_http_content_type";
    proxy_pass http://backend;
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

The variable is not set until after response headers are created, so it cannot be used in pre-processing stages of a request.

If the response headers do not include a Content-Type, this variable will be empty. Ensure that your application or backend service sets it properly.