proxy_pass, dynamic ports upstream

mahhy nginx-forum at
Thu Nov 15 16:43:11 UTC 2012

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.


Needs to be proxied to:

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$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?


upstream engines  {

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:,232849,232849#msg-232849

More information about the nginx mailing list