0

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 with nginx. The issue I have with my Laravel 8 app is that it's not loading the app.css and app.js files from public/. I have ran npm install & npm run dev/prod but I still got the 404 error - not found in the chrome console. So my app.css and .js are alive and well in my public/css - /js folders.The path is also generated correctly for the resources/assets.Still not working. I have tried several server block configurations, with no succes.

If you have the same issue and have tried npm install and run dev, using the asset helper function to generate the path, <script src="{{ asset('/js/app.js') }}" defer></script> and <link href="{{ asset('/css/app.css') }}" rel="stylesheet"> and it's still not working check my answer bellow, *it might save you the 1 week I spent searching.*emphasized text

1 Answer 1

0

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;

}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.