set_rotate

The `set_rotate` directive allows you to implement a rotating selection among specified values in NGINX configurations.

Syntaxset_rotate variable name value1 value2 ...;
Defaultnone
Contexthttp, server, location, if in server, if in location
Arguments3

Description

The set_rotate directive is part of the NGINX Set-Misc module, which provides various functionalities including variable manipulation. It allows you to define a set of values from which NGINX can select one to assign to a variable. When set_rotate is executed, it iterates through the defined set of values, maintaining an internal state that enables it to pick the next value on each request sequentially, effectively 'rotating' through the list. This behavior allows for load balancing or performing round-robin actions based on defined values.

This directive requires three arguments: the name of the variable to set, the first value in the set, and one or more additional values. The directive will store the result in the specified variable, cycling through the list each time it's called within the same request context. This is particularly useful in situations where you want to distribute traffic across multiple backends, among other possible use cases.

The directive can be utilized within various contexts, including http, server, location, and if within those contexts, offering flexibility in its application across different configuration scopes. It is crucial to ensure that the order of values is well defined since the directive strictly follows the sequence provided.

Config Example

location /example {
    set_rotate $my_var "value1" "value2" "value3";
}

Make sure to define at least two values; otherwise, the rotation has no effect.

The internal state of the rotation is maintained on a per-request basis; new requests will start from the first defined value.

This directive does not persist the last selected value across requests; it resets on new requests.

← Back to all directives