IIS的url-rewrite配置預壓縮

參考:https://stackoverflow.com/questions/45199213/angulars-pre-gzipped-files-are-not-served-via-iis
參考:https://stackoverflow.com/questions/40884212/serve-bundle-js-gz-file-on-iis
參考:https://gist.github.com/suhrab/552af7b3383706081fbe51c102c290d7
參考:https://www.iis.net/downloads/microsoft/url-rewrite
(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

Url-Rewrite是什麼?

url-rewrite是網頁服務器的一項功能:

  1. 入站規則:可以用於重寫訪問的url路徑,可以把符合條件的請求的url路徑路由到另一個url路徑上去。
  2. 出站規則:可以根據一些條件,如消息的類型,修改返回消息的類型等內容。
    下面是官方的一段解釋:
    IIS URL Rewrite 2.1 enables Web administrators to create powerful rules to implement URLs that are easier for users to remember and easier for search engines to find. By using rule templates, rewrite maps, .NET providers, and other functionality integrated into IIS Manager, Web administrators can easily set up rules to define URL rewriting behavior based on HTTP headers, HTTP response or request headers, IIS server variables, and even complex programmatic rules. In addition, Web administrators can perform redirects, send custom responses, or stop HTTP requests based on the logic expressed in the rewrite rules.

Url-Rewrite的安裝

url-rewrite缺省是沒有安裝到IIS服務器上的,使用前,我們需要把它下載安裝到電腦上。
可以在這個官方的下載路徑上下載一個版本進行安裝:
https://www.iis.net/downloads/microsoft/url-rewrite

Url-Rewrite配置預壓縮

不同於nginx,IIS是沒有支持靜態預壓縮的。
這裏的靜態預壓縮指:把web服務器上的html/js等資源,使用gzip預先生成一份壓縮數據,當請求過來的時候,優先返回這些gzip壓縮的文件,這樣可以顯著的減少傳輸的數據量,提高傳輸效率。

實現策略

爲了讓IIS能夠返回gzip壓縮的文件,我們可以考慮使用url-rewrite功能。
1. 在請求入站時把請求的文件,添加gz壓縮包後綴。目的是:讓fetch文件時,直接把.gz的壓縮文件對外返回。
2. 在出站時,需要把content-encoding置位gzip,另外也需要把content-type置位 application/octet-stream。置位這些的目的是:如果不修改標識,直接返回gzip文件,瀏覽器會認爲客戶請求的就是gzip文件,是要下載gzip文件;而修改標識後,瀏覽器會識別出這是請求文件的gzip壓縮文件,使用前需要解壓,瀏覽器會對它進行解壓並傳給網頁程序進行使用。

配置樣例

配置樣例參考stackoverflow上的答覆:
https://stackoverflow.com/questions/45199213/angulars-pre-gzipped-files-are-not-served-via-iis

在網站跟目錄的的web.config文件中修改:
入站配置:識別js後綴的添加.gz後綴

		 <rules>
                <rule name="https" enabled="true" stopProcessing="false">
                    <match url="(.*).js" />
                    <conditions></conditions>
                    <action type="Rewrite" url="{R:1}.js.gz" appendQueryString="true" logRewrittenUrl="true" />
                </rule>
            </rules>

出站配置:.gz後綴的content-encoding修改爲gzip

<outboundRules>
                <rule name="Rewrite content-encoding header" preCondition="IsGZ" stopProcessing="false">
                    <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
                    <action type="Rewrite" value="gzip" />
                </rule>
                <preConditions>
                    <preCondition name="IsGZ">
                        <add input="{URL}" pattern="\.gz$" />
                    </preCondition>
        </preConditions>
        </outboundRules>
    </rewrite>

經過上面的配置,就可以實現js文件的預壓縮支持。

缺點分析

使用該種策略,一定程度可以在IIS上實現支持預壓縮文件,不過畢竟不像nginx的支持,該種變通實現是有一定的副作用的,如果使用的話注意規避該副作用。

  1. 修改後不再找原路徑:入站規則直接修改了請求的url,意爲着,如果修改後的url路徑文件不存在,就會返回404。如果請求不到.gz壓縮文件,是不會找原不帶.gz的請求文件的。
  2. gz後置被標識爲encoding-type爲gzip,瀏覽器收到後會進行解壓,這對原來就請求下載gz文件的情況,進行了干擾。請求下載的gz文件,瀏覽器先解壓了,理論上保存就是解壓後的文件了。

(Owed by: 春夜喜雨 http://blog.csdn.net/chunyexiyu)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章