How to change nginx config in amazon elastic beanstalk running a docker instance How to change nginx config in amazon elastic beanstalk running a docker instance codeigniter codeigniter

How to change nginx config in amazon elastic beanstalk running a docker instance


Amazon actually recommends editing the staging version of the nginx deployment file. There are several located at /tmp/deployment/config/, one for editing the general 'http' context, and then a few for configuring different aspects of the server.

I wanted to attach caching functionality to the default proxy server, so I wrote an .ebextensions config file to replace #etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf, which is then copied over to /etc/nginx/conf.d during deployment. You can inline the file if its simple enough, but I put mine in S3 so that different applications and pull it down and use it. Here's the config file:

commands:   01-get-nginx-conf-file:    command: aws s3 cp s3://<bucket-name>/custom-nginx.conf /home/ec2-usercontainer_commands:  01-replace-default-nginx-config:    command: mv -f /home/ec2-user/custom-nginx.conf /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf


I also needed to modify the nginx configuration.

  1. Create a script that modifies the nginx configuration (probably you want /etc/nginx/sites-enabled/elasticbeanstalk-nginx-docker.conf) and restarts the nginx service (service nginx restart).
  2. You need to execute that script after this nginx config file is written which is after normal ebextensions are executed. This is undocumented, but Evan shared how to do this here: essentially you use an ebextension to copy the script into a directory with hooks that gets executed at the proper time.

An example ebextension config is .ebextensions/01modify_nginx.config:

container_commands:  copy:    command: "cp .ebextensions/01rewrite_nginx_config.py /opt/elasticbeanstalk/hooks/appdeploy/enact/"  make_exe:    command: "chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/01rewrite_nginx_config.py"

This is working nicely now for my project (here is the source where you can see it in action).


Another way to extend Elastic Beanstalk nginx config is to create a file in the .ebextensions directory, named for example nginx.config with the following content :

    files:      "/etc/nginx/conf.d/000_my_config.conf":      content: |        upstream nodejsserver {          server 127.0.0.1:8081;          keepalive 256;        }        server {          listen 8080;          location / {            proxy_pass  http://nodejsserver;            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;          }          location /myconfig {            proxy_pass http://my_proxy_pass_host;          }        }

/etc/nginx/conf.d/000_my_config.conf is the filename which will be created on the Elastic Beanstalk EC2 instances. By default this configuration is in the file /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf. So if you prefix with 000, it guarantees you that your configuration will be taken into account first.

The content has been copied from the default nginx configuration (/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf again), then customized with my own configuration.