Skip to content Skip to sidebar Skip to footer

Laravel 8 + Nginx - App.css And App.js Resources From Public/ Not Loading - 404 Not Found

This might seem like a duplicate question, but it's different.Trust me, I researched the entire stack, laracast, reddit and github for this one I have a Laravel app on a ubuntu VM

Solution 1:

The solution, if you have the same configuration as me, is painfully simple.

I installed my Laravel app in the /home/myUsername/myLaravelAppName with a symlink for index.php in/var/www/myLaravelSiteName - /var/www/myLaravelSiteName being the default nginx root for serving websites - as you know.

The solution: make a symlink also for each of the /public/css/ and /public/js/ directories to /var/www/myLaravelSiteName, because the index.php symlink does not also serve css and js folders from /public/.Apparently this was not so obvious for me.

Or you could symlink the entire /public/ directory to /var/wwwmyLaravelSiteName.

If this still does not work you might have other issues so keep searching as there are other fixes for those. One quick but 'dirty' fix would be using the online CDN links from bootstrap and adding them in app.blade.php. It will work but it's not recommended.

Besides, any new custom css/js file you add will have to be loaded, so the proper way of functioning is preferred. Those will have to have symlinks and well if served individually and not with the entire public/ directory.

PS. do not forget to make the symlink from sites-available to sites-enabled.

My working server block:

server {
    listen 80;
    listen [::]:80;



    root /var/www/laravel;


    add_header X-Frame-Options "SAMEORIGIN";
    add_header X-XSS-Protection "1; mode=block";
    add_header X-Content-Type-Options "nosniff";

    charset utf-8;

    index index.php index.html index.htm ;

    server_name laravel www.laravel;

    location = /favicon.ico { access_log off; log_not_found off; }
    location = /robots.txt  { access_log off; log_not_found off; }
    
    location / {
        try_files $uri$uri/ /index.php?$query_string;
    }

    location ~ \.php$ {
        
        include /etc/nginx/fastcgi_params;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/run/php/php7.4-fpm.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        try_files $uri = 404;
    }

    location ~* \.(jpg|jpeg|png|gif|css|js|ico|html)$ {

        access_log off;
        expires max;
        log_not_found off;
    }


    location ~/\.ht {
        deny all;
    }

    sendfile off;

}

Post a Comment for "Laravel 8 + Nginx - App.css And App.js Resources From Public/ Not Loading - 404 Not Found"