一般情況在iis部署web網站都非常順利,但是遇到復雜環境,或者被配置過又正在使用的時候,就束手無策了,
因為對IIS和Web.config不熟悉,不知其中要害,導致浪費一天甚至更久的時間去處理一個可能是不起眼的配置問題
本文主要和大家共同探討下Web.config的system.webServer節點配置,如有錯誤之處,勞煩指點下
環境:IIS 7.0及以上
一、總覽
system.WebServer 是 configuration 節的子級。有關更多信息,請參見 IIS 7.0: system.webServer Section Group (IIS Settings Schema)(IIS 7.0:system.webServer 節組(IIS 設置架構))。
下面是可以在 system.WebServer 配置組中進行的 Web 服務器設置的示例:
-
當請求未包含特定資源時,Web 服務器返回給客戶端的默認文檔(defaultDocument 元素)。
-
響應的壓縮設置(httpCompression 元素)。
-
自定義標頭(httpProtocol 節的 customHeaders 元素)。
-
模塊(modules 元素)。
-
處理程序(handlers 元素)。
system.webServer 節中的某些設置只適用於 IIS 7.0 集成模式,而不適用於經典模式。
具體而言,如果應用程序正在經典模式下運行,則會忽略 Web.config 文件的 system.WebServer節中指定的所有托管代碼模塊和處理程序。
與 IIS 的早期版本相同,托管代碼模塊和處理程序必須在 system.web 節的 httpModules 和 httpHandlers 元素中定義。
本主題闡釋需要修改 system.webServer 節的三個常見配置任務:
如果 Web.config 文件尚未包含 system.webServer 節,請在 configuration 元素中創建該節,如下面的示例所示:
<configuration> <system.webServer> </system.webServer> </configuration>
二、配置默認文件
當請求 URL 未包含 Web 應用程序的特定文件時,IIS 7.0 將提供一個默認文件。
在 system.webServer 元素內,創建一個 defaultDocument 元素;
在 defaultDocument 元素內,創建一個 files 元素;
在 files 元素內創建一個 add 元素,並在 value 屬性內指定默認文件的路徑和名稱;
下面的示例演示了一個 system.webServer 節,該節配置為提供 Products.aspx 文件作為默認文件。
<configuration>
<system.webServer>
<defaultDocument> <files> <add value="Products.aspx" /> </files> </defaultDocument>
</system.webServer>
</configuration>
三、注冊托管代碼模塊
每次請求時都會調用托管代碼模塊,通過該模塊可對請求或響應進行自定義。
在 system.webServer 元素內,創建一個 modules 元素;在 modules 元素內創建一個 add 元素,並在 name 和 type 屬性中指定自定義模塊。實際的名稱和類型取決於要添加的模塊;
下面的示例演示如何添加名為CustomModule的自定義模塊,該模塊將實現為類型Samples.CustomModule。
<configuration>
<system.webServer>
<modules> <add name="CustomModule" type="Samples.CustomModule" /> </modules>
</system.webServer>
</configuration>
向模塊注冊中添加 precondition 屬性,並將其值設置為managedHandler。
此前置條件會導致僅在請求 ASP.NET 應用程序資源(例如 .aspx 文件或托管處理程序)時才調用該模塊。該資源中不包括靜態文件(例如 .htm 文件)。
其 configuration 節將類似於以下示例。
<configuration>
<system.webServer>
<modules>
<add name="CustomModule" type="Samples.CustomModule" precondition="managedHandler" />
</modules>
<defaultDocument>
<files>
<add value="Products.aspx" />
</files>
</defaultDocument>
</system.webServer>
</configuration>
關於MVC
對於像MVC這種比較特殊的URL,例如www.store.com/books/GetById/2
因為沒有文件后綴名,IIS通常會無法解析,返回403或者404錯誤。
ASP.NET v4.0增加了新的特性,當運行在IIS7以上版本,並且需要IIS的一個快速修復程序KB980368,配置web.config后,將會正常處理上面這種 extensionless URL。
<validation validateIntegratedModeConfiguration="false"/> 這個主要作用是設置不檢測 <system.web>中的配置 。
<modules runAllManagedModulesForAllRequests="true" /> 這里當設置為true的時候,所有的請求,包含靜態文件的請求和沒有文件擴展名的請求,都會經過自定義的HttpModule,其實就是為了偽靜態。
IIS經典模式下的配置
服務器會繼續通過 Aspnet_isapi.dll 路由托管代碼請求,其處理請求的方式就像應用程序在 IIS 6.0 中運行一樣,aspnet_isapi.dll(IIS的native handler擴展),通過映射到System.Web.DefaultHttpHandler進行處理。
<system.webServer>
<validation validateIntegratedModeConfiguration="false" />
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness32" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" modules="IsapiModule" scriptProcessor="%windir%\Microsoft.NET\Framework64\v4.0.30319\aspnet_isapi.dll" preCondition="classicMode,runtimeVersionv4.0,bitness64" responseBufferLimit="0" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="GET,HEAD,POST,DEBUG,PUT,DELETE,PATCH,OPTIONS" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer>
IIS集成模式下的配置
服務器將使用 IIS 和 ASP.NET 的集成請求處理管道來處理請求,映射到System.Web.Handlers.TransferRequestHandle來處理
<modules runAllManagedModulesForAllRequests="true" />
<handlers>
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="*." verb="*" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
經典模式(classic mode)和集成模式(Integrated mode)比較
在經典模式下,IIS會用ISAPI擴展(ISAPI extension aspnet_isapi.dll)和 ISAPI過濾器(ISAPI filter aspnet_filter.dll)來調用ASP.NET運行庫來處理請求。
使用經典模式的話,服務器會用兩種管道來處理請求一個負責源代碼,另外一個負責托管代碼。在這種模式下,應用程序不能充分使用IIS7.X提供的服務。
httpHandler 和 httpModule 在集成和經典模式下的區別
經典模式,在web.config的 <system.web> 的子節點<httpModules> 加入<add name="MyHttpModule" type="WebApplication.MyHttpModule, WebApplication"/>
集成模式,在web.config的 <system.webServer> <modules> 加入<add name="MyHttpModule" type="WebApplication.MyHttpModule, WebApplication"/>
四、配置自定義響應標頭
利用自定義響應標頭,可向瀏覽器發送應用程序特定的信息。例如,可以添加 Content-Language 標頭來描述網頁正文中使用的語言。若要執行此操作,請提供一個或多個語言和國家/地區值,例如 en-US(美國英語)或 en-GB(英國英語)。
在 system.webServer 元素內,創建一個 httpProtocol 元素。
在 httpProtocol 元素內,創建一個 customHeaders 元素。
在 customHeaders 元素內創建一個 add 標記,並在 name 和 value 屬性中指定自定義標頭。實際的名稱和類型將取決於該標頭在應用程序中的功能。
下面的示例演示如何添加名為CustomHeader且值為CustomHeader的自定義標頭。
<configuration>
<system.webServer>
<httpProtocol> <customHeaders> <add name="CustomHeader" value="CustomHeader" /> <customHeaders> </httpProtocol>
</system.webServer>
</configuration>
五、配置靜態內容
<staticContent>元素配置與Internet Information Services(IIS)7中的靜態文件處理請求相關的多個設置。
<staticContent>元素包含以下三個屬性,指定IIS 7是否應將文檔頁腳應用於靜態文件:
enableDocFooter屬性指定是否啟用文檔頁腳。
defaultDocFooter屬性包含:
如果isDocFooterFileName屬性設置為false,則IIS 7將用於文檔頁腳的文本字符串
如果isDocFooterFileName屬性設置為true,則文件的完整限定路徑包含IIS 7將用於文檔頁腳的文本。
如上所述,isDocFooterFileName屬性指定defaultDocFooter屬性是否包含IIS 7將用於文檔頁腳的文本字符串或包含IIS 7將用於文檔頁腳的文本的文件的標准路徑。
注意:默認情況下,isDocFooterFileName屬性設置為false並全局鎖定。要為文檔頁腳使用文件,您需要在全局級別將isDocFooterFileName屬性設置為true,或者解鎖該屬性。
配置mineMap節點,增加mime類型
在app和音頻視頻橫行的時代,增加mime類型成了iis必不可少的配置
配置mineMap節點,使得網站部署一步到位,不用每次為新服務器添加
<staticContent>
<remove fileExtension=".woff" />
<remove fileExtension=".woff2" />
<mimeMap fileExtension=".woff" mimeType="application/font-woff" />
<mimeMap fileExtension=".woff2" mimeType="application/font-woff" />
</staticContent>
再貼一段常用代碼
<staticContent>
<remove fileExtension=".mp4" />
<remove fileExtension=".m4a" />
<remove fileExtension=".m4v" />
<remove fileExtension=".ogv" />
<remove fileExtension=".webm" />
<mimeMap fileExtension=".mp4" mimeType="video/mp4" />
<mimeMap fileExtension=".m4a" mimeType="video/mp4"/>
<mimeMap fileExtension=".m4v" mimeType="video/m4v" />
<mimeMap fileExtension=".ogv" mimeType="video/ogg"/>
<mimeMap fileExtension=".webm" mimeType="video/webm"/>
</staticContent>
注意:添加之前必須先remove,否則如果原來已存在,會引起異常
參考文章:
https://technet.microsoft.com/zh-cn/sysinternals/bb763179.aspx
https://www.iis.net/configreference/system.webserver/staticcontent
http://www.cnblogs.com/imust2008/p/5651752.html