Forum: NGINX proxy_pass, dynamic ports upstream

Posted by mahhy (Guest)
on 2012-11-15 17:43
(Received via mailing list)
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=her...

Needs to be proxied to:
http://10.11.12.13:2201/reg/106903/0?something=her...

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: 
http://forum.nginx.org/read.php?2,232849,232849#msg-232849
Posted by Jonathan Matthews (Guest)
on 2012-11-15 18:25
(Received via mailing list)
On 15 November 2012 16:43, mahhy <nginx-forum@nginx.us> 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: 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 Matthews // Oxford, London, UK
http://www.jpluscplusm.com/contact.html
Posted by mahhy (Guest)
on 2012-11-15 20:33
(Received via mailing list)
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 Matthews Wrote:
-------------------------------------------------------
> off $request_uri or $uri.
>
> Jonathan
> --
> Jonathan Matthews // Oxford, London, UK
> http://www.jpluscplusm.com/contact.html
>
> _______________________________________________
> nginx mailing list
> nginx@nginx.org
> http://mailman.nginx.org/mailman/listinfo/nginx

Posted at Nginx Forum: 
http://forum.nginx.org/read.php?2,232849,232858#msg-232858
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.