9

I have an AWS EB environment of Python 3.7 running Amazon Linux 2/3.1.2 using Nginx as a proxy server. I'm trying to add a gzip compression for my application. I tried out several tutorials online but they all don't appear to work for me. I'm also new to AWS so might not be familiar with some of its services.

Currently, I had a directory tree like this:

-- .ebextensions
-- .platform
   -- nginx
     -- conf.d
        -- gzip.conf
-- (other files)

I tried adding a config file in .ebextensions to create a .conf to enable gzip compression, but it didn't seem to work. I also tried switching the proxy to Apache, but no luck. This tutorial says that for the latest version of Amazon Linux 2, the nginx config files should be placed in the .platform folder, so I did as noted. However, my gzip.conf file still didn't seem to work - files are still rendered in their original formats.

Currently my gzip.conf:

gzip on;
gzip_vary on;
gzip_min_length 10240;
gzip_proxied expired no-cache no-store private auth;
gzip_types text/plain text/html text/css text/xml text/javascript application/x-javascript application/xml;
gzip_disable "MSIE [1-6]\.";

EDIT: I SSH'd into my eb instance and found this file is at /etc/nginx/conf.d/gzip.conf and the content is the same as what I uploaded. Would this path be correct to enable gzip?

Any help will be appreciated!

Parzival
  • 2,051
  • 4
  • 14
  • 32
  • 1
    If you ssh into the EB instance, can you manually setup nginx to make it work? This way you can at least confirm the options you want to use. – Marcin Jan 19 '21 at 09:59
  • I ran into similar issues and wrote a gist summarizing how to change nginx configurations for Amazon Linux 2. You can find it here: https://gist.github.com/henhan/2943013c9064606425b0ee5bb1ca8c99 – Henrik Hansson Aug 01 '21 at 11:26

3 Answers3

10

Big idea: To gain full control of your nginx configurations, you need to override the default settings in the .platform/nginx/nginx.conf file in your project directory.

The problem: When I ssh'd into my EB instance, I found that in the file /etc/nginx/nginx.conf still includes the default setting gzip off. For some reason my extension of this file did not overwrite this setting. I suppose it's because in Amazon Linux 2, the proxy configurations should be under .platform/nginx directory.

Solution: I used ssh to obtain a copy of nginx.conf, added it to my project directory .platform/nginx, commented out the original settings for gzip, and added the new gzip settings. Below is a snippet of my updated nginx.conf file:

#Original Settings
#gzip                  off;
#gzip_comp_level       4;
#gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;

#New Settings
gzip on;
gzip_static on;
gzip_comp_level 9;
gzip_proxied any;
gzip_types application/javascript application/rss+xml application/vnd.ms-fontobject application/x-font application/x-font-opentype application/x-font-otf application/x-font-truetype application/x-font-ttf application/x-javascript application/xhtml+xml application/xml application/json font/opentype font/otf font/ttf image/svg+xml image/x-icon text/css text/html text/javascript text/plain text/xml;

After deploying, it finally worked! Hope this will help others with the same question.

Thanks to @Marcin's suggestion to ssh into my instance, which helped me figure out what's going on.

Parzival
  • 2,051
  • 4
  • 14
  • 32
  • 5
    You should never touch anything on ElasticBeanstalk instances. Those instances are continuously going up and down due to the scaling and the config file changes will be lost. You should do this with .ebextentions – Peter May 04 '21 at 16:44
  • @Peter, what Parzival did is correct. Starting from Amazon Linux 2, `.platform/` directory is meant for configuration too. – pmoleri Aug 16 '22 at 11:59
  • @pmoleri wrong, he is talking about SSH-ing to an instance and changing the files there, that's very not recommended! You can have a .platform dir in your project folder that EB will use during deploying – Peter Aug 17 '22 at 00:07
  • 3
    @Peter No, this is not what I recommended. I was talking about how ssh-ing into my instance helped me figure out the solution (and get a copy of the original nginx.conf file), not ssh-ing to change the files. Hope this clarifies it – Parzival Aug 17 '22 at 01:17
  • @Peter I find it pretty clear actually, even he mentions "project" and "after deploying", but perhaps it's because this concept of retrieving the conf file was already familiar to me. Perhaps there's room for improvement but it's certainly not wrong. – pmoleri Aug 18 '22 at 11:37
4

I fixed this issue with a small script that I left on .platform/hooks/predeploy.

Code snippet below.

#!/usr/bin/env bash
set -e

echo Forcing gzip on.
sed -ri 's/gzip([ \t]+)off[ \t]*;/gzip\1 on;/' /var/proxy/staging/nginx/nginx.conf
Adam Alves
  • 41
  • 1
1

An alternative solution to @Parzival 's, instead of overwriting the root nginx config, you can overwrite the app config file.

To get the application configuration file, ssh into the instance and get the configuration file from: /etc/nginx/conf.d/elasticbeanstalk/00_application.conf

Mine looked like this:

location / {
    proxy_pass          http://127.0.0.1:8080;
    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;
}

Saved it into my project at: .platform/nginx/conf.d/elasticbeanstalk/00_application.conf

And edited it adding what I need:

location / {
    # Base config from elasticbeanstalk:
    proxy_pass          http://127.0.0.1:8080;
    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;

    # Add forwarded protocol and port details
    proxy_set_header X-Forwarded-Proto $http_x_forwarded_proto;
    proxy_set_header X-Forwarded-Port $http_x_forwarded_port;

    # Override gzip directive in server block
    gzip on;
    gzip_comp_level 5;
    gzip_types text/* application/json application/javascript application/x-javascript application/xml application/xml+rss;
}

After that, create a new bundle and deploy it. Beanstalk will use your custom 00_application.conf instead of the default one.

Notes:

  • I added protocol and port forwarding, unrelated to this question but still worth sharing
  • I edited gzip_types setting text/*, the default config in the root file was:
    gzip_types text/plain text/css application/json application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript;
    
pmoleri
  • 4,238
  • 1
  • 15
  • 25