Skip to content

Instantly share code, notes, and snippets.

@mjul
Last active November 6, 2020 03:29
Show Gist options
  • Save mjul/e0f73d159c71e4fce13c645a2f83344c to your computer and use it in GitHub Desktop.
Save mjul/e0f73d159c71e4fce13c645a2f83344c to your computer and use it in GitHub Desktop.
Enable gzip compression in Elastic Beanstalk Docker nginx proxy (add to .ebextensions folder)
files:
"/etc/nginx/conf.d/gzip.conf":
mode: "644"
owner: "root"
group: "root"
content: |
# enable gzip compression
gzip on;
gzip_min_length 1100;
gzip_buffers 4 32k;
gzip_types text/plain text/html text/xml text/css application/javascript application/json;
gzip_vary on;
# end gzip configuration
container_commands:
02_restart_nginx:
command: service nginx restart

Add the attached script to the .ebextensions folder in the deployment package to enable gzip compression on the nginx proxy. This compresses text payloads such as HTML, Javascript and JSON for a better client experience. Works for Elastic Beanstalk with single-container Docker. Some EB stacks allow the choice between Apache httpd and nginx so it will not for all EB configurations.

@albacoretuna
Copy link

albacoretuna commented Feb 9, 2017

I still couldn't get this to work, I got this:

Command failed on instance. Return code: 127 Output: /bin/sh: service: command not found. container_command 02_restart_nginx in travis-03f3fd2dc94f868fdec1f1db00024asdf9c2311d93-1486642650/.ebextensions/02_nginx_gzip.config failed. For more detail, check /var/log/eb-activity.log using console or EB CLI.

And then in eb-activity log

Activity execution failed, because: /bin/sh: service: command not found

I'm inspecting it more tomorrow

@albacoretuna
Copy link

albacoretuna commented Feb 13, 2017

It seems like on line 11, the text/html is not needed as it is compressed by default, and nginx will give warnings for duplication and it stops deployment.

@albacoretuna
Copy link

@henrythe9th
Copy link

Looks like Elasticbeanstalk now has gzip enabled by default:

/etc/nginx/sites-available/elasticbeanstalk-nginx-docker-proxy.conf

map $http_upgrade $connection_upgrade {
        default        "upgrade";
        ""            "";
    }

    server {
        listen 80;

        gzip on;
            gzip_comp_level 4;
            gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;



        access_log    /var/log/nginx/access.log;

        location / {
            proxy_pass            http://docker;
            proxy_http_version    1.1;

            proxy_set_header    Connection            $connection_upgrade;
            proxy_set_header    Upgrade                $http_upgrade;
            proxy_set_header    Host                $host;
            proxy_set_header    X-Real-IP            $remote_addr;
            proxy_set_header    X-Forwarded-For        $proxy_add_x_forwarded_for;
        }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment