A, B testing with nginx

I needed to set up A-B testing with nginx.

Env: nginx is the front and loadbalancer and should spread traffic to two backend webservers. Also nginx in this case, running as two docker containers.

Traffic is entering at docker.labnet.nu and gets redirected to one or the other backend service. This method of load sharing redirects the same client IP to the same backend server all the time. It is done by hashing the clients IP and splitting all possible hashes into two parts (can be more), one containing 10% and the other gets the rest.

upstream _old_upstream {

upstream _new_upstream {

split_clients "${remote_addr}" $split_upstream {
10% _new_upstream;
* _old_upstream;

location / {
proxy_pass http://${split_upstream}${request_uri};
proxy_set_header Host $http_host;
proxy_set_header X-Forwarded-For $remote_addr;
proxy_set_header X-Forwarded-Ssl on;
proxy_set_header X-Forwarded-Proto https;

Started Docker containers like this:

oscar@docker01:~$ for i in {0..1} ; do sudo docker run --name nginx-4408${i} -p 4408${i}:80 -v /media/nfs/nginx/4408${i}:/usr/share/nginx/html:ro -d nginx && echo "Container nginx-4408${i} was started." ; done

Stop and remove container with:

oscar@docker01:~$ for i in {0..1} ; do sudo docker stop nginx-4408${i} && sudo docker rm nginx-4408${i} && echo "Container nginx-4408${i} whas stoped and removed." ; done

I test with https://geopeeker.com to see that both backends are used.


Leave a comment