5

I am trying to configure Nginx to host multiple PHP based apps in two different directories in the same domain. The outcome I'm trying to get to is:

http://webserver.local/ > app served from /path/to/website

http://webserver.local/app > app served from /path/to/php-app

Here is the configuration I have.

Can someone please shed some light as to where I am going wrong? Thanks :)

server {
    listen       80;
    server_name  webserver.local;

    location / {
        root   /path/to/website;
        index  index.php;

        location ~ \.php$ {
            root           /path/to/website;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }

    location ^~ /app {
        alias   /path/to/php-app;
        index  index.php;

        location ~ \.php$ {
            root           /path/to/php-app;
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_param  SCRIPT_FILENAME $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }
    }
}
cpjones44
  • 160
  • 2
  • 8

1 Answers1

9

Your nested location ~ \.php$ block will not find your PHP scripts. The $document_root is set to /path/to/php-app. And $fastcgi_script_name is the same as $uri which still includes the /app prefix.

The correct approach is to use $request_filename and remove your bogus root statement:

location ^~ /app {
    alias   /path/to/php-app;
    index  index.php;

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        include        fastcgi_params;
        fastcgi_param  SCRIPT_FILENAME $request_filename;
    }
}

Always include fastcgi_params before any fastcgi_param statements to avoid them being silently overwritten by the contents of the include file. See this document for details.

Pothi Kalimuthu
  • 6,117
  • 2
  • 26
  • 38
Richard Smith
  • 12,834
  • 2
  • 21
  • 29