1 Answer

0 votes
by

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:

  1. 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,

  1. set $a hello;  
  2. 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:

VariableDescription
$ancient_browserThis variable is used to equal the value set by the ancient_browser_value directive, if a browser was identified as ancient.
$arg_nameName of the argument in the request line.
$argsList 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_sentNumber of bytes sent to the client, not counts the response header.
$bytes_receivedNumber 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_activeSame as Active connections value
$connections_readingSame as Reading value
$connections_waitingSame as the Waiting value
$connections_writingSame as the writing value.
$content_length"Content length" request header field.
$content_type"Content type" request header field
$cookie_nameThe name of cookie
$date_gmtCurrent time in GMT (Greenwich Mean Time). To set the format, use the config command with timefmt parameter.
$date_localCurrent time in the local time zone. To set the format, use the config command with timefmt parameter.
$document_rootValue of root or alias directive for the current request.
$document_uriIt is same as $uri.

Related questions

0 votes
asked Sep 5, 2019 in NGINX by Robin
0 votes
asked Sep 5, 2019 in NGINX by Robin
...