Azure App Service多虚拟应用配置及Express API部署路径问题排查
我用Express开发了一个API,原本部署在Azure App Service默认路径site\wwwroot能正常运行。现在想把它部署到site\backend路径,配置路径映射后部署相同代码却出现500错误。我删除了site\wwwroot里的代码(怀疑是web.config重复导致),也重启了App Service,但问题依旧。
错误截图

路径映射截图

应用设置
SCM_DO_BUILD_DURING_DEPLOYMENT=true WEBSITE_NODE_DEFAULT_VERSION=~16
Web Config内容
<?xml version="1.0" encoding="utf-8"?> <!-- This configuration file is required if iisnode is used to run node processes behind IIS or IIS Express. --> <configuration> <system.webServer> <webSocket enabled="false" /> <handlers> <!-- Indicates that the server.js file is a node.js site to be handled by the iisnode module --> <add name="iisnode" path="server.js" verb="*" modules="iisnode"/> </handlers> <rewrite> <rules> <!-- Do not interfere with requests for node-inspector debugging --> <rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true"> <match url="^server.js\/debug[\/]?" /> </rule> <!-- First we consider whether the incoming URL matches a physical file in the /public folder --> <rule name="StaticContent"> <action type="Rewrite" url="public{PATH_INFO}"/> </rule> <!-- All other URLs are mapped to the node.js site entry point --> <rule name="DynamicContent"> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/> </conditions> <action type="Rewrite" url="server.js"/> </rule> </rules> </rewrite> <!-- 'bin' directory has no special meaning in node.js and apps can be placed in it --> <security> <requestFiltering> <hiddenSegments> <remove segment="bin"/> </hiddenSegments> </requestFiltering> </security> <!-- Make sure error responses are left untouched --> <httpErrors existingResponse="PassThrough" /> <!-- You can control how Node is hosted within IIS using the following options: * watchedFiles: semi-colon separated list of files that will be watched for changes to restart the server * node_env: will be propagated to node as NODE_ENV environment variable * debuggingEnabled - controls whether the built-in debugger is enabled --> <!--<iisnode watchedFiles="web.config;*.js"/>--> </system.webServer> </configuration>
1. 修正路径映射的物理路径格式
Azure App Service的物理路径需使用正斜杠,确认你的路径映射配置的是/site/backend,而非反斜杠格式的site\backend,路径格式错误会导致IIS无法定位代码目录。
2. 适配子目录的web.config规则
当前web.config的重写规则基于根目录,部署到/backend子路径时需调整规则:
- 修改
handlers节点,指向子目录的入口文件:<add name="iisnode" path="/backend/server.js" verb="*" modules="iisnode"/> - 修改静态资源重写规则,添加子目录前缀:
<rule name="StaticContent"> <action type="Rewrite" url="/backend/public{PATH_INFO}"/> </rule> - 修改动态请求重写规则:
<rule name="DynamicContent"> <conditions> <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/> </conditions> <action type="Rewrite" url="/backend/server.js"/> </rule>
3. 开启iisnode日志排查细节
在<system.webServer>节点下添加iisnode配置,开启日志定位具体错误:
<iisnode nodeProcessCommandLine=""%programfiles%\nodejs\%WEBSITE_NODE_DEFAULT_VERSION%\node.exe"" watchedFiles="web.config;*.js;backend\*.js" loggingEnabled="true" logDirectory="iisnode"/>
日志会生成在site\backend\iisnode目录,可查看Node进程启动失败的具体原因(如依赖缺失、代码报错)。
4. 验证部署文件完整性
通过Kudu控制台(https://<你的应用名>.scm.azurewebsites.net/DebugConsole)进入site\backend目录,检查:
server.js是否存在node_modules目录是否完整,若缺失则手动执行npm installpackage.json的start命令是否正确指向server.js
5. 查看详细错误日志
在Azure门户的App Service中,进入诊断和解决问题 -> 应用程序日志开启详细错误日志,或查看Kudu的LogFiles目录下的IIS日志,获取500错误的具体触发原因。
6. 核对Node版本兼容性
确保WEBSITE_NODE_DEFAULT_VERSION=~16与本地开发环境的Node版本一致,版本差异可能导致依赖包兼容性问题。同时查看Kudu的构建日志,确认SCM_DO_BUILD_DURING_DEPLOYMENT=true触发的构建过程无报错。
内容的提问来源于stack exchange,提问作者Afiq Rosli

