$query_string

The $query_string variable contains the query string part of the client's request URI, excluding the leading '?'. — NGINX Core (HTTP)

$query_string NGINX Core (HTTP)

Description

The `$query_string` variable in NGINX captures the query string from the client's request URI. The query string is part of the URL that follows the '?' character and typically includes parameters and their values. For example, in the URL `http://example.com/index.html?name=John&age=30`, the query string is `name=John&age=30`. This variable is automatically populated by NGINX when a request is received and can be utilized in various contexts, particularly within location blocks where conditional logic is applied. This variable is useful for implementing logic based on the specific parameters passed in a query string. For instance, you can use `$query_string` in rewrites, access control, or logging configuration to direct requests based on their parameters. It is important to note that this variable is a simple string representation of the query string and does not parse its content; it is up to the administrator to handle parsing if more fine-grained control is needed over the parameters themselves.

Config Example

location /search {
    if ($query_string ~* "keyword=(.*)") {
        set $search_keyword $1;
        add_header Search-Keyword "$search_keyword";
    }
}

Subsystem

http

Cacheable

No

Contexts

http, server, location, if

Ensure your query string parameters do not contain sensitive information, as they can be logged or exposed in URLs.

Remember that the `$query_string` variable does not include the '?' character. This might lead to confusion when constructing URLs or redirects.