title: How to get Slim PHP Framework routes in a subdirectory on nginx
body: <p>I'm trying to move a Slim application to a subdirectory, so that it can be accessed at <code>example.com/api/</code>, but I'm having serious problems getting the routing to work.</p>
  
  <p>The main script is at <code>/website/workbench/api/public/index.php</code>, so a call to <code>example.com/api/project/1</code> should hit the API folder. However, I also need to be able to access example.com's <code>index.html</code> file (which is running on Angular JS).</p>
  
  <p>It does hit the PHP script when I go to <code>example.com/api/project/1</code> - I can <code>var_dump</code> variables and see them. However, the routing is not taking effect, and the request variables seem to be empty.</p>
  
  <h2>/etc/nginx/sites-available/workbench</h2>
  
  <pre><code>server {
      listen   80; ## listen for ipv4; this line is default and implied
  
      root /website/workbench;
      index index.php index.html index.htm;
  
      server_name example.com;
  
      location / {
          try_files $uri $uri/ index.php?$query_string;
      }
  
      location /api/ {
          try_files $uri $uri/ /api/public/index.php?$query_string;
      }
  
      # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
      location ~ \.php$ {
          fastcgi_split_path_info ^(.+\.php)(/.+)$;
          fastcgi_pass unix:/var/run/php5-fpm.sock;
          fastcgi_index index.php;
          fastcgi_param PHP_VALUE "newrelic.appname=workbench";
          include fastcgi_params;
      }
  
      location ~ /\.ht {
          deny all;
      }
  }
  </code></pre>
  
  <p>(Obviously <code>example.com</code> is replaced with the real domain name)</p>
  
  <h2>Partial output of <code>var_dump($_SERVER)</code>;</h2>
  
  <pre><code>array(34) {
    ["USER"]=&gt;
    string(8) "www-data"
    ["HOME"]=&gt;
    string(8) "/var/www"
    ["FCGI_ROLE"]=&gt;
    string(9) "RESPONDER"
    ["PHP_VALUE"]=&gt;
    string(26) "newrelic.appname=workbench"
    ["QUERY_STRING"]=&gt;
    string(0) ""
    ["REQUEST_METHOD"]=&gt;
    string(3) "GET"
    ["CONTENT_TYPE"]=&gt;
    string(0) ""
    ["CONTENT_LENGTH"]=&gt;
    string(0) ""
    ["SCRIPT_FILENAME"]=&gt;
    string(39) "/website/workbench/api/public/index.php"
    ["SCRIPT_NAME"]=&gt;
    string(21) "/api/public/index.php"
    ["REQUEST_URI"]=&gt;
    string(15) "/api/projects/1"
    ["DOCUMENT_URI"]=&gt;
    string(21) "/api/public/index.php"
    ["DOCUMENT_ROOT"]=&gt;
    string(18) "/website/workbench"
    ["SERVER_PROTOCOL"]=&gt;
    string(8) "HTTP/1.1"
    ["GATEWAY_INTERFACE"]=&gt;
    string(7) "CGI/1.1"
    ["SERVER_SOFTWARE"]=&gt;
    string(11) "nginx/1.4.6"
  }
  </code></pre>
  
  <p><code>var_dump($_REQUEST)</code> gives an empty array.</p>
  
  <p>Clearly there's something spectacularly wrong with my set-up, but I'm struggling to see what! Changing <code>$query_string</code> to <code>$args</code> has no effect either.</p>