在Amazon Elastic Beanstalk Windows环境中启用gzip

时间:2015-05-04 00:57:42

标签: windows amazon-web-services iis gzip elastic-beanstalk

我无法为AWS Elastic Beanstalk创建适当的部署包,以便在Windows IIS环境中启用gzip压缩。

我按照here所述在网络配置中启用了。这仅适用于静态文件,动态文件按原样提供。

有人有解决方案吗?

修改 IIS还有另一个问题。它不会压缩从代理请求的文件,也会在第一次请求时提供原始文件。这会导致CDN提供未压缩的文件,因为它们的端点会缓存原始文件。

2 个答案:

答案 0 :(得分:7)

经过10个小时的努力,我终于想出了一个坚实的解决方案。

AWS支持配置文件来修改环境。它们在部署应用程序之前运行。 http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html

所以我创建了一个配置文件来在IIS上启用gzip,将它放到" .ebextensions / gzip.config"在我的项目文件夹中。

以YAML格式配置:

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

web.config中需要对system.webServer部分进行一些更改:

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

通过这两项更改,Elastic Beanstalk实例可以为压缩的静态和动态文件提供服务。也适用于CDN。

答案 1 :(得分:3)

  1. 如果您没有设置压缩角色,请参阅&00; 00&#39;以下
  2. 如果您的applicationHost.config禁用了web.config中的更改:

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

    我发现最简单的补充现有的applicationHost.config 使用&#39; 05&#39;以及

  3. 的dynamicTypes。

    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