显示存储/公共文件夹中的图像时,Docker上的Laravel出现403访问禁止错误

olmpazwi  于 2023-02-15  发布在  Docker
关注(0)|答案(1)|浏览(145)

我在Docker上运行Laravel 9,并使用媒体库上传和编辑文件,目前我将它们上传到存储目录。文件上传到存储文件夹,从浏览器访问它们的URL类似于http://localhost:8888/storage/1/my-test-image.jpg
然而,当试图打开这个网址在一个新的标签页看到的形象,我得到了404错误。
经过一番调查后,我尝试按照文档www.example.com在storagepublic目录之间创建符号链接。我尝试了两种方法,但都出现了403访问禁止错误:a)通过运行php artisan storage:link b)删除以前的符号链接并"手动"创建https://laravel.com/docs/9.x/filesystem#the-local-driver但是我仍然得到403访问禁止错误。 using the command ln -s ../storage/app/public public/storage to create the symlink as a relative path instead. Also I have added in the public/.htaccess file the Options +FollowSymlinks . If I do a ls -la within the public folder I can see the files listed there, so the symlink seems to work, however I still get the 403 access forbidden error.
最后,这些是我在公共目录中拥有的权限

// From outside the running php container
ls -lah public
total 48
-rw-r--r--@ 1 lykos  staff   6.0K Feb  9 19:24 .DS_Store
-rw-r--r--  1 lykos  staff   631B Feb 13 16:16 .htaccess
drwxr-xr-x  3 lykos  staff    96B May 31  2022 css/
-rw-r--r--  1 lykos  staff     0B May 30  2022 favicon.ico
-rw-r--r--  1 lykos  staff   1.7K May 30  2022 index.php
drwxr-xr-x  3 lykos  staff    96B May 31  2022 js/
-rw-r--r--  1 lykos  staff    71B Jun  7  2022 mix-manifest.json
-rw-r--r--  1 lykos  staff    24B May 30  2022 robots.txt
lrwxr-xr-x  1 lykos  staff    21B Feb 13 16:21 storage@ -> ../storage/app/public

// from within the running php container
/var/www/html # ls -lah public/
total 24K    
drwxr-xr-x   11 www-data www-data     352 Feb 14 13:39 .
drwxr-xr-x   37 root     root        1.2K Feb 14 13:39 ..
-rw-r--r--    1 www-data www-data    6.0K Feb  9 19:24 .DS_Store
-rw-r--r--    1 www-data www-data     668 Feb 14 17:30 .htaccess
drwxr-xr-x    3 www-data www-data      96 May 31  2022 css
-rw-r--r--    1 www-data www-data       0 May 30  2022 favicon.ico
-rw-r--r--    1 www-data www-data    1.7K May 30  2022 index.php
drwxr-xr-x    3 www-data www-data      96 May 31  2022 js
-rw-r--r--    1 www-data www-data      71 Jun  7  2022 mix-manifest.json
-rw-r--r--    1 www-data www-data      24 May 30  2022 robots.txt
lrwxr-xr-x    1 www-data www-data      21 Feb 13 16:21 storage -> ../storage/app/public
/var/www/html #

文件系统配置为

<?php

return [

    'default' => env('FILESYSTEM_DISK', 'local'),

    'disks' => [

        'local' => [
            'driver' => 'local',
            'root' => storage_path('app'),
            'throw' => false,
        ],

        'public' => [
            'driver' => 'local',
            'root' => storage_path('app/public'),
            'url' => env('APP_URL').'/storage',
            'visibility' => 'public',
            'throw' => false,
        ],

        's3' => [
            'driver' => 's3',
            'key' => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => env('AWS_BUCKET'),
            'url' => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
        ],

    ],

    'links' => [
        public_path('storage') => storage_path('app/public'),
    ],

];

``
我是不是错过了什么?有什么想法我可以修复403(或404)?

4nkexdtk

4nkexdtk1#

由于文件夹所有者是root,因此您收到403。请将web文件夹所有者更改为您的web服务器用户,例如nginx web默认用户www-data
nginx示例:chown -R www-data:www-data projectfolder.

相关问题