2

The problem we're having is that openresty nginx only resolves the IP of AWS ELBs at startup and then caches the IP forever (until it reloads). Since the AWS loadbalancer IP can change anytime, I am looking for a solution that can resolve the ELB IPs for every time its being used in upstream. Looking for something similar to upstream "resolve" option in nginx+, but in Openresty. Or some other method to invalidate upstream DNS cache

Bilal Ahmad
  • 322
  • 5
  • 11

3 Answers3

2

What about using a DNS server as your resolver and a variable for the value of proxy_pass?

Look at the example below from this post "Nginx with dynamic upstreams" by Jeppe Fihl-Pearson

resolver 172.16.0.23;
set $upstream_endpoint http://service-1234567890.us-east-1.elb.amazonaws.com;
location /foo/ {
    rewrite ^/foo/(.*) /$1 break;
    proxy_pass $upstream_endpoint;
}
user454322
  • 7,300
  • 5
  • 41
  • 52
1

There is balancer module which you can use from within balancer_by_lua_block directive. You would be able to set any upstream 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.

You may use lua-resty-dns and resolve your ELB IP for every request (is it performant?!) in access_by_lua_*, save obtained IP(s) into ngx.ctx and use it within balancer_by_lua_block.

Alexander Altshuler
  • 2,930
  • 1
  • 17
  • 27
  • Thanks for the answer. It did help me in resolving the load balancer. But I ended up doing something very easy, which was to load dynamic module in openresty which did the same thing exactly – Bilal Ahmad Jun 07 '18 at 12:26
0

Add this to the server directive

resolver local=on valid=5s;

You can then use the upstream block as usual.

basex
  • 490
  • 4
  • 11