我试图让多个Node.js应用程序在同一个Azure App Service实例中并行工作。我尝试为每个Node.js应用程序创建一个虚拟应用程序,但我不断收到服务器错误(代码500)。我使用ZipDeploy或Azure DevOps来部署我的应用程序。
我怀疑问题可能是与我的应用程序关联的web.config文件不正确。
以下是我的设置的一些细节:
在我的App Service实例的应用程序设置菜单中,我转到虚拟应用程序和目录设置并输入以下内容:
/ site\wwwroot Application x
/mysite1 site\wwwroot\mysite1 Application x
/mysite2 site\wwwroot\mysite2 Application x
我的wwwroot目录如下所示。为了确保问题不是来自代码本身,我对所有三个使用了相同的Node.js应用程序:
index.html
server.js
scripts
web.config (*)
----- mysite1
|----- index.html
|----- server.js
|----- scripts
|----- web.config (**)
----- mysite2
|----- index.html
|----- server.js
|----- scripts
|----- web.config (***)
我使用默认的web.config文件(provided by Kudu)作为其中三个:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<webSocket enabled="false" />
<handlers>
<add name="iisnode" path="server.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="NodeInspector" patternSyntax="ECMAScript" stopProcessing="true">
<match url="^server.js\/debug[\/]?" />
</rule>
<rule name="StaticContent">
<action type="Rewrite" url="public{REQUEST_URI}"/>
</rule>
<rule name="DynamicContent">
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="server.js"/>
</rule>
</rules>
</rewrite>
<security>
<requestFiltering>
<hiddenSegments>
<remove segment="bin"/>
</hiddenSegments>
</requestFiltering>
</security>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
</configuration>
使用此配置,我可以毫无问题地到达https://myappservice.azurewebsites.net/,但https://myappservice.azurewebsites.net/mysite1只返回服务器错误。
我尝试从类似问题中指示的子目录(**和***)中的web.config文件中取消处理程序,但无济于事。
这三个文件的正确配置是什么?我想重写规则需要调整,但我不确定每个的确切期望值是多少。
我终于通过IIS日志(Kudu上的/LogFiles/W3SVC.../)确定了确切的问题:
ModuleName="RewriteModule", Notification="SEND_RESPONSE", HttpStatus="500", HttpReason="URL Rewrite Module Error.", HttpSubStatus="52", ErrorCode="Cannot create a file when that file already exists.
(0x800700b7)", ConfigExceptionInfo="\\?\D:\home\site\wwwroot\mysite1\web.config ( 16) :Cannot add duplicate collection entry of type 'rule' with unique key attribute 'name' set to 'NodeInspector'
"
需要更改的是子web.config文件中的处理程序和规则名称,以便它们不会与父web.config文件中的名称冲突。现在一切正常。
您可以更改虚拟应用程序和目录设置,如下所示:
/ site\wwwroot Application x
/mysite1 site\mysite1 Application x
/mysite2 site\mysite2 Application x
使用zipdeploy或DevOps进行部署时,站点名称和目标URL如下所示:
有关详细信息,请参阅Deploying multiple virtual directories to a single Azure Website。