【问题标题】:Laravel and Nginx static filesLaravel 和 Nginx 静态文件
【发布时间】:2017-06-22 11:25:30
【问题描述】:

所以我在让 Nginx 为我的 Laravel 应用程序提供静态文件时遇到了问题。我可以在 chrome 的开发工具中看到请求是对路径发出的,这些文件应该在哪里(http://mywebsite.com/public/css/style.css)。但它们根本没有被加载。我已经尝试过以多种方式让它工作,但它似乎并没有完成这项工作。有人可以帮我吗?干杯!

  server {

    listen 80;

    server_name mydomainname.com;

    keepalive_timeout   70;
    root /var/www/html/portfolio/public;
    index index.php index.html index.htm index.nginx-debian.html;
    autoindex on;

    charset utf-8;

    location / {
        root   /var/www/html/portfolio/public;
        index index.php index.html index.htm;
        try_files $uri $uri/ /index.php$is_args$args;
    }

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

    access_log off;
    error_log  /var/log/nginx/myapp-error.log error;

    sendfile on;

    client_max_body_size 100m;

    rewrite ^ /index.php last;

    location ~ \.php$ {
        try_files $uri =404;
    fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/run/php/php7.0-fpm.sock;
        fastcgi_index index.php;
        include fastcgi_params;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_intercept_errors off;
        fastcgi_buffer_size 16k;
        fastcgi_buffers 4 16k;
    }

    location ~ /\.ht {
        deny all;
    }
}

基本上,/public 目录中有很多文件没有加载,但应该加载。例如 css、js、角度模板的 html 文件等......

【问题讨论】:

  • 第一个红旗是“公开的”,它不应该出现(例如:example.com/css/whatever.css)。
  • 你需要打开你的日志(nginx 的)并阅读它。 500 很可能是权限问题,但请阅读日志,可能编辑问题并添加日志尾部。如果它的 404 也看看 laravel 日志。另外,您是否通过...public/index.php 访问您的页面?

标签: php laravel nginx assets


【解决方案1】:

正如 Kyslik 所暗示的,这可能是权限问题。

我遇到了类似的问题,在 Windows 10 上的 Homestead 上运行 Laravel。就我而言,我可以从 public\css 目录访问文件,但不能从 public\css\vendor 访问文件。我意识到我从 Windows 创建了vendor 目录,结果,ngingx 无法访问其中的文件。

为了解决这个问题,我删除了 vendor 目录并在 vagrant box 中重新创建了它。

【讨论】:

    【解决方案2】:

    网址不需要在路径中包含/public 部分。 /public 是您的网络资产的根。所以你的网址应该是:

    http://mywebsite.com/css/style.css
    

    【讨论】:

    • 是的,但是当我在这样的路径上卷曲时,我得到 404,这就是为什么我认为它根本没有提供服务
    • @AndrewPomorski 你在public 文件夹中有css 文件夹吗?你在那个文件夹中有 style.css 吗?
    • @Kyslik 是的,完全正确。
    【解决方案3】:

    您所要做的就是了解服务器是完全不同的电脑,并解析全球托管服务的路径范围。

    最佳做法是确保您的资产只深入到您的应用程序中一步

    例如。

    <link href="folder/bootstrap/css/bootstrap.min.css" />

    <link href="bootstrap/bootstrap.min.css" />正确

    服务器不会强调扫描目录。服务器只会更深一步。

    AWS Ec2 实例 Ubuntu x Laravel 应用程序。

    【讨论】:

      猜你喜欢
      • 2016-03-12
      • 2021-09-03
      • 2015-07-30
      • 2017-09-09
      • 2016-07-17
      • 1970-01-01
      • 2014-06-07
      • 2021-05-12
      • 1970-01-01
      相关资源
      最近更新 更多