You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel生产环境(VPS+Nginx)Storage URL连接超时问题求助

Laravel Storage 符号链接在Nginx VPS环境无法访问的解决方案

针对你遇到的「本地环境Storage::disk('a disk')->url($user->image)正常,但Nginx VPS环境下生成的URL超时,复制storage到public可访问但符号链接不行」的问题,按以下步骤排查修复:

1. 修正Nginx配置,允许跟随符号链接

Nginx默认可能限制符号链接访问,需在站点配置的server块中添加静态资源处理规则:

server {
    listen 80;
    server_name 你的VPS_IP;
    root /var/www/your-laravel-app/public;
    index index.php index.html;

    location / {
        try_files $uri $uri/ /index.php?$query_string;
    }

    # 处理静态资源时允许跟随符号链接
    location ~* \.(png|jpg|jpeg|gif|ico|css|js)$ {
        expires 30d;
        add_header Cache-Control "public, no-transform";
        # 关键配置:禁用符号链接限制
        disable_symlinks off;
    }

    # PHP处理配置
    location ~ \.php$ {
        fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # 替换为你的PHP版本对应路径
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

修改后重启Nginx:sudo systemctl restart nginx

2. 检查符号链接的有效性

  • 删除可能出错的旧链接:rm public/storage
  • 重新创建符号链接:php artisan storage:link
  • 验证指向是否正确:执行ls -l public/storage,输出应显示public/storage -> ../storage/app/public

3. 修复storage目录的权限与所有者

VPS上的Web服务用户(通常为www-data)需拥有storage目录的访问权限:

# 修改目录所有者为Web服务用户
sudo chown -R www-data:www-data /var/www/your-laravel-app/storage
# 设置正确权限
sudo chmod -R 755 /var/www/your-laravel-app/storage
sudo chmod -R 644 /var/www/your-laravel-app/storage/app/public/*

4. 清除配置缓存并验证APP_URL

  • 确保.env中的APP_URL为你的VPS IP(无末尾斜杠):APP_URL=http://123.45.67.89
  • 清除配置缓存:
php artisan config:clear
php artisan cache:clear

5. 排查SELinux限制(若VPS开启了SELinux)

若以上步骤无效,可能是SELinux阻止Nginx访问符号链接:

  • 临时关闭SELinux测试:sudo setenforce 0,若能正常访问,再配置永久规则:
# 允许Nginx读写storage目录
sudo chcon -R -t httpd_sys_rw_content_t /var/www/your-laravel-app/storage
# 保存SELinux规则
sudo semanage fcontext -a -t httpd_sys_rw_content_t "/var/www/your-laravel-app/storage(/.*)?"
sudo restorecon -R /var/www/your-laravel-app/storage

内容的提问来源于stack exchange,提问作者Ahsan Ullah Sarbaz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 11:36:29