AWS部署Laravel项目:/public路径冗余及路由NotFound问题求助
解决方案:移除Laravel URL中的/public后缀并修复路由问题
核心原则
Laravel的入口文件是public/index.php,正确的做法是让Web服务器直接指向public目录——既保证安全(避免暴露.env等敏感文件),又能彻底去掉URL中的/public后缀。
一、Apache环境配置(AWS EC2等)
修改虚拟主机配置
编辑Apache站点配置文件(路径通常为/etc/httpd/conf.d/your-site.conf或/etc/apache2/sites-available/your-site.conf),确保DocumentRoot指向项目的public目录:<VirtualHost *:80> ServerName your-domain.com DocumentRoot /var/www/your-project/public <Directory /var/www/your-project/public> AllowOverride All Require all granted Options -Indexes +FollowSymLinks </Directory> ErrorLog /var/log/httpd/your-site-error.log CustomLog /var/log/httpd/your-site-access.log combined </VirtualHost>确保public目录的.htaccess正确
检查public/.htaccess是否为Laravel默认配置,若缺失或被修改,替换为以下内容:<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] # Send Requests To Front Controller... RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^ index.php [L] </IfModule>重启Apache服务
sudo systemctl restart httpd # Debian/Ubuntu系统执行:sudo systemctl restart apache2
二、Nginx环境配置
编辑Nginx站点配置
创建或修改站点配置文件(路径通常为/etc/nginx/sites-available/your-site.conf),设置root指向public目录,并配置try_files规则:server { listen 80; server_name your-domain.com; root /var/www/your-project/public; add_header X-Frame-Options "SAMEORIGIN"; add_header X-Content-Type-Options "nosniff"; index index.php; charset utf-8; location / { try_files $uri $uri/ /index.php?$query_string; } location = /favicon.ico { access_log off; log_not_found off; } location = /robots.txt { access_log off; log_not_found off; } error_page 404 /index.php; location ~ \.php$ { fastcgi_pass unix:/var/run/php/php8.2-fpm.sock; # 根据你的PHP版本调整路径 fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } }启用站点并重启Nginx
sudo ln -s /etc/nginx/sites-available/your-site.conf /etc/nginx/sites-enabled/ sudo nginx -t # 检查配置合法性 sudo systemctl restart nginx
三、Livewire & Filament 专属检查
确认.env配置
确保APP_URL不带/public后缀:APP_URL=http://your-domain.com清理并重建缓存
php artisan config:cache php artisan route:cache # 仅生产环境使用,开发环境可跳过 php artisan view:clearLivewire资源同步
重新发布Livewire静态资源后执行构建:php artisan livewire:publish --assets npm run build若本地构建后上传服务器,需确保
public/build目录的文件同步完成。Filament初始化检查
执行以下命令确保Filament资源配置正确:php artisan filament:install
四、常见问题排查
- 删除根目录下自行添加的
.htaccess文件,避免和public目录的规则冲突。 - 禁止将public目录的文件移动到根目录,这会暴露.env等敏感文件,存在严重安全风险。
- 修复目录权限:确保Web服务器用户(apache/nginx/www-data)拥有
storage和bootstrap/cache目录的写入权限:sudo chown -R www-data:www-data /var/www/your-project/storage /var/www/your-project/bootstrap/cache sudo chmod -R 755 /var/www/your-project/storage /var/www/your-project/bootstrap/cache
内容的提问来源于stack exchange,提问作者Xabed hossain
相关产品推荐
相关产品推荐

