skip to Main Content

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!

3

Answers


  1. Chosen as BEST ANSWER

    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.


  2. 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;
      
    Login or Signup to reply.
  3. 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]*;/gzip1 on;/' /var/proxy/staging/nginx/nginx.conf
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search