nginx上游为red:connect()at lua-resty-redis

时间:2016-03-31 14:32:03

标签: nginx lua redis

我需要在预先配置的redis服务器上游和动态IP:6379之间切换自定义标头以进行测试。当前配置示例:

upstream backend_cluster {

    server aaa.bbb.ccc.ddd:6379 fail_timeout=0 max_fails=0;

    server aaa.bbb.ccc.ddd:6380 fail_timeout=0 max_fails=0;

    server aaa.bbb.ccc.ddd:6381 fail_timeout=0 max_fails=0;

    server aaa.bbb.ccc.ddd:6382 fail_timeout=0 max_fails=0;


keepalive 16;

}

map $http_BackendIP $backend_upstream {

    default                                 "backend_cluster";
    "~^([0-9]{1,3}[\.]){3}[0-9]{1,3}+"      "$BackendIP";

}

server {

    listen  80 default_server;
    server_name _;
    set $BackendIP "${http_BackendIP}:6379";

    location = /test {

        ........
        content_by_lua_block {
                    local redis = require "resty.redis"
                    local red = redis:new()
                    red:set_timeout(1000)
                    local ok, err = red:connect(ngx.var.backend_upstream)
                    if not ok then
                        ngx.log(ngx.ERR, "failed to connect: ", err)
                        return
                    end
                    ok, err = red:rpush(ngx.var.redis_key, ngx.var.query)
                    if not ok then
                        ngx.log(ngx.ERR, "rpush error: ", err)
                        return
                    end
        }

    }

}

如果我将自定义标头设置为

,此配置成功将数据推送到redis,电话号码是123.456.789.0:6379

"BackendIP: 123.456.789.0"

但没有BackendIP标头,它无法理解nginx上游错误:

** failed to connect: backend_cluster could not be resolved (3: Host not found) **

我可以以某种方式让那件事按预期工作吗?先谢谢,同志们。

0 个答案:

没有答案