set_hashed_upstream
Sets a hashed upstream variable used for consistent hashing in NGINX.
Description
The set_hashed_upstream directive is part of the NGINX Set-Misc module and is used to compute a hash value based on upstream parameters, allowing for consistent routing of requests to upstream servers. This directive takes three arguments: the variable to set with the computed hash, a string that determines the hashing algorithm, and an optional argument that can be tied to specific upstream configurations or behaviors. The directive thus facilitates load balancing and ensures that requests with the same upstream parameters are consistently directed to the same upstream server, regardless of the number of servers available or added.
When using set_hashed_upstream, you can influence the way requests are distributed across your upstream servers. This is particularly useful in scenarios where you have sticky sessions or need to maintain session state consistently across requests from the same client. The hashing function can help ensure that clients consistently hit the same backend server, which can minimize issues such as session loss or state mismatch if there's session data stored on the specific server. This makes the directive valuable in high-availability or distributed application setups.
Config Example
set_hashed_upstream $hashed_upstream "my_request_id";
Make sure the hashing algorithm string is specified correctly; otherwise the behavior may be unpredictable.
Avoid using this directive within a 'if' context without understanding the implications, as NGINX may not always behave as expected.
Ensure that the variable being set is not also used in the upstream block, as this could lead to circular dependencies.