Nginx请求分发与响应记录:如何实现30%到mirror1和90%到mirror2?

Nginx复制到mirror1的请求保留30%,复制到mirror2的请求保留40%,并记录mirror1和mirror2的response。

http {
    # ...

    server {
        listen 80;

        location / {
            # 启用Lua代理
            proxy_pass http://your_upstream;
            proxy_set_header Host $host;

            # 设置Lua代理参数
            proxy_set_body $body_bytes_sent;

            # 调用Lua脚本处理请求
            content_by_lua_block {
                local res = ngx.location.capture("/lua_proxy", {
                    method = ngx.var.request_method,
                    body = ngx.var.request_body,
                })

                -- 检查子请求的状态
                if res.status == 200 then
                    ngx.status = res.status
                    ngx.say(res.body)  -- 转发子请求的response
                else
                    ngx.status = res.status
                    ngx.say("Error: " .. res.body)
                end
            }
        }

        location /lua_proxy1 {
            internal;
            proxy_pass http://your_upstream;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_body $request_body;
        }
        
        location /lua_proxy2 {
            internal;
            proxy_pass http://your_upstream;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_body $request_body;
        }
    }
}
阅读 692
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏