$proxy_host

The variable $proxy_host contains the hostname of the upstream server being proxied to. — NGINX Core (HTTP)

$proxy_host NGINX Core (HTTP)

Description

In NGINX, the variable $proxy_host is integral in reverse proxy configurations that direct client requests to backend servers. When a request is proxied, this variable is set to the host part of the URL specified in the proxy_pass directive. It reflects the upstream server's hostname as determined by the proxy settings in the configuration file. The $proxy_host variable is available during request processing, particularly in contexts where a request is being proxied, such as inside a location block that uses the proxy_pass directive. If the upstream server is defined with a hostname or an IP address, $proxy_host will return that value, allowing for dynamic handling of requests based on the destination server's identity. Typical values for $proxy_host might be an actual hostname like "api.example.com" or an IP address if specified directly in the upstream definition. When using multiple upstream servers, this variable becomes crucial for logging and debugging purposes. It helps track which server a request was sent to, especially when dealing with load-balanced configurations. It's also useful in constructing custom headers or modifying requests based on the resolved upstream hostname.

Config Example

location /api {
    proxy_pass http://backend;
    proxy_set_header Host $proxy_host;
}

Subsystem

http

Cacheable

No

Contexts

http, server, location

$proxy_host will be empty if proxying is not configured correctly or if the upstream server is unreachable.

Ensure the proxy_pass directive is resolved to a valid upstream to avoid unexpected behaviors.