我已将 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 , 看起来这是可能的。
2017 年 1 月 16 日更新 感谢 @dave-maple 的回答,我开始查看 Elastic Beanstalk 文档的相关部分。
首先,我发现 Apache 是默认的代理服务器。我本可以将它更改为 nginx,但我没有任何特别的理由去走那条路。
其次,我发现将 .ebextensions
文件夹添加到我的 Spring Boot 项目的顶层。我不想用特定于云提供商的配置文件污染我的代码库,但这似乎是最容易实现的目标。所以我去了。
我添加了以下层次结构:
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
响应 header ……。知道会发生什么吗?
最佳答案
实现压缩的最好方法是在 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;
关于amazon-web-services - 更新正在运行的 ElasticBeanstalk Tomcat 实例的配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41645686/