Last Updated: February 25, 2016
· willybarro

Split clients on nginx and pass a query string variable on the request

If you ever need to split your clients (e.g.: for a/b testing) and send a query string ($_GET) variable to your application, signaling something. You can do as follows:


# Split config
split_clients "${remote_addr}" $addQsVariable {
    51% "beta=1";
    49% "";

- For 51% of the visitors, set the variable $addQsVariable to "beta=1", otherwise, set it to an empty string.


location ~ \.php$
    set $args "${query_string}&${$addQsVariable}";

- When a php file is requested, append the variable $addQsVariable to the query string.

Think of splitclients as a function that receives a variable ($remoteaddr) as an argument that will be used to generate a percentage (based on a hash algorithm).

This percentage will be compared to those values you informed (lets take 51% as example) and set the $addQsVariable with the value just after the directive (beta=1).

Keep in mind, that if you provide a static input (like the IP variable $remote_addr), the user will send the same "beta=1" variable until he/she changes its IP.

  • For testing purposes, using "$query_string" as an input is OK, because you can change your browser url/query string to simulate percentages.

1 Response
Add your response


over 1 year ago ·