【问题标题】:Laravel 8 + nginx - app.css and app.js resources from public/ not loading - 404 not foundLaravel 8 + nginx - 来自公共/未加载的 app.css 和 app.js 资源 - 未找到 404
【发布时间】:2021-05-25 23:26:13
【问题描述】:

这似乎是一个重复的问题,但它是不同的。相信我,我为此研究了整个堆栈、laracast、reddit 和 github

我在ubuntu VMnginx 上有一个Laravel 应用程序。我遇到的问题 我的 Laravel 8 应用程序没有加载 app.cssapp.js 来自public/ 的文件。我已经跑了npm install & npm run dev/prod 但我仍然收到 404 错误 - 在 chrome 中找不到 控制台。 所以我的app.css and .js 在我的 public/css - /js 文件夹。路径也正确生成 资源/资产。仍然无法正常工作。我尝试了几个server block 配置,但没有成功。

如果您有同样的问题并尝试过npm install and run dev, 使用asset 辅助函数生成path<script src="{{ asset('/js/app.js') }}" defer></script><link href="{{ asset('/css/app.css') }}" rel="stylesheet"> 仍然没有 工作检查下面我的答案,*它可能会为您节省我花费的 1 周 搜索。*强调文本

【问题讨论】:

    标签: javascript css nginx http-status-code-404 laravel-8


    【解决方案1】:

    如果你的配置和我一样的话,解决方法非常简单。

    我在/home/myUsername/myLaravelAppName 中安装了我的 Laravel 应用程序,其中 index.php 的符号链接在/var/www/myLaravelSiteName - /var/www/myLaravelSiteName 是默认的 nginx 根用于服务网站 - 如您所知。

    解决方案:也为每个 /public/css/ 和 /public/js/ 目录创建一个符号链接到 /var/www/myLaravelSiteName,因为index.php symlink 也不提供来自 /public/ 的 css 和 js 文件夹。显然这对我来说并不那么明显。

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

    如果这仍然不起作用,您可能还有其他问题,因此请继续搜索,因为还有其他解决方法。 一种快速但“肮脏”的解决方法是使用引导程序中的在线CDN 链接并将它们添加到app.blade.php。它会起作用,但不建议这样做。

    此外,您添加的任何新的自定义 css/js 文件都必须加载,因此首选正确的运行方式。如果单独提供而不是与整个 public/ 目录一起提供,这些将必须有符号链接。

    附言。不要忘记将符号链接从可用站点变为已启用站点。

    我的工作服务器块:

    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;
    
    }
    

    【讨论】:

      猜你喜欢
      • 2020-01-16
      • 2021-11-16
      • 2021-02-27
      • 2021-03-18
      • 2018-01-04
      • 2021-08-06
      • 2021-08-02
      • 2022-06-30
      • 2014-09-16
      相关资源
      最近更新 更多