I've been fighting with this the last couple days and I am hoping someone with a deeper nginx understanding can point me in the right direction.
I have need to serve a secondary PHP application from a subdirectory within an existing PHP applications "root" using PHP-FPM.
The structure is
- /var/www/www.example.com/
- public/
- index.php
- subapplication/
- public/
- index.php
Both of these applications would be available from the same domain at www.example.com
and www.example.com/subapplication/
.
I can serve either of these applications directly with nginx but I am struggling to get my directives to work so that I can properly serve both the main and sub applications.
This is the configuration I have at this point that feels closest, but it's still not quite working.
server {
listen 80 default_server;
server_name www.example.com;
set $base /var/www/www.example.com;
index index.php;
location /subapplication/ {
alias $base/subapplication/public;
try_files $uri /subapplication/index.php?$query_string;
location ~ \.php$ {
# 404
try_files $fastcgi_script_name =404;
# default fastcgi_params
include fastcgi_params;
# fastcgi settings
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
# fastcgi params
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}
location / {
root $base/public;
try_files $uri $uri/ /index.php?$query_string;
location ~ \.php$ {
# 404
try_files $fastcgi_script_name =404;
# default fastcgi_params
include fastcgi_params;
# fastcgi settings
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_buffers 8 16k;
fastcgi_buffer_size 32k;
# fastcgi params
fastcgi_param DOCUMENT_ROOT $realpath_root;
fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
}
}
}
Am I missing something obvious?
Currently I simply get a 404
for all requests to the /subapplication/
route. Adding $uri/
to the try_files
under the location /subapplication/
gives me a 403
stating it can't index the subapplication/public
folder.
I would deeply appreciate any insight someone might be able to offer.