Configuring nginx to serve a Symfony project in a subdirectory of a wordpress website.
It is surprisingly non-trivial to configure Nginx to serve two different PHP applications on the same domain, one being in a logical subdirectory.
I spent a few hours scouring the web and trying different things. I ended up with this configuration, thanks from a source (of which I lost track. Sorry!)
The configuration file defines a server listening for server mysite.com
on port 80
. The WordPress application is located in /var/www/wordpress
and the Symfony application is located in /var/www/symfony
.
When a browsers requests the http://mysite.com/subdirectory
resource, the request is passed to the Symfony app. Otherwise, the request goes to the WordPress app.
server {
listen 80;
listen [::]:80;
server_name mysite.com;
root /var/www/wordpress;
index index.php app.php index.html;
location /subdirectory {
root $symfonyRoot;
rewrite ^/subdirectory/(.*)$ /$1 break;
try_files $uri @symfonyFront;
}
location / {
try_files $uri $uri/ /index.php?$args;
}
set $symfonyRoot /var/www/symfony/web;
set $symfonyScript app.php;
# This is for the Symfony application
location @symfonyFront {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
include /etc/nginx/fastcgi_params;
fastcgi_param SCRIPT_FILENAME $symfonyRoot/$symfonyScript;
fastcgi_param SCRIPT_NAME /subdirectory/$symfonyScript;
fastcgi_param REQUEST_URI /subdirectory$uri?$args;
}
# This is for the wordpress app
location ~ \.php {
fastcgi_pass unix:/var/run/php/php7.2-fpm.sock;
fastcgi_index index.php;
fastcgi_param PATH_INFO $fastcgi_path_info;
fastcgi_param PATH_TRANSLATED $document_root$fastcgi_path_info;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param REQUEST_URI $uri?$args;
include /etc/nginx/fastcgi_params;
}
}
I also created a public gist with this configuration file.