Laravel项目Nginx无法读取软链接返回File not found问题
解决Laravel软链接下Nginx返回"File not found"的问题
核心原因分析
问题的根源在于Nginx运行用户www-data无法访问软链接指向的真实项目目录,同时权限配置重复覆盖、Nginx/PHP-FPM路径解析逻辑存在偏差:
/home/ubuntu默认权限为700,www-data用户无法进入该目录,导致无法读取软链接指向的Laravel文件- 权限命令重复执行,未针对真实项目目录做精准配置
- Nginx配置中重复定义
root,PHP-FPM未使用真实文件路径解析脚本
分步解决方案
1. 修复真实项目目录的权限链
首先确保www-data能访问真实项目的父目录:
# 让www-data用户能进入ubuntu家目录 sudo chmod 750 /home/ubuntu sudo usermod -a -G ubuntu www-data
然后给真实Laravel项目目录配置正确权限:
sudo chown -R ubuntu:www-data /home/ubuntu/domain.name/laravel_server/code sudo find /home/ubuntu/domain.name/laravel_server/code -type f -exec chmod 664 {} \; sudo find /home/ubuntu/domain.name/laravel_server/code -type d -exec chmod 775 {} \; # 确保Laravel读写目录权限正确 sudo chgrp -R www-data /home/ubuntu/domain.name/laravel_server/code/storage /home/ubuntu/domain.name/laravel_server/code/bootstrap/cache sudo chmod -R ug+rwx /home/ubuntu/domain.name/laravel_server/code/storage /home/ubuntu/domain.name/laravel_server/code/bootstrap/cache
2. 优化Nginx配置
移除冗余配置,确保软链接解析正常:
server { index index.php; server_name domain.name www.domain.name; root /var/www/domain.name/html/public/; error_log /var/log/nginx/error.log; access_log /var/log/nginx/access.log; # 允许Nginx解析软链接 disable_symlinks off; location / { autoindex on; autoindex_exact_size on; index index.html index.php; try_files $uri $uri/ /index.php$is_args$args; } location ~ \.php$ { fastcgi_split_path_info ^(.+\.php)(/.+)$; fastcgi_buffering off; fastcgi_buffer_size 2M; fastcgi_buffers 8 2M; fastcgi_pass unix:/var/run/php/php8.1-fpm.sock; fastcgi_index index.php; # 使用真实路径解析PHP脚本,避免软链接路径问题 fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.ht { deny all; } listen 443 ssl; # managed by Certbot ssl_certificate /etc/letsencrypt/live/domain.name/fullchain.pem; # managed by Certbot ssl_certificate_key /etc/letsencrypt/live/domain.name/privkey.pem; # managed by Certbot include /etc/letsencrypt/options-ssl-nginx.conf; # managed by Certbot ssl_dhparam /etc/letsencrypt/ssl-dhparams.pem; # managed by Certbot } server { if ($host = www.domain.name) { return 301 https://$host$request_uri; } # managed by Certbot if ($host = domain.name) { return 301 https://$host$request_uri; } # managed by Certbot listen 80; server_name domain.name www.domain.name; return 404; # managed by Certbot }
关键修改:
- 移除
location /内重复的root配置 - 将
SCRIPT_FILENAME改为$realpath_root$fastcgi_script_name,确保PHP-FPM使用真实文件路径而非软链接路径
3. 重启服务生效
sudo systemctl reload nginx sudo systemctl restart php8.1-fpm
内容的提问来源于stack exchange,提问作者Pawan Bhatia
相关产品推荐
相关产品推荐

