$content_type

The $content_type variable in NGINX holds the value of the Content-Type header of the HTTP response. — NGINX Core (HTTP)

$content_type NGINX Core (HTTP)

Description

The $content_type variable in NGINX is automatically set when processing a response to represent the Content-Type header of that response. Typically, it is used to indicate the media type of the resource being returned, following the format defined by RFC 6838. Common values include 'text/html' for HTML documents, 'application/json' for JSON responses, and 'image/png' for PNG images. This variable is set during the request processing phase and can be manipulated by various directives that influence the response, such as 'add_header', 'default_type', and 'types'. When NGINX serves static files, the Content-Type is determined based on the file extension and configuration settings defined under the 'http' block or within specific 'server' or 'location' contexts. For example, if a request is made for a file with a '.html' extension, the $content_type would typically be set to 'text/html'. In cases where no explicit type is set, NGINX defaults to the value provided by the 'default_type' directive if available, otherwise it may be left unset.

Config Example

location /images/ {
    types {
        image/png png;
        image/jpeg jpg;
        image/gif gif;
    }
    default_type application/octet-stream;
    add_header Content-Type $content_type;
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

If no type is matched, $content_type may be empty, leading to unexpected responses.

Custom types should be declared correctly in the 'types' block to ensure proper assignment of $content_type.

Using $content_type in a context that doesn't deal with responses may lead to irrelevant values.