Laravel 和 Nginx 静态文件

Laravel and Nginx static files

所以我在让 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 文件 angular 模板等...

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

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

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

我有一个类似的问题,运行 Laravel 在 Windows 10 的 Homestead 上。在我的例子中,我可以访问 public\css 目录中的文件,但不能来自 public\css\vendor。我意识到我从 Windows 创建了 vendor 目录,结果里面的文件不能被 ngingx 访问。

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

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

最佳做法是确保您的资产仅深入您的应用程序

例如

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

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

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

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