Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setup IIS10 to serve pre-compressed files

my angular2 projects build pre-compressed gzip files for my web-app but my IIS only serves the normal ".js" files instead of the compressed ".gzip" files. My browser is willing to accept gzip.

What is the correct setting for IIS to allow gzip responses?

I already searched google/SO/SU but only found solutions for not "pre-compressed" content.

like image 723
Alex Avatar asked Mar 07 '23 20:03

Alex


1 Answers

A more neat and elegant solution:

NOTICE: The file extension .gzip seems strange, in general, we name a gziped file as .gz, so in this example, we use .gz instead .gzip, if you insist on .gzip, just replace all the extensions in the following config file.


Code first, this is what all we need for web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <staticContent>
      <remove fileExtension=".js.gz" />
      <remove fileExtension=".css.gz" />
      <remove fileExtension=".png.gz" />
      <remove fileExtension=".jpg.gz" />
      <remove fileExtension=".gif.gz" />
      <remove fileExtension=".svg.gz" />
      <remove fileExtension=".html.gz" />
      <remove fileExtension=".json.gz" />
      <mimeMap fileExtension=".js.gz" mimeType="application/javascript" />
      <mimeMap fileExtension=".css.gz" mimeType="text/css" />
      <mimeMap fileExtension=".png.gz" mimeType="image/png" />
      <mimeMap fileExtension=".jpg.gz" mimeType="image/jpeg" />
      <mimeMap fileExtension=".gif.gz" mimeType="image/gif" />
      <mimeMap fileExtension=".svg.gz" mimeType="image/svg+xml" />
      <mimeMap fileExtension=".html.gz" mimeType="text/html" />
      <mimeMap fileExtension=".json.gz" mimeType="application/json" />
    </staticContent>
  
    <rewrite>
      <outboundRules rewriteBeforeCache="true">
        <rule name="Custom gzip file header">
          <match serverVariable="RESPONSE_CONTENT_ENCODING" pattern=".*" />
          <conditions>
            <add input="{REQUEST_URI}" pattern="\.gz$" />
          </conditions>
          <action type="Rewrite" value="gzip"/>
        </rule>
      </outboundRules>
      
      <rules>
        <rule name="Rewrite gzip file">
          <match url="(.*)"/>
          <conditions>
            <add input="{HTTP_ACCEPT_ENCODING}" pattern="gzip" />
            <add input="{REQUEST_FILENAME}.gz" matchType="IsFile" />
          </conditions>
          <action type="Rewrite" url="{R:1}.gz" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

And, here are how it works:

In order to achieve a successful gziped data transmission, we need:

  • Client side accept gziped data, Accept-Encoding
  • Response with a header with Content-Encoding
  • Proper MIME type, as the original file is, BUT NOT application/gzip
  • Gziped file

The four conditions must be satisfied at the same time.

If you send an uncompressed file with Content-Encoding: gzip, the browser will return an error;

If you send a compressed file without a Content-Encoding header, or a mismatched MIME type, the page may return some Zenith Star's text.

So what we are doing is:

  • Redefine each type of gziped file's MIME
  • If the client side accept a gziped file, then redirect the response file to a gziped version on the server side directly (not 302/303/307 response)
  • Rewrite the header of the response header of Content-Encoding, only if the client side send the header Accept-Encoding

This solution works on my IIS7, not sure if it will also work on IIS10.

If you met any problem, let me know :D

like image 98
Losses Don Avatar answered Mar 20 '23 01:03

Losses Don