Home > Software design >  docker symfony with nginx GET routes not found
docker symfony with nginx GET routes not found

Time:08-25

I have deployed a symfony app using docker. it has two containers: one for my symfony app and another one for nginx

I load the homepage ok but it is making an ajax GET call to fetch /articles. I get error in docker logs:

nginx | 2022/08/24 19:29:10 [error] 30#30: *1 open() "/var/www/app/public/articles" failed (2: No such file or directory)

in my browser console

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>nginx/1.23.1</center>
</body>
</html>

I am not sure why the GET route is translating to a directory in the public folder ? /articles is just a get route to get data from database

here is my nginx config

server {

    listen [::]:443 ssl;
    listen 443 ssl;
    ssl_certificate /etc/letsencrypt/live/mydomain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/mydomain.com/privkey.pem;
    include /etc/letsencrypt/options-ssl-nginx.conf;
    ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem;
    root /var/www/app/public/;
    index index.php;
    server_name mydomain.com;

    location ~ \.php$ {
        root /var/www/app/public/;
        fastcgi_pass   app:9000;
        fastcgi_index index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        include   /etc/nginx/fastcgi_params;
    }

}

CodePudding user response:

when you just access /, the server naturally looks for the index you named (index.php, in the line index index.php).

nginx will - besides from that - always try to find the exact file that matches the URI - if no matching rewrite/location/whatever is found - for example /articles. This is obviously the default for a web server like nginx, serving files whose path on disk match the URI. However, you don't have a file called articles in your public folder (which might have had existed, from nginx's standpoint).

But it doesn't find that, so it will try to match configured locations. But /articles doesn't match \.php$ (obviously, but it did match index.php before).

Your nginx config is missing the try_files directive which is usually used in routing without a matching file structure. It is also missing the location / block, which it would fallback on, if no other location was matching. Your config therefor doesn't apparently represent what you are intending to achieve...

You should take a look on how to configure nginx for symfony - note the try_files in the location / block, which does the magic there, rewriting all urls to a parameter on the index.php script (entry point for symfony) and thus allowing symfony to work its routing magic.

  • Related