RobL RobL - 2 years ago 268
PHP Question

How to get Slim PHP Framework routes in a subdirectory on nginx

I'm trying to move a Slim application to a subdirectory, so that it can be accessed at
, but I'm having serious problems getting the routing to work.

The main script is at
, so a call to
should hit the API folder. However, I also need to be able to access's
file (which is running on Angular JS).

It does hit the PHP script when I go to
- I can
variables and see them. However, the routing is not taking effect, and the request variables seem to be empty.


server {
listen 80; ## listen for ipv4; this line is default and implied

root /website/workbench;
index index.php index.html index.htm;


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
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;

is replaced with the real domain name)

Partial output of

array(34) {
string(8) "www-data"
string(8) "/var/www"
string(9) "RESPONDER"
string(26) "newrelic.appname=workbench"
string(0) ""
string(3) "GET"
string(0) ""
string(0) ""
string(39) "/website/workbench/api/public/index.php"
string(21) "/api/public/index.php"
string(15) "/api/projects/1"
string(21) "/api/public/index.php"
string(18) "/website/workbench"
string(8) "HTTP/1.1"
string(7) "CGI/1.1"
string(11) "nginx/1.4.6"

gives an empty array.

Clearly there's something spectacularly wrong with my set-up, but I'm struggling to see what! Changing
has no effect either.

Answer Source

I think you can remove $query_string from try_files. Add a root directive in /api location, like this:

location /api {
    root /website/workbench/api;
    try_files $uri $uri/ /public/index.php;

and use the fastcgi_param SCRIPT_FILENAME parameter eventually defined as:

 fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;

into the fastcgi_params file.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download