【问题标题】:Images shows 404 on production server(Laravel project)图像在生产服务器上显示 404(Laravel 项目)
【发布时间】:2022-08-17 01:29:24
【问题描述】:

我已经将 laravel 项目部署到带有 LEMP 堆栈的 vps ubuntu 服务器。一切正常,但图像显示 404,即使有指向 storage/app/public 文件夹的符号链接。我认为,这个问题是关于权限的,我尝试了一些权限模块,但还是一样。

这是我的项目,他们的权限:

这是在storage/app/public 文件夹内:

这是我的公共文件夹及其权限和符号链接:

如果需要,这里是我的 nginx 服务器配置(/etc/nginx/sites-available/default):

server {
    listen 80 default_server;
    #listen [::]:80 default_server;

    root /var/www/html/west-hospital-admin/public;
    #root /home/west/west-hospital-admin/public;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;
    server_name _;

    location / {
        try_files $uri $uri/ /index.php$query_string;
    }
    # pass PHP scripts to FastCGI server

    location ~ \\.php$ {
        include snippets/fastcgi-php.conf;
        fastcgi_pass unix:/var/run/php/php8.1-fpm.sock;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
    }
}

我有一个名为 _asset() 的辅助函数:

function _asset($path = null, $data = null): string
    {
        $darkmode = Settings::select(\'darkmode\')->firstOrFail()->darkmode;
        $placeholderImg = $darkmode ? \'mazer/img/no-img-dark.png\' : \'mazer/img/no-img.png\' ;

        return asset(
            $path === null
                ? $placeholderImg
                : ($data === null || $data == \'\'
                    ? (file_exists($path)
                        ? $path
                        : $placeholderImg)
                    : (file_exists(\'uploads/\' . $path . \'/\' . $data)
                        ? \'uploads/\' . $path . \'/\' . $data
                        : $placeholderImg))
        );
    }

我正在加载这样的图像:

<img src=\"{{ _asset(\'images/vacancies\', $vacancy->image) }}\" height=\"60px\" width=\"80px\">

像这样在 html 中加载图像:

http://109.74.199.165/uploads/images/vacancies/164967-1659941875.webp

该目录中有图像,但访问链接时显示404。

  • 请不要发布代码的图像。使用 SO 代码块粘贴您的代码,因此我们不必手动从图像中重新编写代码来测试或修改
  • 我已经用代码本身编辑了帖子
  • 你能显示加载图像的代码吗?
  • 我已经编辑了问题,您可以查看。

标签: php laravel nginx vps laravel-9


【解决方案1】:

我已经想通了。是权限问题。 chmod -R 755 storage 将完成这项工作。但是,如果创建了一个新文件夹,则需要再次授予该文件夹的权限。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 2020-08-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 2021-12-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多