1

I cannot create a proper deployment package for AWS Elastic Beanstalk to enable gzip compression on Windows IIS environments.

I enabled in web config as described here. This worked only for static files, dynamic files are served as is.

Anybody have a solution for this?

Edit: There is another issue with IIS. It doesn't compress files requested from proxies and also serves the original file on the first request. This causes CDN's to serve uncompressed file because their endpoints caches the original file.

Community
  • 1
  • 1
Kerem Demirer
  • 1,186
  • 2
  • 13
  • 24

2 Answers2

8

After struggling 10 hours finally I came up with a solid solution.

AWS supports config files to modify the environment. They run before deploying application. http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html

So I created a config file to enable gzip on IIS, placed it to ".ebextensions/gzip.config" in my project folder.

Configuration in YAML format:

container_commands: 
     00-server-config:
       command: c:\windows\system32\inetsrv\appcmd set config /section:system.webServer/serverRuntime /frequentHitThreshold:1 /frequentHitTimePeriod:00:00:05
       waitAfterCompletion: 0
     01-server-config:
       command: c:\windows\system32\inetsrv\appcmd set config /section:system.webServer/httpCompression /noCompressionForHttp10:False /noCompressionForProxies:False
       waitAfterCompletion: 0
     02-gzip-dynamic: 
       command: c:\windows\system32\inetsrv\appcmd set config /section:urlCompression /doDynamicCompression:True
       waitAfterCompletion: 0
     03_gzip_static: 
       command: c:\windows\system32\inetsrv\appcmd set config /section:urlCompression /doStaticCompression:True
       waitAfterCompletion: 0
     04_restart_iis: 
       command: iisreset
       waitAfterCompletion: 0

There are some changes needed in web.config to system.webServer section:

<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files" noCompressionForHttp10="false" noCompressionForProxies="false">
    <scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll"/>
    <dynamicTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="application/x-javascript" enabled="true"/>
        <add mimeType="application/json; charset=utf-8" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
    </dynamicTypes>
    <staticTypes>
        <add mimeType="text/*" enabled="true"/>
        <add mimeType="message/*" enabled="true"/>
        <add mimeType="application/javascript" enabled="true"/>
        <add mimeType="application/x-javascript" enabled="true"/>
        <add mimeType="application/atom+xml" enabled="true"/>
        <add mimeType="application/xaml+xml" enabled="true"/>
        <add mimeType="*/*" enabled="false"/>
    </staticTypes>
</httpCompression>
<httpProtocol>
    <customHeaders>
        <remove name="Vary" />
        <add name="Vary" value="Accept-Encoding" />
    </customHeaders>
</httpProtocol>

With this two changes Elastic Beanstalk instances are prepared to serve compressed static and dynamic files. Also works with CDN.

Kerem Demirer
  • 1,186
  • 2
  • 13
  • 24
  • 1
    Also, if using Visual Studio, make sure you change the file properties of gzip.config so it will "Copy to Output Directory". – adam0101 May 08 '19 at 17:12
  • 1
    I'd like to point out that for dotnet core, you can still add a web.config file to your project. It works in conjunction with the settings MS moved to the csproj – carlin.scott Jan 24 '22 at 17:44
  • I think you need to add compression at Startup.cs. This may help https://www.talkingdotnet.com/how-to-enable-gzip-compression-in-asp-net-core/ – Kerem Demirer Jan 25 '22 at 10:26
5
  1. If you don't have the compression roles setup see '00' below
  2. If your applicationHost.config disables changes in web.config:

      <section name="httpCompression" allowDefinition="AppHostOnly" overrideModeDefault="Deny" />
    

    I found it easiest to supplement the existing applicationHost.config dynamicTypes using '05''s below.


commands: 
  00-install-comp:
    command: powershell.exe -nologo -noprofile -command "& { Import-Module ServerManager; Add-WindowsFeature Web-Stat-Compression,Web-Dyn-Compression; }"
    waitAfterCompletion: 0
  01-server-config:
    command: c:\windows\system32\inetsrv\appcmd set config /section:system.webServer/serverRuntime /frequentHitThreshold:1 /frequentHitTimePeriod:00:00:05
    waitAfterCompletion: 0
  02-server-config:
    command: c:\windows\system32\inetsrv\appcmd set config /section:system.webServer/httpCompression /noCompressionForHttp10:False /noCompressionForProxies:False
    waitAfterCompletion: 0
  03-gzip-dynamic: 
    command: c:\windows\system32\inetsrv\appcmd set config /section:urlCompression /doDynamicCompression:True
    waitAfterCompletion: 0
  04_gzip_static: 
    command: c:\windows\system32\inetsrv\appcmd set config /section:urlCompression /doStaticCompression:True
    waitAfterCompletion: 0
  05_gzip_dyn_type_1:
    command: c:\windows\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json',enabled='True']" /commit:apphost
    waitAfterCompletion: 0
    ignoreErrors: true
  05_gzip_dyn_type_2:
    command: c:\windows\system32\inetsrv\appcmd set config -section:system.webServer/httpCompression /+"dynamicTypes.[mimeType='application/json; charset=utf-8',enabled='True']" /commit:apphost
    waitAfterCompletion: 0
    ignoreErrors: true
  06_restart_iis: 
    command: iisreset
    waitAfterCompletion: 0
dave fernholz
  • 409
  • 4
  • 4