【问题标题】:Laravel: 403 when trying to access any folder/file in /publicLaravel:尝试访问 /public 中的任何文件夹/文件时出现 403
【发布时间】:2016-04-08 17:21:12
【问题描述】:

我得到了 Laravel 的 4.2 宅基地服务器 (nginx)。 所以,如果我在任何视图中这样写:

<script src="media/js/application.js"></script>
<link rel="icon" href="media/img/branding/favicon.png" type="image/x-icon">
<link rel="stylesheet" href="media/css/markdown.css">

它包含所有文件,但下面的代码不起作用(403错误):

<div style="background-image: url(media/img/image.jpg)"></div>

另外,如果我尝试直接访问 /public 中的某些文件夹/文件,它也会返回 403。为什么?这是我的 nginx 站点配置(事实上这是一个自动生成的宅基地配置):

vagrant@homestead:~/work/Timetable$ cat /etc/nginx/sites-enabled/timetable.dev 
server {
    listen 80;
    listen 443 ssl;
    server_name timetable.dev;
    root "/home/vagrant/work/Timetable/public";

    index index.html index.htm index.php;

    charset utf-8;

    location / {
        try_files $uri $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; }

    access_log off;
    error_log  /var/log/nginx/timetable.dev-error.log error;

    sendfile off;

    client_max_body_size 100m;

    location ~ \.php$ {
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass unix:/var/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;
        fastcgi_connect_timeout 300;
        fastcgi_send_timeout 300;
        fastcgi_read_timeout 300;
    }

    location ~ /\.ht {
        deny all;
    }

    ssl_certificate     /etc/nginx/ssl/timetable.dev.crt;
    ssl_certificate_key /etc/nginx/ssl/timetable.dev.key;
}

【问题讨论】:

    标签: http laravel nginx laravel-4 homestead


    【解决方案1】:

    你提出了两个问题,第一个:

    您的媒体文件夹应该在资产文件夹下,然后包含它:

    asset('media/img/image.jpg');

    关于第二个问题,它已经存在于这个网站上:Laravel Homestead: 403 forbidden on nginx

    【讨论】:

    • 1) 不起作用,403 2) 我的 nginx 已经在监听 /public 文件夹了
    • 另外,它是 Laravel 4.2,它没有 assets 文件夹
    • asset 不仅适用于资产,也不会查看资产文件夹。
    【解决方案2】:

    &lt;div style="background-image: url(/media/img/image.jpg)"&gt;&lt;/div&gt;

    在媒体之前添加额外的/

    【讨论】:

    • 根据我的前端经验,我知道 html/css 路径开头的斜线不是必需的
    【解决方案3】:

    我对 nginx 不熟悉,但您是否 100% 确定文件夹权限是应有的权限? 403 转换为禁止表示,在您的配置或权限的某处,它不允许访问。

    如果您尝试chmod -R 777 public 会怎样?如果你这样做,它会起作用吗?

    【讨论】:

      猜你喜欢
      • 2013-11-05
      • 1970-01-01
      • 2016-10-22
      • 2014-08-03
      • 2012-07-26
      • 2014-05-04
      • 1970-01-01
      • 2014-09-22
      • 2015-08-20
      相关资源
      最近更新 更多