4

I am trying to modify the Nginx config for my Elastic Beanstalk deployment. The default config is:-

upstream nodejs {
    server 127.0.0.1:8081;
    keepalive 256;
}

server {
    listen 8080;


    if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
        set $year $1;
        set $month $2;
        set $day $3;
        set $hour $4;
    }
    access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
    access_log  /var/log/nginx/access.log  main;


    location / {
        proxy_pass  http://nodejs;
        proxy_set_header   Connection "";
        proxy_http_version 1.1;
        proxy_set_header        Host            $host;
        proxy_set_header        X-Real-IP       $remote_addr;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
    }

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;


}

I attempt add the additional commands to the location node:-

chunked_transfer_encoding off;
proxy_buffering off;
proxy_cache off;

I initially tried adding the following to my .ebextensions:-

    files:
    "/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf" :
            mode: "000644"
            owner: root
            group: root
            content: |
            upstream nodejs {
                            server 127.0.0.1:8081;
                            keepalive 256;
                    }

                    server {
                            listen 8080;
                            if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
                                    set $year $1;
                                    set $month $2;
                                    set $day $3;
                                    set $hour $4;
                            }
                            access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
                            access_log  /var/log/nginx/access.log  main;


                            location / {
                                    proxy_pass  http://nodejs;
                                    proxy_set_header   Connection "";
                                    proxy_http_version 1.1;
                                    chunked_transfer_encoding off;
                                    proxy_buffering off;
                                    proxy_cache off;
                                    proxy_set_header        Host            $host;
                                    proxy_set_header        X-Real-IP       $remote_addr;
                                    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                            }

                    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;


                    }

However, on SSH-ing into the EC2 instance I can see that the file has not changed from the original. I then did some more research where it was suggested that /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf gets deployed after the .ebextensions files are run.

  1. http://finik.net/2014/10/29/Beanstalk/
  2. Nginx config file overwritten during Elastic Beanstalk deployment?

So I tried the following instead:-

    files:
    "/tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf" :
            mode: "000755"
            owner: root
            group: root
            content: |
            upstream nodejs {
                            server 127.0.0.1:8081;
                            keepalive 256;
                    }

                    server {
                            listen 8080;
                            if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
                                    set $year $1;
                                    set $month $2;
                                    set $day $3;
                                    set $hour $4;
                            }
                            access_log /var/log/nginx/healthd/application.log.$year-$month-$day-$hour healthd;
                            access_log  /var/log/nginx/access.log  main;


                            location / {
                                    proxy_pass  http://nodejs;
                                    proxy_set_header   Connection "";
                                    proxy_http_version 1.1;
                                    chunked_transfer_encoding off;
                                    proxy_buffering off;
                                    proxy_cache off;
                                    proxy_set_header        Host            $host;
                                    proxy_set_header        X-Real-IP       $remote_addr;
                                    proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
                            }

                    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;


                    }

Again, on SSH-ing into the instance I can see that neither /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf or /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf get modified either.

So now I am very stuck. What are my options?

Community
  • 1
  • 1
baynezy
  • 6,493
  • 10
  • 48
  • 73
  • The `.ebextensions` format is either YAML or JSON. And, your config format is YAML. YAML needs correct indentation. In your config above, it looks like the indentation is wrong (`files:` and `"/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf" :` should not in same indentation). – Edward Samuel Sep 10 '15 at 11:46

1 Answers1

2

What works is writing a sed script which updates the 00_elastic_beanstalk_proxy.conf file (by doing search and replace) and run said script at the container_command hook.

You can have a .ebextensions file like that:

files:
  /tmp/deployment/custom_mod_nginx.sh:
    mode: "000755"
    content: |
        sed -i 's/proxy_http_version 1.1;/     proxy_http_version 1.1;\n     chunked_transfer_encoding off;\n     proxy_buffering off;\n   proxy_cache off;\n/' /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf
container_commands:
  nginx_real_ip:
    command: "/tmp/deployment/custom_mod_nginx.sh"

What this will do is create a script named custom_mod_nginx.sh (yu can use the name you want). The script is executed after beanstalk deployed the configuration.

Vincent de Lagabbe
  • 4,964
  • 3
  • 31
  • 38
  • This is the only solution that currently works to modify the `/tmp/deployment/config/#etc#nginx#nginx.conf` file. – Manuel Apr 20 '20 at 22:45