title: Dynamic proxy_pass with map and regexp
body: <p>I'm trying to get the following pattern to work. I need to specify a dynamic path in my client side code to be able to switch to a few predefined hosts. I map these hosts by appending their alias to a /fwd/ url. The alias is mapped to the real server in nginx like this:</p>
  
  <pre><code>map $uri $repoUrl {
      default             invalid;
      ~^/fwd/foo/.*        http://foo.domain.nl/;
      ~^/fwd/bar/.*        http://bar.domain.nl/;
    }
  </code></pre>
  
  <p>Then in the server config part I catch any url starting with fwd and apply the mapped alias value. The remaining part of the url, after the alias should be appended to the url as well.</p>
  
  <pre><code>location /fwd/(\w+)/(.*)$ {
    add_header X-FwdHost "$repoUrl$2";
    add_header Access-Control-Allow-Origin "*";
    proxy_pass         "$repoUrl$2";
    proxy_redirect off;
    access_log on;
  }
  </code></pre>
  
  <p>If I test this with:</p>
  
  <pre><code>curl -i http://localhost:8080/fwd/foo/something/else
  </code></pre>
  
  <p>I get:</p>
  
  <pre><code>X-FwdHost: http://foo.domain.nl/
  </code></pre>
  
  <p>But when test the results from the regexp I get:</p>
  
  <pre><code> $1: foo
   $2: something/else
  </code></pre>
  
  <p>So overall it seems to be working. The regex appears to be ok, but I can't get it to concatenate into one string? Any ideas or is there an easier/better way to accomplish the same?</p>
  
  <p>[EDIT]</p>
  
  <p>I found a possibly much easier way to do this, by using a query parameter named forward. First map the query parameter to a the right host:</p>
  
  <pre><code>map $arg_forward $repo_forward {
  default           http://invalid_repo_forward/;
  
  foo              http://foo.domain.nl/;
  bar              http://bar.domain.nl/;
  </code></pre>
  
  <p>}</p>
  
  <p>Then use the parameter in the path to be forwarded:</p>
  
  <pre><code>location /fwd/ {
    add_header X-FwdHost $repo_forward;
    add_header Access-Control-Allow-Origin "*";
    proxy_pass         $repo_forward;
    proxy_redirect off;
    access_log on;
  }
  </code></pre>
  
  <p>I would expect and url like:</p>
  
  <pre><code>http://localhost:8080/fwd/?forward=foo
  </code></pre>
  
  <p>To result in:</p>
  
  <pre><code>http://foo.domain.nl/
  </code></pre>
  
  <p>...but still this doesn't work. I get a 404 returned. What am I missing?</p>