by Nitish Phanse

由Nitish Phanse

在40行以下代码中使用NGINX进行A / B测试 (A/B testing with NGINX in under 40 lines of code)

A/B Testing, has enabled designers and product managers to get a deep insight into user behavioral patterns.

A / B测试使设计师和产品经理可以深入了解用户的行为模式。

On the one hand, it has allowed product managers more flexibility while conceptualizing user journeys. But on the other, it’s become a developer’s nightmare, being told to make two versions of the same component.

一方面,它使产品经理在概念化用户旅程的同时具有更大的灵活性。 但另一方面,它被告知要制作同一组件的两个版本,这已成为开发人员的噩梦。

“The general concept behind A/B testing is to create an experiment with a control group and one or more experimental groups (called “cells” within Netflix) which receive alternative treatments. Each member belongs exclusively to one cell within a given experiment, with one of the cells always designated the “default cell”. This cell represents the control group, which receives the same experience as all Netflix members not in the test.”

“ A / B测试背后的一般概念是与对照组和一个或多个接受替代治疗的实验组(在Netflix中称为“细胞”)建立实验。 每个成员在给定实验中仅属于一个单元,其中一个单元始终指定为“默认单元”。 这个单元代表对照组,与未参加测试的所有Netflix成员一样,他们将获得相同的体验。”

- Netflix blog


当前的生态系统能提供什么? (What does the present ecosystem offer?)

A lot of companies like Mixpanel, VWO, and Optimisely provide client SDKs (JavaScript code) which have to be added in the head tag of the page HTML. The tests can then be created via a dashboard.

很多公司(例如Mixpanel,VWO和Optimizely)都提供了客户端SDK(JavaScript代码),这些必须添加到页面HTML的head标签中。 然后可以通过仪表板创建测试。

Although the above methods give you a lot of options when it comes to button colors, component height, and other CSS attributes, it doesn’t really allow you to create two separate flows altogether.


Also, some external libraries can really hamper your page load times and can create a jittery/laggy experience for users.


介绍NGINX (Presenting NGINX)

Nginx is a light web server that offers a bunch of functionality like load balancing, reverse proxying, and HTML compression. It’s easy to setup and really offers a lot of control to developers.

Nginx是一个轻量级的Web服务器,提供了很多功能,例如负载平衡,反向代理和HTML压缩。 它易于设置,确实为开发人员提供了很多控制。

NGINX is a terrific tool for distributing traffic for split tests.


It’s stable, it’s blazingly fast, and configurations for typical use cases are prevalent online. More complex configuration can be accomplished after just a couple hours exploring the documentation. Small companies may not have resources to spend on paid software for A/B testing, but NGINX provides an option to carry out some form of A/B testing.

它稳定,速度极快,典型用例的配置在网上很普遍。 在浏览文档仅几个小时之后,即可完成更复杂的配置。 小型公司可能没有资源来购买用于A / B测试的付费软件,但是NGINX提供了进行某种形式的A / B测试的选项。

For example, say you want to see which of the forms below will have better conversion:


Your hypothesis might be that fewer form fields imply less data being inputted by the user, thus leading to more conversions.


So we can define two buckets: Version A and Version B. The former is the control group, which is shown to 80% of the traffic. The latter is the test group, which forms the remaining 20% of the traffic.

因此,我们可以定义两个存储桶:版本A 和版本B。前者是对照组,显示为80%的流量。 后者是测试组,占剩余流量的20%。

Port 7770 will host one bucket of the code, whereas port 7777 will host the second bucket of the code.


码 (Code)

Your nginx.conf file can be modified as shown below.


http {    # ...    # application version 1a    upstream version_a {        server server; ## Can be an external ip too    }
# application version 1b    upstream version_b {        server server; ## Can be an external ip too    }
split_clients "app${remote_addr}${http_user_agent}${date_gmt}"   $appversion {        80%     version_1a;        *       version_1b;    }
server {        # ...        listen 80;        location / {            proxy_set_header Host $host;            proxy_pass http://$appversion;        }    }}

Create two upstreams, one for each bucket.


The split_client directive helps us divert traffic along with the specified weightage to a particular upstream.

split_client 指令可帮助我们将流量和指定的权重转移到特定的上游。

The app${remote_addr}${http_user_agent}${date_gmt}appversion creates a hash based on the above parameters and is used by nginx to log a request made to either bucket. Preferably these parameters are those which are pertaining solely to a user, like user_agent, remote addr.

app${remote_addr}${http_user_agent}${date_gmt}appversion 根据上述参数创建一个哈希,nginx使用该哈希记录对任一存储桶发出的请求。 优选地,这些参数是仅与用户有关的那些参数,例如user_agentremote addr

Ok — so this will work, but it doesn’t give the user a persistent experience.


If I refresh my page, there is a chance I’ll switch between buckets, and this can be a horrid user experience.


Consider the above case: imagine trying to fill a six field form, and then suddenly, on refreshing, seeing a two field form. Confusing!

考虑上面的情况:假设尝试填写一个六字段表单,然后在刷新时突然看到一个两字段表单。 令人困惑!

不同的方法 (A different approach)

  1. Proxy pass request to either bucket代理通过请求到任一存储桶
  2. Set a cookie with an expiration time equal to duration of test.设置cookie的有效时间等于测试持续时间。
  3. Check for cookie existence and proxy pass to correct bucket to ensure a uniform user experience.检查cookie的存在和代理传递以更正存储桶,以确保统一的用户体验。

We will use NGINX’s map directive and map the $cookie_name variable to different buckets that we have created.

我们将使用NGINX的map指令并映射$cookie_name 变量以创建不同的存储桶。

http {    # ...    # application version a    upstream version_a {        server server; ## Can be an external ip too    }
# application version b    upstream version_b {        server server; ## Can be an external ip too    }    split_clients "app${remote_addr}${http_user_agent}${date_gmt}"     $appversion {        80%     version_a;        *       version_b;    }
map $cookie_split_test_version $upstream_group {        default $appversion;        "version_a" "version_a";        "version_b" "version_b";    }
server {        # ...        listen 80;        location / {            add_header Set-Cookie "split_test_version=$upstream_group;Path=/;Max-Age=518400;";
proxy_set_header Host $host;
if ($upstream_group = "version_a") {                proxy_pass;                break;            }
if ($upstream_group = "version_b") {                proxy_pass;                break;            }
proxy_pass http://$appversion;        }    }}

As it’s a little hard to format the above code…


结论 (Conclusion)

  1. NGINX provides a very simple API to create an A/B test environment.NGINX提供了一个非常简单的API来创建A / B测试环境。
  2. Allows for multiple buckets to be created. The example above shows two buckets, but we can split traffic and create more buckets.允许创建多个存储桶。 上面的示例显示了两个存储桶,但我们可以拆分流量并创建更多存储桶。
  3. As the same code is hosted on two ports, deployment can become tricky (presently I have two branches: a master and a test branch), whether done off a different branch or from the same one.由于将相同的代码托管在两个端口上,因此部署可能会变得很棘手(目前我有两个分支:一个主分支和一个测试分支),无论是在其他分支上进行还是从同一分支进行。
  4. Carrying more than one A/B test can become tricky. Yes, you can use the location directive and set different cookies based on the required tests, but having two tests (Test 1, Control: 80, Test 20 & Test 2 Control: 50, Test 50) is impossible. That said, you should not have more than one A/B test at a time per page. Otherwise you will end up having 2^n versions of your page, where n is the number of tests, and tracking conversions will be hell.

    进行多个A / B测试可能会很棘手。 是的,您可以使用location指令并根据所需的测试设置不同的cookie,但是不可能有两个测试( Test 1,Control:80,Test 20&Test 2 Control:50,Test 50 )。 就是说,您每页一次不能进行多个A / B测试。 否则,您最终将拥有2 ^ n个页面版本,其中n是测试数量,并且跟踪转换将是地狱。

  5. Tracking can now be done at a very granular level as the code bases are effectively separate.由于代码库是有效分离的,因此现在可以在非常精细的级别上进行跟踪。

Do let me know if I’ve made any mistake in the above. Happy to correct and learn. Hope you liked the article.

如果我在上面有任何错误,请告诉我。 乐于纠正和学习。 希望您喜欢这篇文章。

PS: Did anyone notice it is less than 40 lines of code?!




测试nginx网站代码_在40行以下代码中使用NGINX进行A / B测试相关推荐

  1. 聚类 python 代码_不足 20 行 Python 代码,高效实现 k-means 均值聚类算法

    下载好向圈APP可以快速联系圈友 您需要 登录 才可以下载或查看,没有帐号?立即注册 x 不足 20 行 Python 代码,高效实现 k-means 均值聚类算法-1.jpg (143.81 KB, ...

  2. 50行python游戏代码_使用50行Python代码从零开始实现一个AI平衡小游戏

    使用50行Python代码从零开始实现一个AI平衡小游戏 发布时间:2020-10-23 09:26:14 来源:脚本之家 阅读:74 集智导读: 本文会为大家展示机器学习专家 Mike Shi 如何 ...

  3. 简单20行python代码_用 20 行 python 代码实现人脸识别!Python实现就是这么简单!...

    今天给大家介绍一个世界上最简洁的人脸识别库 face_recognition,你可以使用 Python 和命令行工具进行提取.识别.操作人脸. 基于业内领先的 C++ 开源库 dlib 中的深度学习模 ...

  4. python秒表游戏代码_用20行Python代码实现2048小游戏,你会吗?

    前些天在b站上看到有个大佬用c写了一个2048小游戏,我便一下来了兴趣.心想着,我貌似也能用Python来整一波,话不多说,直接开搞. 2048的游戏规则: 2048游戏总共有16个格子,初始时会有两 ...

  5. python画人脸代码_[转]7行Python代码的人脸识别

    随着去年alphago 的震撼表现,AI 再次成为科技公司的宠儿.AI涉及的领域众多,图像识别中的人脸识别是其中一个有趣的分支.百度的BFR,Face++的开放平台,汉王,讯飞等等都提供了人脸识别的A ...

  6. 服不服?40行Python代码,实现卷积特征可视化

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 本文转自|深度学习这件小事 卷积神经网络(CNN)变革了计算机视觉 ...

  7. 40行Python代码,实现卷积特征可视化

    最近在阅读 Jeremy Rifkin 的书<The End of Work>时,我读到一个有趣的关于 AI 的定义.Rifkin 写到:「今天,当科学家们谈论人工智能时,他们通常是指『一 ...

  8. python爬虫教程:如何用40行Python代码实现天气预报和每日鸡汤推送功能

    这篇文章主要介绍了通过40行Python代码实现天气预报和每日鸡汤推送功能,代码简单易懂,非常不错具有一定的参考借鉴价值 ,需要的朋友可以参考下 情人节刚过去几天,但是这和我们程序员有什么关系呢,对我 ...

  9. python博弈论代码_使用 40 多行的 Python 代码实现一个简单的演化过程

    Python部落(组织翻译,禁止转载,欢迎转发. 在纳米比亚的 PyCon 会议上,我发表了一篇名为 <使用 Python 解决"升级版的剪 ...


  1. R语言text函数在R原生可视化结果中添加文本标签、内容:指定文本显示的位置、颜色、字体大小等(Position、Change Color 、 Size of Text)
  2. 元数据驱动设计 —— 为动态移动应用创建Web API
  3. Expected more than 1 value per channel when training, got input size torch.Size
  4. channelinboundhandler中都包含了哪一类的方法_备考CMA考试有哪些方法技巧?
  5. python线下培训-线下培训价值一万八的某达PYTHON培训视频
  6. java基础-容器-Set
  7. linux GDB 简单用法以及例子
  8. 王道408数据结构——第一章 绪论
  9. c语言中怎样变大输出的字符串,C语言。要输入一个超大数比如 111111111111111111111111111111111111111 怎样用字符串数组求各位的和呢?...
  10. BZOJ 4310 二分+SA+RMQ
  11. @babel/polyfill按需加载
  12. 三菱PLC QCPU用户手册(功能解说/程序基础篇)
  13. 机械设备行业经销商管理系统细致化经销商管理,让渠道管理更可控
  14. 信创办公--基于WPS的Word最佳实践系列(应用导航窗格:轻松掌握文章结构)
  15. 当红小生酒店施暴性感女星
  16. 机器学习推荐算法之关联规则(Apriori)——支持度;置信度;提升度
  17. Unity5.x从入门到精通资源(电子版+书内项目)
  18. Grammar API
  19. Font Awesome 的使用
  20. AI巨头们建造的“新世界”,进展如何?


  1. 软件测试--利用正交表设计测试用例
  2. 为何 Map接口不继承Collection接口
  3. java 套接字关联的通道_Java 通道教程 – NIO 2.0
  4. 后端返回的数据中换行符 html换行
  5. python之网络爬虫
  6. Parentheses Balance (括号平衡)---栈
  7. 盖茨基金会:如何使用Python拯救生命
  8. “互联网+”的时代,易佳互联也随着时代步伐前进着
  9. [转]解决Eclipse中编辑xml文件的智能提示问题
  10. ORACLE SQL: 经典查询练手第二篇