$location
The $location variable returns the name of the location block that matched the current request in the NGINX configuration. — NGINX CoolKit Module
Description
The $location variable in the NGINX CoolKit Module provides the name of the matched location block for the incoming request. It is evaluated during the handling of an HTTP request whenever NGINX processes location directives in its configuration. As the request traverses through the configuration, NGINX checks each location block to find the most specific match for the requested URI. Once a match is found, the name of the corresponding location block is stored in this variable. Typical values for the $location variable can be exact paths, such as '/auth', or more complex patterns defined by regular expressions. This functionality is beneficial for logging, conditional processing, and customizing responses based on the matched location. The $location variable is particularly useful in configurations where URL rewrites, or access controls, are dictated by the matched location. Being a non-cacheable variable, $location reflects real-time changes in the request-processing context. This ensures that it is dynamically updated as the NGINX processes requests against the defined location contexts, making it essential for scenarios where behavior must vary depending on the matched URI.
Config Example
http {
server {
location = /auth {
internal;
}
location / {
add_header X-Matched-Location $location;
proxy_pass http://backend;
}
}
}Subsystem
httpCacheable
NoContexts
http, server, location, ifEnsure that your location blocks are defined correctly to match desired URIs. Misconfigured blocks can lead to unexpected matches or no matches at all.
Avoid using $location in contexts where it might not yet be defined, such as in server or main configuration contexts.