Dynamic nginx proxy_pass

135 Views Asked by At

I would like to have every single IP request be redirect to a different on-demand created hostname. For example IP 11.22.33.44 should be redirected to the host which has DNS entry of box-11-22-33-44. (There is already a manager taking care of creating such DNS/box, you can assume that the link does exist).

The nginx proxy will be listening on multiple TCP ports to redirect to. However, to keep it simple, let's use port 80 as an example. I did try the following stream configuration which is running in a k8s pod:

stream {
 
    log_format  basic   'From $remote_addr to server-$remote_addr'
    ' Proxy: "$upstream_addr"'
    ;
    access_log      /var/log/nginx/stream.log  basic buffer=1k flush=1s;

    map $remote_addr $backend_svr {
      ~(.*)\.(.*)\.(.*)\.(.*)    box-$1-$2-$3-$4:80;
    }

    server {
      # resolver 10.96.0.10 ; # <--- DOES HOLD AND DOES NOT WORK
      listen 80;
      proxy_pass $backend_svr;
      proxy_protocol on;
   }
}

However, the previous configuration does not work and does either timeout on the DNS resolution or fails to resolve the box-$1-$2-$3-$4:80 at all.

Is there anyway to have nginx work for my scenario where I need dynamic proxy forwarding?

0

There are 0 best solutions below