http-var #

Defines nginx variables with functions for strings, numbers, encoding, hashing, time and IP addresses. Use it to compute values in the config instead of with Lua or njs; it works for HTTP and stream.

Source: upstream source

Directives

abs #

syntax: var $variable abs number;

Returns the absolute value of an integer or decimal number, preserving its textual representation apart from a leading minus sign. Errors: Invalid numeric input returns empty. The value without its decimal point must fit the native signed integer magnitude.

cidr #

syntax: var $variable cidr address ipv4_bits [ipv6_bits];

Returns the network address after applying the prefix length, without a /prefix suffix. IPv4 lengths are 1 through 32; IPv6 lengths are 1 through 128. If omitted, ipv6_bits uses the value of ipv4_bits. Both lengths are validated regardless of the address family.

crc32 #

syntax: var $variable crc32 value;

Returns the CRC32 checksum as eight lowercase hexadecimal digits.

extract_json #

syntax: var $variable extract_json [-i] json path;

Extracts a value from a JSON document. Object names are case-sensitive unless -i is used. The path supports dotted object names, zero-based array indices, and quoted object names in brackets. Errors: Invalid JSON, an invalid path, or a missing value returns empty. This function requires nginx 1.31.5 or later, or cJSON on earlier versions.

extract_param #

syntax: var $variable extract_param [-i] value separator delimiter name;

Returns the value of the first matching name, without surrounding spaces. The name must immediately precede the delimiter. Errors: An invalid separator or delimiter, an empty name after trimming, or no matching entry with a delimiter returns empty.

file #

syntax: var $variable file path;

Reads up to the first 1024 bytes of a file. Relative paths are resolved against the nginx prefix. Errors: Returns empty when the file cannot be opened or read, including missing files, permission failures, and directories.

gmt_time #

syntax: var $variable gmt_time [timestamp] format;

Formats a nonnegative timestamp in GMT/UTC. Omitting the timestamp uses the current time. http_time and cookie_time select nginx's HTTP and cookie date formats. A format of exactly %s returns the timestamp itself. Errors: Invalid timestamps, formatting failures, or a custom format or result of 2048 bytes or more return empty.

hexrand #

syntax: var $variable hexrand [length];

Returns length lowercase hexadecimal characters. The default length is 32; supported lengths are 1 through 32. Errors: On initial generation, an invalid length returns empty.

initcap #

syntax: var $variable initcap value;

Uppercases the first character of each word and lowercases the remaining letters. Words are sequences of ASCII letters and digits, separated by other bytes.

len #

syntax: var $variable len value;

Returns the byte length as a decimal integer.

local_time #

syntax: var $variable local_time [timestamp] format;

Formats a nonnegative timestamp in the nginx process's local timezone. Omitting the timestamp uses the current time. A format of exactly %s returns the timestamp itself. Errors: Invalid timestamps, formatting failures, or a format or result of 2048 bytes or more return empty.

position #

syntax: var $variable position [-i] value substring;

Returns the 1-based byte position of the first occurrence, or 0 if absent. An empty substring matches at position 1. -i ignores ASCII case.

rand #

syntax: var $variable rand start end;

Returns a random nonnegative decimal integer. With one argument, the inclusive range is 0 through end. With two arguments, it is start through end. Bounds must be nonnegative integers with start <= end. Without arguments, the function uses its default generator range.

regex_capture #

syntax: var $variable regex_capture [-i] value pattern replacement;

Matches the input and returns the expanded replacement using captures from the first match. Errors: A regex execution failure or no match returns empty.

regex_gsub #

syntax: var $variable regex_gsub [-i] value pattern replacement;

Replaces all non-overlapping matches, expanding the replacement separately for each match. Unmatched input is returned unchanged. Zero-length matches are supported. Errors: Regex execution or substitution failures return empty.

regex_sub #

syntax: var $variable regex_sub [-i] value pattern replacement;

Replaces the first match. Unmatched input is returned unchanged. Errors: Regex execution or substitution failures return empty.

repeat #

syntax: var $variable repeat value count;

Repeats the string count times. count is a nonnegative decimal integer. Errors: An invalid or out-of-range count returns empty.

replace #

syntax: var $variable replace [-i] value search replacement;

Replaces every non-overlapping literal match. If no match exists, the input is returned unchanged. -i ignores ASCII case in the search. Errors: An empty search string returns empty.

reverse #

syntax: var $variable reverse value;

Reverses the bytes of a string. Multibyte characters are not preserved.

round #

syntax: var $variable round number places;

Rounds to places fractional digits, padding with zeros when necessary. places must be a nonnegative decimal integer. Halfway values are rounded away from zero. Errors: Invalid decimal input, including a leading or trailing decimal point, returns empty. An invalid or out-of-range places also returns empty.

rsa_sign #

syntax: var $variable rsa_sign data key [padding=pss|pkcs1] [hash=name] [mgf1_hash=name] [salt_length=value] [password=value];

Signs the original message with a private key and returns a binary signature. Do not pass a precomputed digest as data. Hashes are sha1, sha256, sha384, or sha512. PSS salt length may be digest or a nonnegative integer. padding=pkcs1 does not accept mgf1_hash= or salt_length=.

rsa_verify #

syntax: var $variable rsa_verify data key signature [padding=pss|pkcs1] [hash=name] [mgf1_hash=name] [salt_length=value];

Verifies a binary signature with a public key. Returns 1 on success and 0 on failure. data is the original message. Defaults and parameters match rsa_sign, except that password= is unavailable and PSS verification also accepts salt_length=auto to detect the signature's salt length.

set #

syntax: var $variable set value;

Returns the expanded value.

substr #

syntax: var $variable substr value start [length];

Returns bytes starting at the 0-based start offset. If omitted, length extends to the end of the string. Both numbers must be nonnegative decimal integers. An excessive length is shortened to the remaining bytes. Errors: Invalid or out-of-range numbers, or a start at or beyond the end, return empty.

unescape_uri #

syntax: var $variable unescape_uri value;

Decodes %XX escape sequences using nginx URI unescaping. + remains a literal plus sign. Decoding stops after a literal or decoded ?, including that character in the result. Errors: Malformed escape sequences may be discarded or partially copied: % and %A produce empty output, while %GG produces GG.

unix_time #

syntax: var $variable unix_time value format [timezone];

Without arguments, returns the current Unix timestamp. Otherwise, parses the input using format. http_time uses nginx's HTTP date parser; custom formats use GMT by default. For a custom format, timezone specifies the source timezone as GMT, GMT+HHMM, or GMT-HHMM. The http_time form ignores it.

var #

syntax: var $variable function [-i] arguments...;  ·  default: —

HTTP context: http, server, location, when Stream context: stream, server, when Defines a variable whose value is calculated when it is read. Arguments can contain text, variables, or both. Function names, option names, -i, and regular expression patterns are fixed in the configuration. Functions can be composed through intermediate variables:

Example

var $file_signature rsa_sign $message keys/private.pem;
var $inline_signature rsa_sign $message data:$private_key_pem;
var $inline_valid rsa_verify $message data:$public_key_pem $inline_signature;

↑ back to index