Proxy_pass, dynamic ports upstream

I have a requirement to be able to map a portion of a request URI to a
port
on a set of upstream servers. I’m hoping nginx will be able to solve
this
for me, but so far no luck.

Request:
http://example.com/2201/reg/106903/0?something=here&somemore=stuff

Needs to be proxied to:
http://10.11.12.13:2201/reg/106903/0?something=here&somemore=stuff

So the 1st portion of the URI is used as the upstreams port. However
I’m
having difficulty with this when I attempt to combine it with a set of
upstream servers. The below configuration results in the error “no
resolver
defined to resolve engines”.

If I specify the upstream as " proxy_pass
http://10.11.12.13:$1/$2/?$args;
" (and change nothing else) the below configuration works… however
it’s
not being load balanced obviously.

Basically, can I load balance and use dynamic ports?

Configuration:

upstream engines {
server 10.11.12.13;
server 10.11.12.14;
}

server {
listen *:80;

access_log /var/log/nginx/engines.access.log main;
error_log /var/log/nginx/engines.error.log debug;

location ~/([0-9])/ {
rewrite ^/([0-9]
)/(.*)$ $2 break;
proxy_pass http://engines:$1/$2/?$args;
proxy_next_upstream error timeout invalid_header http_500 http_502
http_503 http_504;
proxy_redirect off;
proxy_buffering on;
proxy_set_header Host $http_host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

Posted at Nginx Forum:

On 15 November 2012 16:43, mahhy [email protected] wrote:

I have a requirement to be able to map a portion of a request URI to a port
on a set of upstream servers. I’m hoping nginx will be able to solve this
for me, but so far no luck.

Use a map: Module ngx_http_map_module

You’ll probably want to use regular expressions, and run the map based
off $request_uri or $uri.

Jonathan

Jonathan M. // Oxford, London, UK
http://www.jpluscplusm.com/contact.html

While I can easily build a map as such:

map $uri $engineport {
~^/2201/ 2201;
~^/2202/ 2202;
<snip 100 more ports>
}

This still doesn’t seem to work with a set of load balanced upstream
servers. Trying to append a port onto the proxy_pass directive when
used
with an upstream server group fails with the resolver related message in
my
orignal post.

With a single server upstream this can be done, however I want to round
robin/loadbalance over 2 or more…

  • mahhy

Jonathan M. Wrote:

off $request_uri or $uri.

Jonathan

Jonathan M. // Oxford, London, UK
http://www.jpluscplusm.com/contact.html


nginx mailing list
[email protected]
nginx Info Page

Posted at Nginx Forum: