【问题标题】:403 Forbidden on Storage Directory403 Forbidden on Storage Directory
【发布时间】:2019-12-18 09:30:22
【问题描述】:

我有一个使用 cPanel 在 Linux 上托管的网站。我可以访问我网站的所有页面,但“存储”目录中的文件除外。文件结构设置为以我的“公共”文件夹和我的主要网站文件(“tbcrp”)为基础,并将“公共”文件夹作为根/默认目录。文件结构图:https://gyazo.com/e1e958f75c92fdc201f2b5975d109f37

“public”文件夹内是指向 website/storage/app/public 文件夹的符号链接。这个想法是,这允许访问存储的子部分,并且是从文档中访问的常用方法: https://laravel.com/docs/5.8/filesystem#the-public-disk

当我尝试访问位于以下地址的文件时: my-domain.com/storage/profiles/homepage-mockup.jpg

我收到 403 禁止错误。

我的文件结构根目录下的 .htaccess 是:

RewriteEngine on

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.my-domain.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^my-domain.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.my-domain.com$
RewriteCond %{REQUEST_URI} !public/
RewriteRule (.*) /public/$1 [L]

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteCond %{REQUEST_URI} !^public
    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

在我的“公共”文件夹中,我有另一个 .htaccess 文件,其中包含以下内容:

<IfModule mod_rewrite.c>
    <IfModule mod_negotiation.c>
        Options -MultiViews -Indexes
    </IfModule>

    RewriteEngine On

    # Handle Authorization Header
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect Trailing Slashes If Not A Folder...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} (.+)/$
    RewriteRule ^ %1 [L,R=301]

    # Handle Front Controller...
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ index.php [L]
</IfModule>

我没有太多的访问经验,但相同的文件结构在本地工作,所以我认为我的网站存在权限配置错误。

谢谢!

【问题讨论】:

  • 默认情况下,存储目录在 Laravel 上是不可访问的。那是因为它包含敏感信息,例如日志、加密密钥……
  • @EliasSoares 更新了我的问题以包含更多细节。我正在通过此处定义的符号链接访问存储的子部分:laravel.com/docs/5.8/filesystem#the-public-disk
  • 您需要启用以下符号链接:stackoverflow.com/q/12973592/3429323
  • @EliasSoares 在这种情况下,是在基本目录中的 .htacess 文件中,还是在“公共”文件夹/webroot 中?

标签: laravel .htaccess


【解决方案1】:

我遇到了同样的问题,经过大量调试,包括修改 .htaccess 但没有用,我设法确定了导致 403 错误的实际问题。

解决方法很简单,在

配置/文件系统.php 在您定义 public_path 和存储路径的地方,您必须具有相同/相同的 public_path 名称和 storage_path 结束目录名称。

例子:

  1. 不正确:
public_path('brands') => storage_path('storage/app/public/brandimages');

这将产生 403 错误,“since public_path('brands')”与“storage_path('../../../brandsimage”不同')"。

  1. 正确:
public_path('brands') => storage_path('storage/app/public/brands');

现在 public_path('brands') 和 "storage_path('../../../brands')" 是相同的,因此,正确的符号链接会产生,从而解决403错误。

使用以下工匠命令生成符号链接

php artisan storage:link

对于相对链接,请使用以下

php artisan storage:link --relative

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-01
    • 2014-10-03
    • 2018-09-03
    • 2015-05-18
    • 2016-01-31
    • 1970-01-01
    • 2020-06-04
    • 2015-02-27
    相关资源
    最近更新 更多