$arg_*

The variable $arg_ retrieves the value of a specific query parameter from the request URI. — NGINX Core (HTTP)

$arg_* NGINX Core (HTTP)

Description

In NGINX, variables prefixed with $arg_ are used to extract query parameters from the request URI. When a client makes an HTTP request, it may include a query string consisting of key-value pairs appended to the URL. For example, in the request URL `http://example.com/path?arg1=value1&arg2=value2`, NGINX allows access to these parameters through corresponding variables like `$arg_arg1` and `$arg_arg2`. The value returned by a specific `$arg_` variable corresponds to the value of the associated query parameter; if the parameter does not exist, the variable returns an empty string. These variables can be particularly useful in scenarios where request handling needs to be influenced by client parameters, such as redirects, access control, or conditional responses. They are evaluated each time a request is processed, making them versatile for dynamic content generation based on client input. Notably, because they are context-sensitive, their proper usage requires an understanding of NGINX configuration, especially regarding the placement of directives that depend on these variables.

Config Example

location /example {
    if ($arg_user_id) {
        return 200 "User ID: $arg_user_id";
    }
    return 400 "No User ID provided";
}

Subsystem

http

Cacheable

No

Type

Prefix variable

Contexts

http, server, location, if

Ensure that the query parameter is properly encoded in the request URL; otherwise, it may not be parsed correctly.

Using if statements with these variables can lead to unexpected behavior if not carefully structured; it’s best to use them within the context of a clean location block.