title: nginx proxy_pass routes to wrong backend location
body: <p>I am running </p>
  
  <pre><code>nginx version: nginx/1.4.6 (Ubuntu)
  </code></pre>
  
  <p>What I am trying to achieve is;</p>
  
  <ol>
  <li>request is from <a href="http://my.frontend.com/goto/mytoken" rel="nofollow">http://my.frontend.com/goto/mytoken</a></li>
  <li>I want it to be handled by <a href="http://my.backend.com:5202/goto/mytoken" rel="nofollow">http://my.backend.com:5202/goto/mytoken</a></li>
  <li>Restriction is that only /goto/ traffic be forwarded to backend, without /goto/ in frontend url, it should be 404 return i.e. even <a href="http://my.frontend.com" rel="nofollow">http://my.frontend.com</a></li>
  </ol>
  
  <p><strong>goto</strong> is static, <strong>mytoken</strong> is different for each request (that is known to backend). </p>
  
  <p>My server definition for this scenario is;</p>
  
  <pre><code>server {
  listen 80;
  server_name my.frontend.com;
  
  location ~* ^/goto/(.*) {
      proxy_pass http://my.backend.com:5202/$uri$1$is_args$args;
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  }
  }
  </code></pre>
  
  <p>But the problem is; although as per access.log my URI /goto/mytoken seems to be constructed ok, but backend returns 404</p>
  
  <blockquote>
    <p>x.x.x.x - - [27/May/2016:13:01:56 +0000] "GET /goto/7328372837182382938 HTTP/1.1" 404 69
    "<a href="http://mywebsite.com/index.html" rel="nofollow">http://mywebsite.com/index.html</a>" "Mozilla/5.0 (Windows NT 6.1;
    WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102
    Safari/537.36"</p>
  </blockquote>
  
  <p>Same scenario works well if I have below i.e. no /goto/ in location.</p>
  
  <pre><code>server {
  listen 80;
  server_name my.frontend.com;
  
  location / {
      proxy_pass http://my.backend.com:5202;
      ...
  }
  }
  </code></pre>
  
  <p>Really appreciate a hint here.</p>