Skip to content

Instantly share code, notes, and snippets.

@kmjones1979
Last active November 28, 2017 22:48
Show Gist options
  • Save kmjones1979/1650488265b02d93ffe4e68d1b1666a6 to your computer and use it in GitHub Desktop.
Save kmjones1979/1650488265b02d93ffe4e68d1b1666a6 to your computer and use it in GitHub Desktop.
This is an example NGINX configuration for the blog: Performing A/B Testing with NGINX - This example demonstrates the sticky route functionality
events { worker_connections 2014; }
http {
default_type text/plain;
log_format cookie '$cookie_route - $route_cookie, '
'$request_uri - $route_uri, '
'$route - $upstream_addr';
error_log /var/log/nginx/debug_error.log debug;
server {
listen 8098;
status_zone frontend;
return 200 "Cookie Value: $cookie_route
Request URI: $request_uri
Results: Site A - Running on port 8089\n";
}
server {
listen 8099;
status_zone frontend;
return 200 "Cookie Value: $cookie_route \n
Request URI: $request_uri
Results: Site B - Running on port 8099\n";
}
map $cookie_route $route_cookie {
~\.(?P<route>\w+)$ $route;
}
map $arg_route $route_uri {
~\.(?P<route>\w+)$ $route;
}
upstream backend {
zone backend 64k;
server 127.0.0.1:8098 route=a;
server 127.0.0.1:8099 route=b;
sticky route $route_cookie $route_uri;
}
server {
listen 80;
access_log /var/log/cookie.log cookie;
location / {
proxy_pass http://backend;
proxy_set_header X-Forwarded-For $remote_addr;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment