如何在 Openresty 中为每个上游解析 AWS ELB 的 IP

How to resolve IPs of AWS ELB in Openresty for every upstream

我们遇到的问题是 openresty nginx 只在启动时解析 AWS ELB 的 IP,然后永远缓存 IP(直到它重新加载)。由于 AWS 负载均衡器 IP 可以随时更改,我正在寻找一种解决方案,每次在上游使用它时都可以解析 ELB IP。 在 nginx+ 中寻找类似于 upstream "resolve" 选项的东西,但在 Openresty 中。 或者其他一些使上游 DNS 缓存无效的方法

您可以在 balancer_by_lua_block 指令中使用 balancer module。您可以设置任何上游 IP。

This Lua code execution context does not support yielding, so Lua APIs that may yield (like cosockets and "light threads") are disabled in this context. One can usually work around this limitation by doing such operations in an earlier phase handler (like access_by_lua*) and passing along the result into this context via the ngx.ctx table.

您可以使用 lua-resty-dns 并为 access_by_lua_* 中的每个请求解析您的 ELB IP(性能好吗?!),将获得的 IP 保存到 ngx.ctx 中并使用它balancer_by_lua_block.

以内

使用 DNS 服务器作为您的 resolverproxy_pass 值的变量怎么样?

请看下面这个 post "Nginx with dynamic upstreams" 的例子,作者是 Jeppe Fihl-Pearson

resolver 172.16.0.23;
set $upstream_endpoint http://service-1234567890.us-east-1.elb.amazonaws.com;
location /foo/ {
    rewrite ^/foo/(.*) / break;
    proxy_pass $upstream_endpoint;
}

将此添加到服务器指令

resolver local=on valid=5s;

然后您可以像往常一样使用上游块。