Make symfony works in a subfolder with nginx

August 15, 2019 87 views
Nginx Ubuntu 18.04

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.

1 Answer

Hello,

I think that you’ve made a small mistake in this part of your server block:

        # Symfony location block
        location /app {
            alias /var/www/my.domain/app/public;
            index index.php;

            rewrite ^/app/(.*)$ /$1 break;
            try_files $uri @symfonyFront;


        }

You need to update this to:

        # Symfony location block
        location /app {
            root /var/www/my.domain/app/public;
            index index.php;

            rewrite ^/app/(.*)$ /$1 break;
            try_files $uri @symfonyFront;


        }

Basically you should change the alias part with root. That is how you specify the directory that the files should be loading from.

Then note that it is always recommended to test your Nginx config before restarting:

nginx -t

If you get Syntax OK you could go ahead and restart Nginx.

Hope that this helps!
Let me know how it goes.
Regards,
Bobby

Have another answer? Share your knowledge.