Laravel迁移Azure Web Apps遇Nginx 404错误,寻求解决办法
Laravel迁移至Azure Web Apps后Nginx 404错误解决方法
你的问题核心在于现有nginx配置不符合Azure Web Apps的运行环境要求,以下是针对性的修正方案:
一、替换适配Azure的nginx.config配置
将你当前的nginx.config替换为以下内容:
server { listen 80; root /home/site/wwwroot/public; index index.php index.html index.htm; add_header X-Frame-Options "SAMEORIGIN"; add_header X-XSS-Protection "1; mode=block"; add_header X-Content-Type-Options "nosniff"; 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 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name; include fastcgi_params; } location ~ /\.(?!well-known).* { deny all; } }
关键修正点说明:
- 根目录调整:Azure Web Apps中Laravel的正确根目录是
/home/site/wwwroot/public,而非常规VPS的/var/www/html - PHP-FPM连接方式:Azure默认使用TCP端口9000而非Unix套接字,因此替换
fastcgi_pass为127.0.0.1:9000 - 移除冗余SSL配置:Azure Web Apps会自动管理HTTPS跳转和SSL证书,无需手动添加Certbot相关配置
- 路由转发规则:修正
try_files为Laravel标准规则,确保所有非静态资源请求转发到index.php
二、Azure门户配置检查
- 设置网站根目录:
进入Azure Web App的「配置」→「虚拟应用程序和目录」,将/的物理路径设置为site\wwwroot\public - 确认PHP版本:
在「配置」→「常规设置」中,确保PHP版本与你的Laravel版本兼容(例如Laravel 9+推荐PHP 8.1及以上)
三、清理Laravel项目缓存
通过Azure Web App的「高级工具」→「SSH」连接到服务器,执行以下命令:
php artisan config:clear php artisan cache:clear php artisan route:clear php artisan view:clear
四、验证.env文件配置
确保.env中的关键参数正确:
APP_URL=https://你的Azure域名 APP_ENV=production APP_KEY=你的应用密钥
内容的提问来源于stack exchange,提问作者Esa Ali
相关产品推荐
相关产品推荐

