Skip to content

Instantly share code, notes, and snippets.

@marcw
Created January 22, 2018 18:17
Show Gist options
  • Save marcw/2bebd7094b649a627a171ee1d5e34b26 to your computer and use it in GitHub Desktop.
Save marcw/2bebd7094b649a627a171ee1d5e34b26 to your computer and use it in GitHub Desktop.
Nginx configuration to serve a Symfony app under a subdirectory of a PHP application
# With this nginx configuration, you will be able to serve a Symfony app in a subdirectory
# of a wordpress (or any other PHP application).
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;
}
}
@Davidmattei
Copy link

Hero! Thanks

@simonkulessa
Copy link

simonkulessa commented Aug 8, 2018

Mhm, tried it with our nginx server - doesn´t work :(

current directory structure is:

/var/www/demo.domain.com/web (Frontend, Single Page React App)
/backend/web (Symfony Application)

But it simply doesn´t work :(

Edit:

We found out, that is seems to be connected to symfony routes. Did you change anything, when putting symfony into a subdirectory/location?

Regards
Simon

@jeroendk
Copy link

Needed this, thanks!

@giorgioma
Copy link

giorgioma commented Jan 12, 2022

Cool stuff, this really pointed me in the right direction!!

Worth noting to remove the subdirectory:

fastcgi_split_path_info ^/subdirectory/(.+\.php)(/.*)$;

@kaloyan13
Copy link

Thank you. Works with Mapbender!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment