且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Laravel 8 + Nginx-来自public/的app.css和app.js资源未加载-找不到404

更新时间:2022-12-29 13:29:49

如果您的配置与我相同,则该解决方案非常简单.

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

我在/home/myUsername/myLaravelAppName 中安装了Laravel应用,并在/var/www/myLaravelSiteName - /var/www/myLaravelSiteName 是服务网站的默认Nginx根目录-

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.

解决方案:还为/public/css/和/public/js/目录中的每个都建立符号链接到 /var/www/myLaravelSiteName ,因为 index.php symlink 也不同时提供/public/中的css和js文件夹 .对我来说很明显.

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.

或者您可以将整个/public/目录符号链接到/var/wwwmyLaravelSiteName .

如果这仍然不起作用,则可能还有其他问题,请继续搜索,因为有针对这些问题的其他修复程序.一种快速但肮脏"的解决方法是使用引导程序中的在线 CDN链接并将其添加到 app.blade.php 中.可以,但是不推荐.

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.

此外,必须添加您添加的任何新的自定义css/js文件,因此首选正确的功能方式.这些服务器必须具有符号链接,并且如果单独使用而不是与整个public/目录一起使用,则必须具有良好的符号链接.

PS.别忘了将符号链接从可用站点转换为启用站点.

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;

}