nginx 中不能在 proxy_pass 里使用变量

以下是我的配置:

server {
    server_name www.xxx.com;
    location ~* ^/(unstable|staging|prod)-console/ {
        set $serverip "";
        rewrite_by_lua '
            if string.find(ngx.var.uri, "unstable") ~= nil then
                ngx.var.serverip = "10.17.21.123"
            elseif string.find(ngx.var.uri, "staging") ~= nil then
                ngx.var.serverip = "10.17.21.123"
            elseif string.find(ngx.var.uri, "prod") ~= nil then
                ngx.var.serverip = "10.17.21.123"
            end
        ';
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $remote_addr;
        proxy_set_header Host $host;
        proxy_pass http://$serverip:1234;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
    }
}

当我访问匹配此位置的页面时,它会给我500错误。在 nginx 的错误日志中,它说 2017/03/17 15:11:19 [error] 2638#2638: *6 no host in upstream ":1234", client: 10.19.35.20, server: console.allinmoney.com, request: "GET /unstable-console/?arg=ifconfig HTTP/1.1", host: "console.allinmoney.com"。这意味着在 lua 脚本中变量没有被正确地更改。有谁能帮助我解决这个问题吗?谢谢

点赞