nginx dynamic proxy_pass based on database lookup

2k views Asked by At

I have a website with a wildcard subdomain: *.mydomain running nginx. I want to proxy_pass to a different port on another server depending on the subdomain.

For example:
abc.mydomain -> proxy_pass http://otherdomain:10001
foo.mydomain -> proxy_pass http://otherdomain:10002
etc.

The port to redirect to is in a database and I can already get it by calling a url to look it up:

curl http://mydomain/getport/abc -> 10001
curl http://mydomain/getport/foo -> 10002

There are hundreds of subdomains on my site. How can I do such a dynamic port lookup on each access for use with proxy_pass in nginx?

My current nginx configuration for a single subdomain is this:

server {
    listen 80;
    server_name abc.mydomain;

    location / {
        proxy_pass http://otherdomain:10001;
    }
}

Thanks

1

There are 1 answers

0
ssun On

Found this may help the problem. Using OpenResty, save the URI into Redis, and look up each request URI schema.

https://openresty.org/en/dynamic-routing-based-on-redis.html