你如何在amazon beanstalk和tomcat上启用html/javascript/css的gzip

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14531421/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 21:52:55  来源:igfitidea点击:

how do you enable gzip of html/javascript/css on amazon beanstalk and tomcat

javascriptamazon-web-servicesamazon-ec2gzipamazon-elastic-beanstalk

提问by James

I see the directions on how to create a new AMI, but I don't even see where tomcat is located. The current ami as of this writing is ami-1a249873 for tomcat 7 deployments

我看到了有关如何创建新 AMI 的说明,但我什至看不到 tomcat 的位置。在撰写本文时,当前 ami-1a249873 用于 tomcat 7 部署

回答by James

I'll answer this myself. Just so its clear to everyone, you CAN connect to your instances of EC2 even though they are being managed by beanstalk. This is helpful because you get to see where things are located. In this case, I didn't know Apache was being used as the webserver for tomcat and had to search for that, but you can find it here as today:

我会自己回答这个问题。每个人都很清楚,您可以连接到您的 EC2 实例,即使它们是由 beanstalk 管理的。这很有帮助,因为您可以看到事物所在的位置。在这种情况下,我不知道 Apache 被用作 tomcat 的网络服务器,因此不得不搜索它,但您可以在这里找到它,就像今天一样:

/etc/httpd

/etc/httpd

Per making changes once you find info like this:
http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html

一旦你找到这样的信息,就进行更改:http:
//docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers.html

If you create a folder called .elasticbeanstalk at the root of your project and then create a file called myapp.config.

如果您在项目的根目录下创建一个名为 .elasticbeanstalk 的文件夹,然后创建一个名为 myapp.config 的文件。

Setup Apache:

设置阿帕奇:

cp conf/httpd/conf.d/enabledeflate.conf /etc/httpd/conf.d/enabledeflate.conf

Then create enabledeflate.conf with something like this:

然后使用以下内容创建 enabledeflate.conf:

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

A couple of notes:
You may need to restart apache the first time you deploy this.
Make sure you put .elasticbeanstalk in the root of your war file (or git repo)

一些注意事项:
您可能需要在第一次部署时重新启动 apache。
确保将 .elasticbeanstalk 放在 war 文件(或 git repo)的根目录中

回答by Dr Manhattan

Adding on to James answer

添加到詹姆斯的答案

A cleaner way is to create a config file

更简洁的方法是创建一个配置文件

.ebextensions/wsgi_custom.config

.ebextensions/wsgi_custom.config

And place this in there

然后把它放在那里

files:
  "/etc/httpd/conf.d/wsgi_custom.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      WSGIPassAuthorization On

      LoadModule deflate_module modules/mod_deflate.so

      SetOutputFilter DEFLATE

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

I also added the WSGIPassAuthorization On in case you need to use this for django-rest-framework using jwt auth

我还添加了 WSGIPassAuthorization On 以防您需要使用 jwt auth 将此用于 django-rest-framework

回答by Sabbir

There is no better place than http://www.tonmoygoswami.com/2013/05/how-to-enable-gzip-on-amazon-elastic.html

没有比http://www.tonmoygoswami.com/2013/05/how-to-enable-gzip-on-amazon-elastic.html更好的地方

for your answer

为您解答

You can restart server from https://console.aws.amazon.com/elasticbeanstalk/

您可以从https://console.aws.amazon.com/elasticbeanstalk/重新启动服务器

click on application name and then from top right section click action dropdown button and 'restart server'

单击应用程序名称,然后从右上角单击操作下拉按钮并“重新启动服务器”

回答by Oli Girling

Seemed like there was a few ways to do this but no complete copy and paste solution. So here is mine, working without any issues.

似乎有几种方法可以做到这一点,但没有完整的复制和粘贴解决方案。所以这是我的,工作没有任何问题。

Created file .ebextensions/01-environment.config

创建的文件 .ebextensions/01-environment.config

Added the following:

添加了以下内容:

# Enable Server-side Compression
files:
  "/etc/httpd/conf.d/enable_mod_deflate.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
        <IfModule mod_deflate.c>
            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

            DeflateCompressionLevel 9

            BrowserMatch ^Mozilla/4 gzip-only-text/html
            BrowserMatch ^Mozilla/4\.0[678] no-gzip
            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>

container_commands:
  02_restart_apache:
    command: sudo apachectl restart

What does this do?

这有什么作用?

  1. Creates a file /etc/httpd/conf.d/enable_mod_deflate.confwith the correct permissions
  2. Adds compression contents inside the file
  3. Finally create a container_commands(link to difference between this and command here), to restart the apache server. This is required to show the effects and is also important as if you are auto scaling and another instance is spun up, this will also create this file then restart apache on the new instance. Without this, the instance would not restart and would require manual restart.
  1. 创建/etc/httpd/conf.d/enable_mod_deflate.conf具有正确权限的文件
  2. 在文件中添加压缩内容
  3. 最后创建一个container_commands(链接到这里和命令之间的区别),以重新启动 apache 服务器。这是显示效果所必需的,并且也很重要,就像您正在自动缩放并且启动另一个实例一样,这也将创建此文件,然后在新实例上重新启动 apache。如果没有这个,实例将不会重新启动并且需要手动重新启动。