$http_host

The `$http_host` variable contains the value of the Host header sent by the client in the HTTP request. — NGINX Core (HTTP)

$http_host NGINX Core (HTTP)

Description

The `$http_host` variable retrieves the value of the Host header as specified in the client's HTTP request. This variable is set during the request processing phase when NGINX parses the incoming headers. Its primary purpose is to allow the server to know which domain or subdomain the client is trying to reach, which is critical for proper routing and serving of requests in environments where multiple sites are hosted on a single server (called virtual hosting). Typically, the value of `$http_host` can be either the fully qualified domain name (FQDN) or an IP address, and it can sometimes include a port number if specified by the client. Here are some potential examples of what `$http_host` might contain: "www.example.com", "example.com", or "192.168.1.1:8080" if the client explicitly included a port in the request header. If the Host header is absent from the request, NGINX may fallback to the default server name, if configured, or the behavior may lead to errors, depending on the rest of the configuration.

Config Example

server {
    listen 80;
    server_name example.com;

    location / {
        return 200 "Host: $http_host";
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

Ensure that the Host header is not stripped by proxy servers or load balancers before reaching NGINX, as this will result in the variable being empty.

The value of `$http_host` might include a port number if it is explicitly sent by the client, which could cause unexpected behavior if not handled correctly.