我已经将Spring Boot REST应用程序作为WAR部署到了AWS Elastic Beanstalk Tomcat 8 + Java 8实例中。随后,我意识到我需要配置以下设置(适用于Tomcat server.xml):

compression="on"
compressionMinSize="2048"
compressableMimeType="text/html,text/css,..."


因为Spring Boot中的application.properties仅适用于嵌入式Tomcat容器。现在更改Elastic Beanstalk实例的平台类型为时已晚。是否可以使用eb config从Elastic Beanstalk CLI更新配置?我在看这个AWS page,看起来好像有可能。

更新于01/16/2017
感谢@ dave-maple的回答,我开始研究Elastic Beanstalk文档的相关部分。

首先,我发现Apache是​​默认的代理服务器。我本可以将其更改为nginx,但是我没有任何特别的理由选择那条路线。

其次,我发现在Spring Boot项目的顶层添加了一个.ebextensions文件夹。我不想使用特定于云提供商的配置文件来污染我的代码库,但是这似乎是悬念最少的成果。所以我去了。

我添加了以下层次结构:

MySpringBootProject
 |
 +- src
     |
     +- main
         |
         +- resources
             |
             +- ebextensions
                 |
                 +- httpd
                 |   |
                 |   +- conf.d
                 |       |
                 |       +- enable_mod_deflate.conf
                 |
                 +- myapp.config
                 |
                 +- tomcat-settings.config


tomcat-settings.config的内容

option_settings:
  aws:elasticbeanstalk:environment:proxy:
    GzipCompression: 'true'


myapp.config的内容

container_commands:
05-restart-apache:
    command: "sudo /etc/init.d/httpd restart"


enable_mod_deflate.conf的内容

# mod_deflate configuration
<IfModule mod_deflate.c>
  # Restrict compression to these MIME types
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE text/xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE application/xml+rss
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE image/png
  AddOutputFilterByType DEFLATE image/gif
  AddOutputFilterByType DEFLATE image/jpeg

  # Level of compression (Highest 9 - Lowest 1)
  DeflateCompressionLevel 9

  # Netscape 4.x has some problems.
  BrowserMatch ^Mozilla/4 gzip-only-text/html

  # Netscape 4.06-4.08 have some more problems
  BrowserMatch ^Mozilla/4\.0[678] no-gzip

  # MSIE masquerades as Netscape, but it is fine
  BrowserMatch \bMSI[E] !no-gzip !gzip-only-text/html

  <IfModule mod_headers.c>
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
  </IfModule>
</IfModule>


将以下行添加到pom.xml中(以在生成的WAR文件的顶级设置.ebextensions文件夹):

<plugin>
    <artifactId>maven-war-plugin</artifactId>
    <configuration>
        <webResources>
            <resource>
                <directory>src/main/resources/ebextensions</directory>
                <targetPath>.ebextensions</targetPath>
                <filtering>true</filtering>
            </resource>
        </webResources>
    </configuration>
</plugin>


我本可以宣誓这样做有效,因为我只看到几次Content-Encoding: gzip响应标头。知道会发生什么吗?

最佳答案

最好的压缩方式是在tomcat前面运行的nginx代理。 Nginx在此操作上效率更高。为此,您可以在部署档案的根目录中创建一个.ebextensions目录,并添加一个nginx-proxy.config文件,例如:

.ebextensions / nginx-proxy.config

option_settings:
  aws:elasticbeanstalk:environment:proxy:
    GzipCompression: 'true'
    ProxyServer: nginx


然后,您可以在Elastic Beanstalk上构建和部署新版本的应用程序。

如果在推出新版本时需要避免停机,则可以使用rolling deployment(在部署新版本之前从LB中删除实例),甚至可以使用blue/green deployment (a new environment + cname swap)

===编辑===

您可能需要自定义nginx将gzip的Content-Type值。

要gzip一切,请在.ebextensions中创建一个配置文件:

.ebextensions / gzip.config

files:
  /etc/nginx/conf.d/gzip.conf:
    content: |
      gzip_types *;


或者,为了更具选择性,请定义要压缩的类型:

.ebextensions / gzip.config

files:
  /etc/nginx/conf.d/gzip.conf:
    content: |
      gzip_types text/plain text/css application/json application/x-javascript text/xml;

07-24 09:49
查看更多