0

This question is imported from SO. I kept the edit made with the comments so we dont have to go over it once more :). The link to the OP: https://stackoverflow.com/questions/57503107/trouble-making-nginx-works-with-symfony-in-subdirectory

I'm trying to setup Symfony 4 next to an already working wordpress site with nginx. Wordpress should manage the homepage at

https://www.my.domain

and the blog posts under

https://www.my.domain/conseils

The symfony application should take over for anything under

https://www.my.domain/app

I've read the default documentation bot here and here aswell as some troubleshooting made here. Furthermore, it seems REALLY close to that answered question but with nginx instead of apache: https://stackoverflow.com/questions/53895202/symfony4-routing-inside-a-subfolder

The fact is i still cant manage to make it working. Here's my current site.conf which gives me a 404, but i cant really find some helpful log even with the debug option enabled in nginx.

 server { listen 80 default_server; listen [::]:80 default_server; server_name www.my.domain; root /var/www/my.domain/html; #symfony location block location /app { alias /var/www/my.domain/app/public; index index.php; try_files $uri /app/public/index.php/$1 last; include snippets/fastcgi-php.conf; fastcgi_param SCRIPT_FILENAME $request_filename; fastcgi_pass unix:/var/run/php/php7.2-fpm.sock; fastcgi_split_path_info ^(.+\.php)(/.*)$; error_log /var/log/nginx/project_error.log; access_log /var/log/nginx/project_access.log; } #Wordpress location block location / { index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; } location ~* \.(css|js|ico|gif|jpeg|jpg|webp|png|svg|eot|otf|woff|woff2|ttf|ogg)$ { expires max; } location ~ /\.ht { deny all; } } } 

Lastly i tried something found here. This times, it gives me the following symfony routing error: No route found for "GET /app". Below the conf file:

server { listen 80 default_server; listen [::]:80 default_server; server_name www.my.domain; root /var/www/my.domain/html; # Symfony location block location /app { alias /var/www/my.domain/app/public; index index.php; rewrite ^/app/(.*)$ /$1 break; try_files $uri @symfonyFront; } set $symfonyRoot /var/www/my.domain/app/public; set $symfonyScript index.php; location @symfonyFront { include snippets/fastcgi-php.conf; 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 /app/$symfonyScript; fastcgi_param REQUEST_URI /app$uri?$args; } #Wordpress location block location / { index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; } location ~* \.(css|js|ico|gif|jpeg|jpg|webp|png|svg|eot|otf|woff|woff2|ttf|ogg)$ { expires max; } location ~ /\.ht { deny all; } } listen [::]:443 ssl ipv6only=on; # managed by Certbot listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/www.my.domain/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/www.my.domain/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } 

In both cases, the wordpress is working as intended. Homepage and blog posts are showing when expected. The symfony app however isnt. For the former cas, i got a 404 when visiting my.domain/app and the latter gives me a symfony routing error. That means i'm actually hitting SF front controller. And i got the kernel stack trace saying the route app/ is not found within symfony. And that's absolutely correct since i expect symfony to work under that directory and it should not be user for the routing.

At this point i'm not even sure which one makes me close to the desired result. Any tip would be appreciated!

EDIT: etc/nginx/error.log with debug activated show many lines but this one caught my attention:

2019/08/15 18:11:42 [alert] 6929#6929: *5 "alias" cannot be used in location "/app" where URI was rewritten, client: 86.252.250.94, server: www.my.domain, request: "GET /app/ HTTP/1.1", host: "www.my.domain"

And when i got the route not found error, i got that (which is expected) in the symfony log:

[2019-08-15 18:26:00] request.ERROR: Uncaught PHP Exception Symfony\Component\HttpKernel\Exception\NotFoundHttpException: "No route found for "GET /app"" at /var/www/my.domain/app/vendor/symfony/http-kernel/EventListener/RouterListener.php line 141 {"exception":"[object] (Symfony\Component\HttpKernel\Exception\NotFoundHttpException(code: 0): No route found for \"GET /app\" at /var/www/my.domain/app/vendor/symfony/http-kernel/EventListener/RouterListener.php:141, Symfony\Component\Routing\Exception\ResourceNotFoundException(code: 0): No routes found for \"/app/\". at /var/www/my.domain/app/vendor/symfony/routing/Matcher/Dumper/CompiledUrlMatcherTrait.php:70)"} []

EDIT2: i've added the

fastcgi_split_path_info ^/app(.+.php)(/.+)$;

without noticeable change. Interestingly enough, when i replaced the rewrite directive with "last" instead of "break" it displays the 404 page from wordpress.

EDIT: fastcgi-php.conf

@www:~$ sudo cat /etc/nginx/snippets/fastcgi-php.conf # regex to split $uri to $fastcgi_script_name and $fastcgi_path fastcgi_split_path_info ^(.+\.php)(/.+)$; # Check that the PHP script exists before passing it try_files $fastcgi_script_name =404; # Bypass the fact that try_files resets $fastcgi_path_info # see: http://trac.nginx.org/nginx/ticket/321 set $path_info $fastcgi_path_info; fastcgi_param PATH_INFO $path_info; fastcgi_index index.php; include fastcgi.conf; 
1
  • For a similar setup (wp in a subfolder with another wp in root folder) a simple reverse proxy for the specific location worked perfectly. So you just proxy_pass /app to your another nginx vhost listening eg localhost:8080.. give it a try Commented Aug 15, 2019 at 22:05

1 Answer 1

0

First "bug" is having prefixed location (location /something) without trailing slash. It needs to be location /app/ {, otherwise the location matches app123.

The try_files in the app location is over-complicated. Keep it simple:

try_files $uri /index.php$is_args$args; 

Considering you retain your current file structure:

  • /var/www/my.domain/html - Wordpress
  • /var/www/my.domain/app - Symfony

Then:

server { listen 80 default_server; listen [::]:80 default_server; server_name www.my.domain; root /var/www/my.domain/html; #Wordpress location block location / { index index.php index.html index.htm; try_files $uri $uri/ /index.php?$args; location ~* \.(css|js|ico|gif|jpeg|jpg|webp|png|svg|eot|otf|woff|woff2|ttf|ogg)$ { expires max; } location ~ /\.ht { deny all; } } #symfony location block location ^~ /app/ { index index.php; error_log /var/log/nginx/project_error.log; access_log /var/log/nginx/project_access.log; alias /var/www/my.domain/app/public/; try_files $uri /index.php$is_args$args; location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; # important to use $request_filename with alias fastcgi_param SCRIPT_FILENAME $request_filename; } } # redirect /app to app/ location = /app { return 301 /app/; } location ~ \.php$ { include snippets/fastcgi-php.conf; fastcgi_pass unix:/run/php/php7.2-fpm.sock; } } 
11
  • Thank you Danlia, and sorry for the late reply, i took 2 days off. When doing that, i get the following error from nginx -t : nginx: [emerg] "alias" directive is duplicate, "root" directive was specified earlier in /etc/nginx/sites-enabled/my.domain:151 nginx: configuration file /etc/nginx/nginx.conf test failed I tried so swap them, but didnt manage to make it work as expected. WHen only root is specified to the public/ directory, then i got a 404 generated by Wordpress. Symfony doesnt seem to be triggered. Commented Aug 20, 2019 at 18:44
  • @KévinBruneau see the updated answer. Let me know if it works :) Commented Aug 20, 2019 at 19:43
  • /app is redirected to /app/ which leads to a wordpress 404. No error in nginx.log, Entry in access log seems fine. Commented Aug 20, 2019 at 20:44
  • Do you want me to add debug log to nginx ? If so, tell me what i should search in it :) Commented Aug 20, 2019 at 20:51
  • Can you list contents of snippets/fastcgi-php.conf? Commented Aug 20, 2019 at 20:54

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.