2017-06-01 83 views
1

我正在使用Azure WebApps構建靜態頁面和節點網站的組合,我想定製404頁面,但我無法使其工作。 大多數網站是靜態的,但我有幾條需要服務器代碼的路線。我的web.config看起來如下:帶節點和Azure Web應用程序的自定義靜態錯誤頁面

<?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不同變化的pathresponseMode值,除去existingResponse="PassThrough"等,但我不能讓它工作。

直接訪問/404.html作品。訪問不存在的url時,服務器返回404錯誤,而不是我想要的自定義URL。 我在做什麼錯?

我知道我可以處理nodeland的所有事情,但是我希望儘可能避免這種情況。

回答

2

我能夠在Azure App Service上獲得自定義404.html頁面與以下web.config文件和expressjs應用程序一起使用。你可以把它作爲參考。

的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> 

文件夾結構

enter image description here

當我訪問任何文件不存在,我得到了顯示404頁。

enter image description here

+0

我不知道我在做什麼錯誤,但工作。非常感謝! –

相關問題