nginx 反相代理这么设置一个概率跳转至另外一个域名

2015-05-16 19:54:30 +08:00
 holinhot

Nginx 反相代理这么设置一个概率跳转至另外一个域名
1.com 反向代理 2.com 设置一个 20%概率跳转至 3.com 这么实现

4368 次点击
所在节点    NGINX
20 条回复
Showfom
2015-05-16 20:01:56 +08:00
cache key一样了
holinhot
2015-05-16 20:07:04 +08:00
@Showfom 有没有智能的
liuhaotian
2015-05-16 20:16:59 +08:00
你需要一个 nginx_lua 模块,可以选择在 rewrite_by_lua 进行控制
holinhot
2015-05-16 20:38:58 +08:00
@liuhaotian 有相关文档吗没用过这个
liuhaotian
2015-05-16 20:52:19 +08:00
holinhot
2015-05-16 20:54:42 +08:00
@liuhaotian

location / {
rewrite_by_lua '
local res = ngx.location.capture("/check-spam")
if res.body == "spam" then
ngx.redirect("/terms-of-use.html")
end
'; fastcgi_pass ...;
}

这个概率应该怎么写呢
liuhaotian
2015-05-16 20:57:46 +08:00
nginx lua 支持 random 随机函数
random=math.random()
你可以针对这个搜索
holinhot
2015-05-16 20:59:53 +08:00
@liuhaotian if时间秒来设置概率?
holinhot
2015-05-16 21:00:15 +08:00
@liuhaotian ok我找一下
ryd994
2015-05-17 01:32:17 +08:00
Nginx 有 AB test模块啊……
jqw1992
2015-05-17 09:01:47 +08:00
这是要做什么啊
holinhot
2015-05-17 13:53:52 +08:00
@jqw1992
@ryd994
@holinhot
@holinhot
@liuhaotian
@Showfom

能不能设置10个轮循 然后8个是代理至源网站 2个到其他服务器,轮到其他服务器这2个其他服务器的就跳转。 不知道能不能对同一个ip源进行轮循
liuhaotian
2015-05-17 13:58:02 +08:00
这个明显的在你的原服务器进行处理就可以了,为什么要代理服务器处理呢… 另外lua应当是可以的
jqw1992
2015-05-17 15:12:49 +08:00
@holinhot 真不知道 你想做什么 。。。这个写个程序 就可以啊
holinhot
2015-05-17 18:48:27 +08:00
@jqw1992 就是我想反代某个网站 想设置一个概率转到我自己的网站
holinhot
2015-05-17 19:03:45 +08:00
@jqw1992
我看nginx-http-footer-filter 用这个在页面加几行代码来实现概率跳转可以吗?
ryd994
2015-05-18 00:57:03 +08:00
holinhot
2015-05-18 18:11:33 +08:00
@ryd994 这个正是我要的
holinhot
2015-05-18 18:37:07 +08:00
@ryd994 还要增加一个同一ip地址固定时间内只跳转1次
ryd994
2015-05-18 20:54:59 +08:00
@holinhot 这个比较麻烦
Nginx为了性能,基本上是无状态的
也许可以用cookies,但是Nginx判断cookie也麻烦
或者可以加个cgi后端,返回个跳转给Nginx不复杂,比较恶心
最简单应该是带尾巴,不过处理不好的话上游会发现。除了这个没有其他问题

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/191576

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX