Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you enable gzip of HTML/JavaScript/CSS on Amazon Beanstalk and Tomcat

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.

like image 648
James Avatar asked Jan 25 '13 22:01

James


3 Answers

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)

like image 69
James Avatar answered Oct 12 '22 22:10

James


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

like image 27
Dr Manhattan Avatar answered Oct 12 '22 22:10

Dr Manhattan


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'

like image 21
Sabbir Avatar answered Oct 12 '22 20:10

Sabbir