带有节点和 Azure WebApp 的自定义静态错误页面
Custom static error page with node and Azure WebApp
我正在使用 Azure WebApps 构建静态页面和节点网站的组合,我想要一个自定义 404 页面,但我无法让它工作。
大多数网站都是静态的,但我有几条路线需要服务器代码。我的 web.config 看起来如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By"/>
<add name="x-dns-prefetch-control" value="on"/>
</customHeaders>
</httpProtocol>
<handlers>
<add name="iisnode" path="src/server/index.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="static">
<match url="(?!dynamicroute).*$" ignoreCase="true"/>
<action type="Rewrite" url="dist{REQUEST_URI}"/>
</rule>
<rule name="dynamic">
<match url="(?:dynamicroute)(.*)$" ignoreCase="true"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="src/server/index.js"/>
</rule>
</rules>
</rewrite>
<!-- Make sure error responses are left untouched -->
<!--<httpErrors existingResponse="PassThrough"/>-->
<httpErrors errorMode="Custom" defaultResponseMode="File" existingResponse="PassThrough">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="~/404.html" responseMode="File" />
</httpErrors>
</system.webServer>
</configuration>
我尝试了 httpErrors
的不同变体、path
的值、responseMode
、删除 existingResponse="PassThrough"
等,但我就是做不到工作。
直接访问 /404.html
有效。服务器 returns 在访问不存在的 url 时出现 404 错误,这不是我想要的自定义对象。
我做错了什么?
我知道我可以处理来自 nodeland 的一切,但我想尽可能避免这种情况。
我能够使用以下 web.config
文件和 Azure 应用服务上的 expressjs 应用程序获得自定义 404.html
页面。可以作为参考。
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
<webSocket enabled="false" />
<handlers>
<!-- Indicates that the app.js file is a node.js site to be handled by the iisnode module -->
<add name="iisnode" path="app.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="^app.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{REQUEST_URI}"/>
</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="app.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>
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/public/404.html" responseMode="ExecuteURL" />
</httpErrors>
<iisnode watchedFiles="web.config;*.js" debuggingEnabled="false" />
</system.webServer>
</configuration>
文件夹结构
当我访问任何不存在的文件时,我会显示 404 页面。
我正在使用 Azure WebApps 构建静态页面和节点网站的组合,我想要一个自定义 404 页面,但我无法让它工作。 大多数网站都是静态的,但我有几条路线需要服务器代码。我的 web.config 看起来如下:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By"/>
<add name="x-dns-prefetch-control" value="on"/>
</customHeaders>
</httpProtocol>
<handlers>
<add name="iisnode" path="src/server/index.js" verb="*" modules="iisnode"/>
</handlers>
<rewrite>
<rules>
<rule name="static">
<match url="(?!dynamicroute).*$" ignoreCase="true"/>
<action type="Rewrite" url="dist{REQUEST_URI}"/>
</rule>
<rule name="dynamic">
<match url="(?:dynamicroute)(.*)$" ignoreCase="true"/>
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="True"/>
</conditions>
<action type="Rewrite" url="src/server/index.js"/>
</rule>
</rules>
</rewrite>
<!-- Make sure error responses are left untouched -->
<!--<httpErrors existingResponse="PassThrough"/>-->
<httpErrors errorMode="Custom" defaultResponseMode="File" existingResponse="PassThrough">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="~/404.html" responseMode="File" />
</httpErrors>
</system.webServer>
</configuration>
我尝试了 httpErrors
的不同变体、path
的值、responseMode
、删除 existingResponse="PassThrough"
等,但我就是做不到工作。
直接访问 /404.html
有效。服务器 returns 在访问不存在的 url 时出现 404 错误,这不是我想要的自定义对象。
我做错了什么?
我知道我可以处理来自 nodeland 的一切,但我想尽可能避免这种情况。
我能够使用以下 web.config
文件和 Azure 应用服务上的 expressjs 应用程序获得自定义 404.html
页面。可以作为参考。
web.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.webServer>
<!-- Visit http://blogs.msdn.com/b/windowsazure/archive/2013/11/14/introduction-to-websockets-on-windows-azure-web-sites.aspx for more information on WebSocket support -->
<webSocket enabled="false" />
<handlers>
<!-- Indicates that the app.js file is a node.js site to be handled by the iisnode module -->
<add name="iisnode" path="app.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="^app.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{REQUEST_URI}"/>
</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="app.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>
<httpErrors errorMode="Custom" defaultResponseMode="ExecuteURL">
<remove statusCode="404" subStatusCode="-1" />
<error statusCode="404" path="/public/404.html" responseMode="ExecuteURL" />
</httpErrors>
<iisnode watchedFiles="web.config;*.js" debuggingEnabled="false" />
</system.webServer>
</configuration>
文件夹结构
当我访问任何不存在的文件时,我会显示 404 页面。