$sent_http_location

当发生内部重定向时,$sent_http_location 变量包含响应中发送的 HTTP 'Location' 首部。 — NGINX Core (HTTP)

$sent_http_location NGINX Core (HTTP)

说明

$sent_http_location 变量在 NGINX 的内部重定向期间被赋值,尤其是在模块处理导致 3xx HTTP 响应的请求时。该变量被赋予由指令(例如 `rewrite`、`error_page` 或 `proxy_redirect`)设置的 'Location' 首部的值。它保存了客户端将被重定向到的 URL,这在基于用户输入或请求路径应用访问控制或逻辑时特别有用。 只有在发生指向不同 URI 的内部重定向时该变量才会被设置,并且它可以在多种场景中使用,包括记录请求最终被重定向到的 URL,或与其他 NGINX 变量结合用于更高级的响应逻辑。典型值包括绝对 URL(例如 "http://example.com/target")或相对 URI(例如 "/target"),具体取决于 NGINX 服务器中重定向的配置。

配置示例

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

子系统

http

可缓存

上下文

http, server, location, if

$sent_http_location 仅在内部重定向期间设置,如果没有发生重定向,可能不会包含值。

请确保 Location 头部确实已设置;否则,该变量将为空。

该变量在初始请求处理上下文中不可用,仅在响应重定向之后才相关。