title: Nested location with alias
body: <p>I have a directory/web app that is located outside of the web root directory of my site.</p>
  
  <p>Say the site is here:</p>
  
  <pre><code>/var/www/site/htdocs/
  </code></pre>
  
  <p>And the external app is located here:</p>
  
  <pre><code>/var/www/apps/coolapp/
  </code></pre>
  
  <p>My question is how can I configure nginx to map/route all requests that are like <code>www.mysite.com/coolapp/*</code> (asterisk being wildcard) to the external location <code>/var/www/apps/coolapp/</code>? For example, www.mysite.com/coolapp/test.php should server <code>/var/www/apps/coolapp/test.php</code>.  </p>
  
  <p>I have added an <code>alias</code> directive in <code>production.conf</code> that the main <code>nginx.conf</code> file includes. This worked fine for everything except .php files because there is another <code>location</code> that is catching .php files instead. So I nested a <code>location</code> in with the <code>alias</code> to catch .php files, but now nginx is telling me it can't find the .php files "404 File Not Found". Here is what <code>production.conf</code> currently looks like</p>
  
  <pre><code>server {
      listen 80;
      listen 443 ssl;
  
      ssl_certificate     /blah/blah/blah;
      ssl_certificate_key /blah/blah/blah;
      ssl_protocols       blah blah blah;
      ssl_ciphers         blahblahblah;
      ssl_prefer_server_ciphers blahblah;
  
      access_log /var/log/nginx/www.mysite.com-access.log;
      error_log  /var/log/nginx/www.mysite.com-error.log error;
  
      server_name mysite.com www.mysite.com;
      root /var/www/site/htdocs;
  
      include conf/magento_rewrites.conf;
      include conf/magento_security.conf;
      include /var/www/site/nginx/*.conf;
  
      #-------CODE IN QUESTION-------
      location  /coolapp/ {
          alias  /var/www/apps/coolapp/;
          location ~ \.php {
              # Copied from "# PHP Handler" below
              fastcgi_param MAGE_RUN_CODE default;
              fastcgi_param MAGE_RUN_TYPE store;
              fastcgi_param HTTPS $fastcgi_https;
              rewrite_log on;
  
              # By default, only handle fcgi without caching
              include conf/magento_fcgi.conf;
          }
      }
  
      # PHP handler
      location ~ \.php {
        ## Catch 404s that try_files miss
        if (!-e $request_filename) { rewrite / /index.php last; }
  
        ## Store code is defined in administration &gt; Configuration &gt; Manage Stores
        fastcgi_param MAGE_RUN_CODE default;
        fastcgi_param MAGE_RUN_TYPE store;
        fastcgi_param HTTPS $fastcgi_https;
        rewrite_log on;
  
        # By default, only handle fcgi without caching
        include conf/magento_fcgi.conf;
      }
  
      # 404s are handled by front controller
      location @magefc {
          rewrite ^(.*) /index.php?$query_string last;
      }
  
      # Last path match hands to magento or sets global cache-control
      location / {
          ## Maintenance page overrides front controller
          index index.html index.php;
          try_files $uri $uri/ @magefc;
          expires 24h;
      }
  }
  </code></pre>
  
  <p>conf/magento_fcgi.conf looks like this:</p>
  
  <pre><code>fastcgi_pass phpfpm;
  
  ## Tell the upstream who is making the request
  proxy_set_header Host $host;
  proxy_set_header X-Real-IP $remote_addr;
  proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
  proxy_redirect off;
  
  # Ensure the admin panels have enough time to complete large requests ie: report generation, product import/export
  proxy_read_timeout 1600s;
  
  # Ensure PHP knows when we use HTTPS
  fastcgi_param  HTTPS           $fastcgi_https;
  
  ## Fcgi Settings
  include                        fastcgi_params;
  fastcgi_connect_timeout        120;
  fastcgi_send_timeout           320s;
  fastcgi_read_timeout           1600s;
  fastcgi_buffer_size            128k;
  fastcgi_buffers 512            64k;
  fastcgi_busy_buffers_size      128k;
  fastcgi_temp_file_write_size   256k;
  fastcgi_intercept_errors       off;
  fastcgi_index  index.php;
  fastcgi_param  SCRIPT_FILENAME /var/www/apps/coolapp$fastcgi_script_name;
  fastcgi_param  SCRIPT_NAME     $fastcgi_script_name;
  # nginx will buffer objects to disk that are too large for the buffers above
  fastcgi_temp_path              /tmpfs/nginx/tmp 1 2;
  #fastcgi_keep_conn              on; # NGINX 1.1.14
  expires                        off; ## Do not cache dynamic content
  </code></pre>
  
  <p>Here are some errors messages I pulled from error.log</p>
  
  <pre><code>2014/02/28 11:10:17 [error] 9215#0: *933 connect() failed (111: Connection refused) while connecting to upstream,   .................   client: x.x.x.x, server: mysite.com, request: "GET /coolapp/test.php HTTP/1.1", upstream: "fastcgi://[::1]:9000", host: "www.mysite.com"  
  2014/02/28 11:10:17 [error] 9215#0: *933 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream,  client: x.x.x.x, server: mysite.com, request: "GET /coolapp/test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mysite.com"  
  2014/02/28 11:11:59 [error] 9220#0: *1193 FastCGI sent in stderr: "Primary script unknown" while reading response header from upstream, client: x.x.x.x, server: mysite.com, request: "GET /coolapp/test.php HTTP/1.1", upstream: "fastcgi://127.0.0.1:9000", host: "www.mysite.com"  
  </code></pre>
  
  <p>Does anyone see what I'm doing wrong?</p>