V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
NGINX
NGINX Trac
3rd Party Modules
Security Advisories
CHANGES
OpenResty
ngx_lua
Tengine
在线学习资源
NGINX 开发从入门到精通
NGINX Modules
ngx_echo
holinhot
V2EX  ›  NGINX

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

  •  
  •   holinhot · 2015-05-16 19:54:30 +08:00 · 4344 次点击
    这是一个创建于 3274 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    20 条回复    2015-05-18 20:54:59 +08:00
    Showfom
        1
    Showfom  
       2015-05-16 20:01:56 +08:00 via iPhone
    cache key一样了
    holinhot
        2
    holinhot  
    OP
       2015-05-16 20:07:04 +08:00
    @Showfom 有没有智能的
    liuhaotian
        3
    liuhaotian  
       2015-05-16 20:16:59 +08:00
    你需要一个 nginx_lua 模块,可以选择在 rewrite_by_lua 进行控制
    holinhot
        4
    holinhot  
    OP
       2015-05-16 20:38:58 +08:00
    @liuhaotian 有相关文档吗没用过这个
    liuhaotian
        5
    liuhaotian  
       2015-05-16 20:52:19 +08:00
    holinhot
        6
    holinhot  
    OP
       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
        7
    liuhaotian  
       2015-05-16 20:57:46 +08:00
    nginx lua 支持 random 随机函数
    random=math.random()
    你可以针对这个搜索
    holinhot
        8
    holinhot  
    OP
       2015-05-16 20:59:53 +08:00
    @liuhaotian if时间秒来设置概率?
    holinhot
        9
    holinhot  
    OP
       2015-05-16 21:00:15 +08:00
    @liuhaotian ok我找一下
    ryd994
        10
    ryd994  
       2015-05-17 01:32:17 +08:00 via Android
    Nginx 有 AB test模块啊……
    jqw1992
        11
    jqw1992  
       2015-05-17 09:01:47 +08:00
    这是要做什么啊
    holinhot
        12
    holinhot  
    OP
       2015-05-17 13:53:52 +08:00
    @jqw1992
    @ryd994
    @holinhot
    @holinhot
    @liuhaotian
    @Showfom

    能不能设置10个轮循 然后8个是代理至源网站 2个到其他服务器,轮到其他服务器这2个其他服务器的就跳转。 不知道能不能对同一个ip源进行轮循
    liuhaotian
        13
    liuhaotian  
       2015-05-17 13:58:02 +08:00 via iPhone
    这个明显的在你的原服务器进行处理就可以了,为什么要代理服务器处理呢… 另外lua应当是可以的
    jqw1992
        14
    jqw1992  
       2015-05-17 15:12:49 +08:00
    @holinhot 真不知道 你想做什么 。。。这个写个程序 就可以啊
    holinhot
        15
    holinhot  
    OP
       2015-05-17 18:48:27 +08:00
    @jqw1992 就是我想反代某个网站 想设置一个概率转到我自己的网站
    holinhot
        16
    holinhot  
    OP
       2015-05-17 19:03:45 +08:00
    @jqw1992
    我看nginx-http-footer-filter 用这个在页面加几行代码来实现概率跳转可以吗?
    holinhot
        18
    holinhot  
    OP
       2015-05-18 18:11:33 +08:00
    @ryd994 这个正是我要的
    holinhot
        19
    holinhot  
    OP
       2015-05-18 18:37:07 +08:00
    @ryd994 还要增加一个同一ip地址固定时间内只跳转1次
    ryd994
        20
    ryd994  
       2015-05-18 20:54:59 +08:00 via Android
    @holinhot 这个比较麻烦
    Nginx为了性能,基本上是无状态的
    也许可以用cookies,但是Nginx判断cookie也麻烦
    或者可以加个cgi后端,返回个跳转给Nginx不复杂,比较恶心
    最简单应该是带尾巴,不过处理不好的话上游会发现。除了这个没有其他问题
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   812 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:42 · PVG 06:42 · LAX 15:42 · JFK 18:42
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.