let

The 'let' directive allows defining variables by evaluating arithmetic and string operations in the NGINX configuration.

Syntaxlet variable_name expression;
Defaultnone
Contexthttp, server, location
Arguments1+

Description

The 'let' directive in NGINX provides a way to create variables based on evaluated expressions directly within the configuration. It supports a variety of arithmetic operations such as addition, subtraction, multiplication, division, and modulus, as well as string operations like concatenation. The use of parentheses is allowed to group expressions, enabling more complex calculations and string manipulations. It is important to follow the syntax rules, especially regarding spacing, as the directive uses NGINX's configuration parser as a lexer, which requires spaces around each operation and between tokens.

This directive can be used in several contexts: http, server, and location blocks. The directive expects one or more arguments, where the first argument is the variable to store the result, and subsequent arguments constitute the expression to be evaluated. The expression can include integers, hexadecimal values, other variables, and string literals. For instance, let $value (1 + 2); would correctly set $value to 3, given the proper spacing. If not formatted correctly, it can lead to syntax errors, which is a common pitfall for users relating to the spacing around operators.

Config Example

let $value ( $uid + 0x12 ) * $offset - 100;
let $remainder $number % 100;
let $welcome "Hi, " . $user . ", you have " . $num . " data items";

Ensure there are spaces around each token in the expression.

Using incorrect syntax, such as omitting spaces, will lead to configuration errors.

Arithmetic operations support only unsigned integers.

String concatenation is done using the '.' operator.

← Back to all directives