Variables are the natural part of the programming languages. These are just containers holding various values in imperative languages like Perl, C/C++, Bourne shell, Perl. And values can be strings like "hello world", numbers like "3.14", or even complicated things like references to arrays or hash tables in those languages.
For the Nginx configuration language, however, variables can hold only one type of values, i.e. strings. But there is an interesting exception: the 3rd party module ngx_array_var extends Nginx variables to contain arrays, but it is implemented by encoding a C pointer as a binary string value behind the scene.
Variable syntax
Consider that our nginx.conf configuration file which has the following line:
- set $a "hello world";
Here, we assign a value to the variable "$a" through the set configuration directive which is coming from the standard nginx_rewrite module. And here we assign the string value "hello world" to "$a".
From the above example, we can see that the Nginx variable name takes a dollar sign ($) in front of it. Hence, we can say that whenever we want to add a reference to an nginx variable in the configuration file, we must add a dollar ($) prefix.
Let's see another simple example,
- set $a hello;
- set $b "$a, $a";
From the above example, we can see that variable $a is used to construct the value for the variable $b. So after these two directives complete execution, the value of $a is "hello", and $b is "hello, hello". This technique is called "variable interpolation".
Let's see the list of variables in Nginx:
Variable | Description |
---|
$ancient_browser | This variable is used to equal the value set by the ancient_browser_value directive, if a browser was identified as ancient. |
$arg_name | Name of the argument in the request line. |
$args | List of arguments on the request line. |
$binary_remote_addr (ngx_http_core_module) | Client address in the form of binary. Length of value is always 4 bytes for IP4 addresses or 16 bytes for IPv6 addresses. |
$binary_remote_addr (ngx_stream_core_module) | Client address in the form of binary. Length of value is always 4 bytes for IP4 addresses or 16 bytes for IPv6 addresses. |
$body_bytes_sent | Number of bytes sent to the client, not counts the response header. |
$bytes_received | Number of bytes received from a client. |
$bytes_sent (ngx_http_core_module) | Number of bytes sent to a client. |
$bytes_sent (ngx_http_log_module) | Number of bytes sent to a client. |
$bytes_sent (ngx_stream_core_module) | Number of bytes sent to a client. |
$connection (ngx_http_core_module) | connection serial number |
$connection (ngx_http_log_module) | connection serial number |
$connection (ngx_stream_core_module) | connection serial number |
$connection_requests (ngx_http_core_module) | Current number of requests made via connection. |
$connection_requests (ngx_http_log_module) | Current number of requests made via connection. |
$connections_active | Same as Active connections value |
$connections_reading | Same as Reading value |
$connections_waiting | Same as the Waiting value |
$connections_writing | Same as the writing value. |
$content_length | "Content length" request header field. |
$content_type | "Content type" request header field |
$cookie_name | The name of cookie |
$date_gmt | Current time in GMT (Greenwich Mean Time). To set the format, use the config command with timefmt parameter. |
$date_local | Current time in the local time zone. To set the format, use the config command with timefmt parameter. |
$document_root | Value of root or alias directive for the current request. |
$document_uri | It is same as $uri. |