$sent_http_location

The $sent_http_location variable contains the HTTP Location header sent in the response when an internal redirection occurs. — NGINX Core (HTTP)

$sent_http_location NGINX Core (HTTP)

Description

The $sent_http_location variable is populated during internal redirection in NGINX, specifically when the module processes a request that results in a 3xx HTTP response. This variable is assigned the value of the 'Location' header set by directives such as `rewrite`, `error_page`, or `proxy_redirect`. It holds the redirected URL that the client will be sent to, which is particularly useful in handling situations where access control or logic is applied based on user inputs or request paths. The variable is only set if an internal redirection to a different URI takes place, and it can be utilized in various contexts, including logging the final URL to which a request is redirected or using it in conjunction with other NGINX variables for advanced response logic. Typical values include absolute URLs (e.g., "http://example.com/target") or relative URIs (e.g., "/target") depending on how redirection is configured in the NGINX server.

Config Example

server {
    listen 80;
    location /example {
        return 302 /new-location;
    }
    location /new-location {
        add_header Location $sent_http_location;
        return 200 'Redirected to new location';
    }
}

Subsystem

http

Cacheable

Yes

Contexts

http, server, location, if

$sent_http_location is only set during internal redirects, and may not contain a value if there's no redirection involved.

Ensure that the Location header is actually set; otherwise, this variable will be empty.

This variable is not available in the initial request processing context and will be relevant only after responding to redirection.