【问题标题】:docker unable to load stylesheets for laraveldocker 无法为 laravel 加载样式表
【发布时间】:2019-04-23 22:07:08
【问题描述】:

我创建了一个非常小且简单的 docker-compose,用于使用 laravel 进行小测试。除了我无法加载我的样式表之外,几乎所有东西都可以正常工作。它在代客本地完美运行。

这是我的 docker-compose

version: '2'
services:
    web:
        build:
            context: ./
            dockerfile: ./dockerfiles/web.docker
        ports:
            - 8080:80
            - 9090:8080
       links:
            - app
            - mariadb
    app:
        build:
             context: ./
            dockerfile: ./dockerfiles/app.docker
        volumes:
            - ~/workbench/laravel/testproject:/var/www/testproject
        depends_on:
          - mariadb
    mariadb:
        image: mariadb:10.2
        environment:
            MYSQL_ROOT_PASSWORD: toor
            MYSQL_DATABASE: testproject
            TEST_DB_NAME: laravel_test
            MYSQL_USER: laravel
            MYSQL_PASSWORD: laravel
            MARIADB_DATABASE: testproject
        volumes:
            - ./mariadb:/var/mariadb
        ports:
            - 33061:3306

这是我的 nginx 虚拟主机

server {
listen 80;
index index.php;
root /var/www/testproject/public;

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

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

我可以访问我所有的路线,而且功能似乎工作得很好,只是我的 javascript、图像或样式表都没有加载。 例如,我尝试从这样的模板加载引导程序:

<link rel="stylesheet" href="/homer/vendor/bootstrap/dist/css/bootstrap.css" />

在我的公用文件夹中,我确实有整个目录 /public/homer/vendor/bootstrap/dist/css/bootstrap.css 该文件位于正确的目录中。

我是否遗漏了我必须在虚拟主机中执行的操作以确保可以正常加载这些文件?

我已尝试从

编辑路径
<link rel="stylesheet" href="/homer/vendor/bootstrap/dist/css/bootstrap.css" />

<link rel="stylesheet" href="public/homer/vendor/bootstrap/dist/css/bootstrap.css" />

以及任何形式的公众。

  • /公共/
  • 公开
  • ../../../../public

但似乎没有一个工作。我也不想改变这个,所以我的路径可以在 docker 和 valet 之间保持不变。

【问题讨论】:

  • 我猜你需要先将 css 复制到你的公共目录,php artisan asset:publish vendor/package
  • 如前所述,我的样式表在我的公共目录中,它们不是我需要发布的包的一部分。
  • 对不起!你可以试试&lt;link rel="stylesheet" href="{{url('afer_public_path/bootstrap.css'}}" /&gt; 吗?
  • 我已经按照下面的回答找到了问题所在。遗憾的是,当我尝试时,您的解决方案也不起作用。

标签: laravel docker nginx


【解决方案1】:

这是一个老问题,但今天我遇到了同样的问题,并通过向 web 服务添加相同的volumes 选项来解决它。在这个 nginx 能够查找 public 目录并在该目录中找到您的所有资产文件之后。

volumes:
    - ~/workbench/laravel/testproject:/var/www/testproject

如果您没有在 web 服务中提供卷选项,docker 将不会将文件映射到容器,/var/www/testproject 目录中将没有文件,因此无法找到文件。

【讨论】:

    【解决方案2】:

    显然我必须将索引 server.php 添加到我的 vhost.conf 中,所以它看起来如下:

    server {
    listen 80;
    index index.php server.php;
    root /var/www/testproject/public;
    
    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }
    
    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass app:9000;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }
    }
    

    但是我仍然不确定这是如何工作的,因为我将根设置在只有 index.php 的公用文件夹中。 server.php 仅加载 index.php,我是否在这里遗漏了有关 nginx 索引如何工作的内容?

    【讨论】:

      【解决方案3】:

      尝试使用 Laravel 内置的 asset() 方法,该方法会自动将主机添加到您的 public 文件夹中。

      <link rel="stylesheet" href="{{ asset('/bootstrap/dist/css/bootstrap.css') }}" />
      

      注意:您可能必须使用 Mix 将您的 bootstrap 文件夹移动到 public 目录,或者自己做。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-08
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多