如何让PHP路由库Steampixel\Route在Azure App Service正常运行?
解决方案
首先明确你的Azure App Service运行环境,对应修改配置:
场景1:使用Windows运行环境(IIS)
web.config缺少目录匹配规则,修改为以下内容即可:
<?xml version="1.0" encoding="UTF-8"?> <configuration> <system.webServer> <rewrite> <rules> <rule name="redirect all requests" stopProcessing="true"> <match url="^(.*)$" ignoreCase="false" /> <conditions logicalGrouping="MatchAll"> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" pattern="" ignoreCase="false" /> <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" pattern="" ignoreCase="false" /> </conditions> <action type="Rewrite" url="index.php" appendQueryString="true" /> </rule> </rules> </rewrite> </system.webServer> </configuration>
修改后重新部署到根目录即可。
场景2:使用Linux运行环境
Windows的web.config配置在Linux环境下完全不生效,根据你使用的Web服务选择对应方案:
- Apache服务:默认关闭了
.htaccess解析权限,进入Azure门户的App Service管理页,依次点击「配置」→「常规设置」,在启动命令输入框填入以下内容保存后重启服务即可:sed -i 's/AllowOverride None/AllowOverride All/g' /etc/apache2/apache2.conf && service apache2 restart - Nginx服务:
.htaccess不生效,需要添加Nginx重写规则,你可以在启动命令中注入自定义配置,核心重写规则如下:location / { try_files $uri $uri/ /index.php?$query_string; }
额外排查点
如果上述配置修改后仍未解决,做以下验证:
- 确认应用部署在域名根路径下,若部署在二级路径,需要将
Route::run('/')修改为Route::run('/你的二级路径/') - 在
index.php最顶部添加代码var_dump($_SERVER['REQUEST_URI']);exit;,访问/testroute如果能打印出/testroute说明重写规则已生效,问题出在路由库配置;如果仍然返回404说明重写规则未正确部署。
内容的提问来源于stack exchange,提问作者DanielGPR
相关产品推荐
相关产品推荐

