crashandburn
crashandburn

Reputation: 125

Nginx proxy pass based on url part to port

Is it possible to set up nginx to reverse proxy server.com/[port]/rest/of/url to server.com:[port]/rest/of/url ? For example, server.com/12345/files should proxy server.com:12345/files. The ports are random and there could 100s of such ports.

I have docker containers in a linux VM which bind to random ports on the host and serve HTTP. I'm trying to setup a proxy for them on a single port.

Upvotes: 1

Views: 1926

Answers (1)

Maxim Dounin
Maxim Dounin

Reputation: 6805

Something like this should do the trick:

server {
    listen 80;

    location ~ ^/(?<port>\d+)/ {
        rewrite ^/\d+(/.*) $1 break;
        proxy_pass http://127.0.0.1:$port;
    }
}

See the following links for more details:

Upvotes: 3

Related Questions