Nginx - location with root in other directory and PHP
Running two PHP applications side-by-side, you either need a common document root, or you need two location ~* \.php
(or similar) blocks to ensure the correct SCRIPT_FILENAME
is sent to the fastcgi backend.
Use nested location
blocks to isolate the /cut
subdirectory, and use the ^~
modifier at the top level to avoid other top level regular expression location
blocks from interfering (see this documentation).
The alias
directive (see this documentation) is used to map /cut
to /var/www/cut/public
. The root
directive can only concatenate, which would make /var/www/cut/public/cut
(which you do not want).
However, I would not recommend using the alias
directive with the try_files
directive because of this long term issue.
So, a solution would be to silently rewrite /cut
to /cut/public
and use a value of root /var/www
.
For example:
location ^~ /cut { rewrite ^/cut(.*)$ /cut/public$1 last;}location ^~ /cut/public { root /var/www; try_files $uri $uri/ /cut/index.php$is_args$args; location ~* \.php(/|$) { fastcgi_pass php:9000; fastcgi_split_path_info ^(.+\.php)(/.*)$; include fastcgi_params; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; }}