【问题标题】:Laravel | nginx config not finding assets拉拉维尔 | nginx 配置找不到资产
【发布时间】:2019-10-10 02:53:28
【问题描述】:

我目前正在尝试使用 Docker 和 Nginx 设置 Laravel 6。

我通过 php-fpm 和 nginx 运行它,并且实际的应用程序加载得非常好。

我面临的问题是它找不到任何资产文件。

我的layout.blade.php顶部有一行

<link rel="stylesheet" type="text/css" href="{{ asset('css/app.css') }}" />

我可以看到该文件确实存在于文件夹中,但它总是在 Chrome 中得到 404。

我正在使用 Laravel 网站上的默认 nginx 配置,并进行了一些小的调整。

events {}

http {
    server {
        listen 80;
        root /var/www/html/public;

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

        index index.php index.html index.htm;

        charset utf-8;

        location / {
            try_files $uri /index.php?$query_string;
        }

        location = /favicon.ico { access_log off; log_not_found off; }
        location = /robots.txt  { access_log off; log_not_found off; }

        error_page 404 /index.php;

        location ~ \.php$ {
            fastcgi_split_path_info ^(.+\.php)(/.+)$;
            fastcgi_pass php:9000;
            fastcgi_index index.php;
            include fastcgi_params;
            fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        }

        location ~ /\.(?!well-known).* {
            deny all;
        }
    }
}

直接导航到 url http://localhost/css/app.css 也会给我 Laravel 默认的 404 页面。

任何帮助将不胜感激!

我还可以进入容器,查看所有文件都在正确的目录中。

【问题讨论】:

  • 有什么解决办法吗?

标签: php laravel nginx


【解决方案1】:

尝试使用 Laravel 建议的 location 块,其中包含 $uri/ ,看看是否有区别:

location / {
    try_files $uri $uri/ /index.php?$query_string;
}

【讨论】:

    【解决方案2】:

    尝试在项目根文件夹中的终端内运行npm run dev,您可能没有编译从一开始就获得的 css/js 文件。因此,您将无法正确获取它们,而是会收到 404 响应。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-25
      • 2017-02-03
      • 2015-08-15
      • 2017-10-06
      • 2016-02-19
      • 2015-03-04
      • 2017-01-07
      • 1970-01-01
      相关资源
      最近更新 更多