Nginx split_clients, set and Variable Evaluation Timing
Assign an experiment variant
Use split_clients in http when an application needs a deterministic partition of input strings into experiment groups. Nginx hashes the expanded input using MurmurHash2 and allocates ranges of the hash space to configured percentages. This is a distribution over keys, not a guarantee of exact proportions for a small sample of requests.
split_clients "lab-v1:$arg_demo" $variant { 25% new; * control;}location = /variant { default_type text/plain; return 200 "$variant\n";}The same demo value and unchanged configuration produce the same result. All requests with no demo argument also share the same input. A user can choose the query value; this is acceptable for a display experiment fixture, not permissions or billing. For real experiments choose an appropriate stable, privacy-reviewed identifier; IP addresses may be shared or change. Request IDs change between requests and therefore do not provide persistent assignment. MurmurHash2 is not a cryptographic security control.
Set a value at request time
location = /label { set $label study; set $label "${label}-ready"; default_type text/plain; return 200 "$label\n";}The rewrite module executes these set statements in order during request processing; expect study-ready. Use set in server, location or supported if context, not directly in http. This is a per-request value, not global state shared by visitors. Avoid treating the configuration as an imperative program: other modules run in their own phases.
Observe a cached and non-cacheable map
map $stage $cached_label { default unknown; first A; second B;}map $stage $fresh_label { volatile; default unknown; first A; second B;}location = /timing { set $stage first; set $before_cached $cached_label; set $before_fresh $fresh_label; set $stage second; default_type text/plain; return 200 "$before_cached/$cached_label $before_fresh/$fresh_label\n";}Predicted response: A/A A/B. Both variables are first needed while stage is first. The ordinary map retains its evaluated result; the volatile map can be reevaluated when used after the source changes. The example is an optional live assertion, not an observed Nginx result. Map evaluation is lazy: declaring a map does not mean every request evaluates it. Geo lookups are also evaluated on demand. volatile was added to map in 1.11.7; the analogous geo option requires 1.29.3, so do not use geo volatile on the older local 1.26.3 binary.
Keep configuration, templates and shell variables separate
cat <<'NGINX'return 200 "$request_method\n";NGINXThe quoted here-document delimiter prevents the shell from expanding $request_method. It prints the fragment to stdout; it does not edit a file. Template tools such as envsubst run before Nginx reads configuration and may replace dollar expressions if used without an explicit allowlist. Nginx request variables are expanded later by the directive that uses them. Check the rendered configuration before starting a lab.
Practice and next step
Send repeated requests with the same demo key and check that their labels agree, then try distinct synthetic keys. Do not require a fixed number of new assignments in ten requests. Compare the predicted timing result to an optional actual lab run. In the next chapter you will use these mental models to understand redirects and rewrite processing.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Variant and timing semantics
Choose the best answer for the chapter examples.
What is guaranteed for unchanged split_clients input and configuration?
Where is set directly allowed?
What is the predicted output of the timing fixture?
What minimum Nginx version is documented for geo volatile?
Why quote the shell here-document delimiter?