Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nginx config file overwritten during Elastic Beanstalk deployment?

Tags:

I need to add p3p headers to the static resource location on a standard Nodejs & Nginx Elastic Beanstalk.

I've created an ebextension script as explained on this question. The script uses set to add a add_header directive under the alias line, which is under the static location directive. It runs on the /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf file.

The script not only modifies the file, it also copies it to a "safe" location, i.e. /home/ec2-user. According to /var/log/cfn-init.log, the script runs correctly. As evidence, the copy of the modified file shows the additional header at the right place. But the /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf file does not have this modification.

I can only deduce that although my script runs fine, something else later in the deployment process overwrites it. Which is strange, because according to documentation container commands are run after the application and web server have been set up, so I don't see what does it.

So ho/what is overwriting this file and how can I prevent that?

like image 966
Antoine Avatar asked Jul 21 '14 08:07

Antoine


People also ask

Where is nginx config Elastic Beanstalk?

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.

Does Elastic Beanstalk use nginx?

Elastic Beanstalk uses nginx as the reverse proxy to map your application to your Elastic Load Balancing load balancer on port 80. Elastic Beanstalk provides a default nginx configuration that you can either extend or override completely with your own configuration.

Where does Elastic Beanstalk store configuration files?

ebextensions, see the Elastic Beanstalk Configuration Files Repository . Location – Place all of your configuration files in a single folder, named . ebextensions , in the root of your source bundle.

When should you not use Elastic Beanstalk?

Elastic Beanstalk isn't great if you need a lot of environment variables. The simple reason is that Elastic Beanstalk has a hard limit of 4KB to store all key-value pairs. The environment had accumulated 74 environment variables — a few of them had exceedingly verbose names.


2 Answers

After spending almost entire day and trying out all the possible solutions, as of July 17, 2017, the above solution does not work. For me, I wanted to replace /etc/nginx/conf.d/elasticbeanstalk/00_application.conf I created the below shown folder structure in my .ebextension folder and the file was overwritten with my content. This solution also worked for nginx.conf which is located in /etc/nginx folder enter image description here

like image 142
Monil Gandhi Avatar answered Oct 21 '22 14:10

Monil Gandhi


It seems that Elastic Beanstalk has changed and the commonly recommended approach/hack of overwriting #etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf doesn't work any more. Nor does creating any file in /tmp/deployment/config.

The solution I found was to overwrite /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf directly, using a container_commands directive, since these commands are executed after the Elastic Beanstalk install creates it's version of the nginx config.

From http://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-container-commands:

They [container_commands] run after the application and web server have been set up and the application version file has been extracted, but before the application version is deployed.

I did this in three steps within .ebextensions:

  1. Create my version of the nginx config file.

  2. Create a script to overwrite the standard config file with my own.

  3. Run the script.

The first two steps happen earlier in the install process, while the last uses container_commands so as described previous happens late in the install.

Here's the files I used:

File .ebextensions/install_nginx_config_01.config:
(Note that the indenting is important)

# #   STEP 1 - Create the nginx config file # files:    "/tmp/my.nginx.conf" :     mode: "000755"     owner: root     group: root     content: |       # This file was overwritten during deployment       # by .ebextensions/install_nginx_config_03.config        upstream nodejs {           server 127.0.0.1:3000;           keepalive 256;       }        server {           listen 8080;            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;       } 

File .ebextensions/install_nginx_config_02.config:

# #   STEP 2 - Create a script that will overwrite the Nginx config # files:    "/tmp/install-nginx-config.sh" :     mode: "000755"     owner: root     group: root     content: |       #!/bin/sh       cp /tmp/my.nginx.conf /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf 

File .ebextensions/install_nginx_config_03.config:

# #   STEP 3 - Run the script to overwrite the nginx config template. # container_commands:    01_runmyshellscript:     command: "/tmp/install-nginx-config.sh" 
like image 37
Philip Callender Avatar answered Oct 21 '22 14:10

Philip Callender