title: nginx try_files rewrite if exists proxy if doesn't
body: <p>I am using an <code>nginx</code> <code>docker</code> container (<a href="https://github.com/jwilder/nginx-proxy" rel="nofollow noreferrer">https://github.com/jwilder/nginx-proxy</a>) to proxy requests to instances of <code>apache</code> running in other docker containers. Each subdomain gets its own apache docker container. One of these subdomains (<strong>sub1</strong>) requires some special rules:</p>
  
  <p>If the URL begins with "foo" then I would like nginx to check if the file exists. If the file exists, then it should rewrite the URL to an access control script (imageControl.php), and pass it to the usual apache container. If the file does not exist then I want the request to be proxied to a remote server.</p>
  
  <p>I thought I set things up correctly, but for some reason nginx is always sending the request to the remote server - even if the file exists locally.</p>
  
  <p>Here is the relevant portion of the nginx config file from within the docker container:</p>
  
  <p><em>default.conf (included from nginx.conf):</em></p>
  
  <pre><code>upstream sub1.mydomain.com {
              ## Can be connect with "dockerized_default" network
              # dockerized_sub1
              server 172.18.0.2:80;
  }
  server {
      server_name sub1.mydomain.com;
      listen 80 ;
      access_log /var/log/nginx/access.log vhost;
      include /etc/nginx/vhost.d/sub1.mydomain.com;
      location / {
          proxy_pass http://sub1.mydomain.com;
      }
  }  
  </code></pre>
  
  <p><em>/etc/nginx/vhost.d/sub1.mydomain.com :</em></p>
  
  <pre><code>location ^~ /foo/ {
      root /path/to/dockerized/webroot;
      try_files $uri @rewrite @remoteproxy;
  }
  
  location @remoteproxy {
     include vhost.d/remoteproxy.inc;
     proxy_pass http://remote.ip.address.here;
  }
  
  location @rewrite {
      rewrite ^/(.*)$ /imageControl.php?file=$1 break;
  }
  </code></pre>
  
  <p>Please note that I do not want nginx to try to run imageControl.php (I am not using <code>php-fpm</code>) - I want nginx to pass it to the upstream apache container. </p>
  
  <p>Desired behavior example:</p>
  
  <ol>
  <li><p>(<em>this part works</em>)</p>
  
  <ul>
  <li>request is <a href="http://sub1.mydomain.com/foo/bar.png" rel="nofollow noreferrer">http://sub1.mydomain.com/foo/bar.png</a></li>
  <li>/path/to/dockerized/webroot/foo/bar.png does not exist, so the request is proxied to the remote ip. </li>
  </ul></li>
  <li><p>(<em>this part does not work</em>)</p>
  
  <ul>
  <li>request is <a href="http://sub1.mydomain.com/foo/baz.png" rel="nofollow noreferrer">http://sub1.mydomain.com/foo/baz.png</a></li>
  <li>/path/to/dockerized/webroot/foo/baz.png exists, so the request is rewritten to <a href="http://sub1.mydomain.com/imageControl.php?file=foo/baz.png" rel="nofollow noreferrer">http://sub1.mydomain.com/imageControl.php?file=foo/baz.png</a></li>
  <li>the request is sent to the upstream apache container</li>
  </ul></li>
  </ol>
  
  <p>I'd appreciate some help.</p>