# Proxy\_pass, dynamic ports upstream

**URL:** <https://www.ruby-forum.com/t/proxy-pass-dynamic-ports-upstream/224539>\
**Category:** NGINX\
**Created:** [November 15, 2012, 5:43pm UTC](https://www.ruby-forum.com/t/proxy-pass-dynamic-ports-upstream/224539 "2012-11-15T17:43:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![aris](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/258eb7/32.png) [@aris](https://www.ruby-forum.com/u/aris)\
**Post date:** [November 15, 2012, 5:43pm UTC](https://www.ruby-forum.com/t/proxy-pass-dynamic-ports-upstream/224539/1 "2012-11-15T17:43:39Z")

</div>

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](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](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](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](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:

> **[proxy\_pass, dynamic ports upstream](https://forum.nginx.org/read.php?2,232849,232849#msg-232849)**
>
> 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:...

---

<div class="post-metadata">

**Author:** ![mahhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@mahhy](https://www.ruby-forum.com/u/mahhy)\
**Post date:** [November 15, 2012, 6:25pm UTC](https://www.ruby-forum.com/t/proxy-pass-dynamic-ports-upstream/224539/2 "2012-11-15T18:25:46Z")

</div>

On 15 November 2012 16:43, mahhy [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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](http://nginx.org/r/map)

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](http://www.jpluscplusm.com/contact.html)

---

<div class="post-metadata">

**Author:** ![mahhy](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/90db22/32.png) [@mahhy](https://www.ruby-forum.com/u/mahhy)\
**Post date:** [November 15, 2012, 8:33pm UTC](https://www.ruby-forum.com/t/proxy-pass-dynamic-ports-upstream/224539/3 "2012-11-15T20:33:45Z")

</div>

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](http://www.jpluscplusm.com/contact.html)
> 
> * * *
> 
> nginx mailing list  
> removed\_email\_address@domain.invalid  
> [nginx Info Page](http://mailman.nginx.org/mailman/listinfo/nginx)

Posted at Nginx Forum:

> **[Re: proxy\_pass, dynamic ports upstream](https://forum.nginx.org/read.php?2,232849,232858#msg-232858)**
>
> 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:...
