Last active
September 27, 2019 19:29
-
-
Save Kelfitas/a54b7794b6b599752230052749ba14eb to your computer and use it in GitHub Desktop.
Nginx A\B testing config
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cloudflare config | |
map $http_cf_connecting_ip $connecting_ip { | |
default $http_cf_connecting_ip; | |
"" $remote_addr; | |
} | |
log_format versioned '[$upstream_group] $remote_addr - $remote_user [$time_local] ' | |
'"$request" $status $body_bytes_sent ' | |
'"$http_referer" "$http_user_agent"'; | |
map $cookie_app_version $upstream_group { | |
default $appversion; | |
"version_1" "version_1"; | |
"version_2" "version_2"; | |
} | |
upstream version_1 { | |
server 127.0.0.1:3000; | |
} | |
upstream version_2 { | |
server 127.0.0.1:3001; | |
} | |
split_clients "app${remote_addr}${http_user_agent}${date_gmt}" $appversion { | |
95% version_1; | |
* version_2; | |
} | |
server { | |
listen *:80; | |
server_name example.com; | |
access_log /var/log/nginx/app.access.log versioned; | |
error_log /var/log/nginx/app.error.log; | |
charset utf-8; | |
etag off; | |
gzip on; | |
set $app_version_root "dir1"; | |
if ($upstream_group = "version_1") { | |
set $app_version_root "dir1"; | |
} | |
if ($upstream_group = "version_2") { | |
set $app_version_root "dir2"; | |
} | |
root /var/www/$app_version_root; | |
location / { | |
add_header Set-Cookie "app_version=$upstream_group;Path=/;Max-Age=518400;"; | |
try_files $uri @frontend; | |
} | |
location @frontend { | |
proxy_buffers 8 32k; | |
proxy_buffer_size 64k; | |
proxy_http_version 1.1; | |
proxy_set_header X-FORWARDED-PROTO http; | |
proxy_set_header Upgrade $http_upgrade; | |
proxy_set_header Connection $http_connection; | |
proxy_set_header X-Real-IP $connecting_ip; | |
proxy_set_header X-Forwarded-For $connecting_ip; | |
# proxy_set_header X-Real-IP $remote_addr; | |
# proxy_set_header X-Forwarded-For $remote_addr; | |
proxy_set_header Host $host; | |
add_header Set-Cookie "app_version=$upstream_group;Path=/;Max-Age=518400;"; | |
if ($upstream_group = "version_1") { | |
proxy_pass http://127.0.0.1:3000; | |
break; | |
} | |
if ($upstream_group = "version_2") { | |
proxy_pass http://127.0.0.1:3001; | |
break; | |
} | |
proxy_pass http://$appversion; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment