18

I want to gzip the JavaScript, HTML, CSS in my war before it goes over the wire. standard web stuff. Beanstalk uses an AMI to scale up.

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.

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
James
  • 1,553
  • 2
  • 11
  • 13
  • can you be more specific as to what you're doing? You're creating a beanstalk app, but you specify an ami? What are you gzipping? – Edwin Jan 25 '13 at 22:50
  • i want to gzip the javascript, html, css in my war before it goes over the wire. standard web stuff. beanstalk uses an ami to scale up. – James Jan 28 '13 at 16:28

4 Answers4

20

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:

/etc/httpd

Per making changes once you find info like this:
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.

Setup Apache:

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

Then create enabledeflate.conf with something like this:

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)

James
  • 1,553
  • 2
  • 11
  • 13
  • sorry for the format, i don't know how to format very well on here yet. – James Jan 28 '13 at 16:28
  • Check out this help to learn more about formatting posts: http://stackoverflow.com/editing-help – Frederik Kammer Jan 29 '13 at 18:38
  • 1
    I love this approach, but YSlow docs recommend you don't deflate images, since they're already compressed. http://developer.yahoo.com/performance/rules.html#gzip – Jeremy Wadhams Jun 07 '13 at 02:29
  • 1
    I removed the reference to the images. – James Aug 05 '13 at 18:03
  • 5
    "Actions > Restart App Server(s)" only restarts tomcat. To restart apache, you have to `ssh` to each server and run `sudo apachectl restart` – Mike Valenty Jan 17 '14 at 23:33
  • `SetOutputFilter DEFLATE` is unnecessary if your intention is to enable compression by MIME type with `AddOutputFilterByType`. – jamix Mar 22 '15 at 18:06
10

Adding on to James answer

A cleaner way is to create a config file

.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

Dr Manhattan
  • 13,537
  • 6
  • 45
  • 41
  • I did as you described and my system went to degraded. (beanstalk, tomcat) – maximus Mar 31 '17 at 08:41
  • I fixed it, found different solution, so you put the entire configuration starting from into file. then create another one: container_commands: 01_setup_apache: command: "cp .ebextensions/enable_mod_deflate.conf /etc/httpd/conf.d/enable_mod_deflate.conf" – maximus Apr 01 '17 at 10:07
5

There is no better place than 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/

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

Sabbir
  • 1,374
  • 1
  • 15
  • 23
  • 3
    Beware that the link above suggests gzipping png, jpeg and gif, which are already compressed formats. You'll be wasting your server's CPU cycles with this. – jamix Mar 22 '15 at 17:43
  • 1
    In case the linked website disappears, the idea here is to include the appropriate .conf file in your application bundle, then copy it into place using a container command. It requires that you manually restart apache after the deploy, though. Maybe you could add a container command to do that too. – Evan Apr 20 '16 at 17:49
2

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

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.conf with 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.
Oli Girling
  • 605
  • 8
  • 14