Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to modify NGINX configuration on Deploying app Elastic beanstalk

I need to add some locations to nginx.conf so the environment URL points to app.php. I have modified the file using vi. Restarting NGINX it works. But I need this configuration to be load automatically when I use eb deploy.

I have read and tried: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html

Elasticbeanstalk configuring HTTPS on Single Instance of Python: null values are not allowed in templates

How to configure .ebextensions for nginx location directive?

Amazon Elastic Beanstalk ebextension

I have /.ebextensions/01_nginx.config

files:
"/etc/nginx/nginx.conf":
    mode: "0000644"
    owner: root
    group: root
    content: |
        My conf file

But that config is not working. I have tried changing "/etc/nginx/nginx.conf": by "/etc/nginx/my_nginx.conf": and the file appeared! So I tried to replace default file by my custom file with:

container_commands:
    deleteConf:
        command: "sudo rm /etc/nginx/nginx.conf"
    changeConf:
        command: "sudo cp /etc/nginx/my_nginx.conf  /etc/nginx/nginx.conf"

Placed below previous config inside 01_nginx.config. But commands are not working. nginx.conf is not being deleted or replaced by mine. What I'm doing wrong?

Edit: I have read that files in .ebextensions are evaluated alphabetically. I was wondering if maybe the copy command was being executed before the file exists. So I created a new file /.ebextensions/02_copy.config and moved there

container_commands:
    deleteConf:
        command: "sudo rm /etc/nginx/nginx.conf"
    changeConf:
        command: "sudo cp /etc/nginx/my_nginx.conf  /etc/nginx/nginx.conf"

No luck with that

like image 745
Adrián Rodriguez Avatar asked Jun 10 '20 08:06

Adrián Rodriguez


People also ask

Where is nginx config Elastic Beanstalk?

The default nginx configuration Below is the default Elastic Beanstalk nginx configuration which is located at /etc/nginx/nginx. conf for most of the distributions. The location of this file will vary depending on how you installed the software on your machine.

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.

What is the default nginx configuration for Elastic Beanstalk?

Elastic Beanstalk provides a default nginx configuration that you can either extend or override completely with your own configuration. By default, Elastic Beanstalk configures the nginx proxy to forward requests to your application on port 5000.

How do I deploy ebextensions to Elastic Beanstalk?

Now you can bundle the source code including the .ebextensions folder in the root directory and deploy the package to the Elastic Beanstalk environment. Elastic Beanstalk automatically creates the 01-timeout.conf file inside /etc/nginx/conf.d folder and is included in the main elastic beanstalk nginx configuration.

What is the default proxy for Elastic Beanstalk?

The default is nginx. Elastic Beanstalk provides a default proxy configuration that you can either extend or completely override with your own configuration. By default, Elastic Beanstalk configures the proxy to forward requests to your application on port 8080.

Where can I edit the nginx deployment file?

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.


2 Answers

After spending a whole day trying to modify nginx.conf on deployment, creating a custom nginx.conf directly in /etc/nginx/ via a .config file in .ebextensions didn't work for me as it would be systematically overwritten by the default one.

Same thing happened when I created the custom file in /tmp and wrote a container_command that would replace /etc/nginx/nginx.conf.

You actually need to create a script that will be executed after deployment, after elastic beanstalk has written all your files.

If you are using an Amazon Linux 1 environment:

Source: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html

To execute any script after deployment put the following code into your .config file at the root of .ebextensions

01_write_custom_ng_conf.config

  1. Create a directory that will store your script.
commands:
  create_post_dir:
    command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
    ignoreErrors: true
  1. Create your modified nginx.conf file and place it in /tmp.
files:
  "/tmp/custom_nginx.conf":
     mode: "000644"
     owner: root
     group: root
     content: |
       # your .conf file
  1. Create a script that will replace the original nginx.conf by your custom one in /opt/elasticbeanstalk/hooks/appdeploy/post created at step 1. This should be automatically executed after deployment.
   "/opt/elasticbeanstalk/hooks/appdeploy/post/update_ng_conf.sh":
     mode: "000644"
     owner: root
     group: root
     content:
       #!/usr/bin/bash
       sudo mv /tmp/custom_nginx.conf /etc/nginx/nginx.conf 

If you are using an Amazon Linux 2 environment:

Source: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html

Scripts are now executed from subfolders in .platform that you have to place at the root of your project, like so:

~/your-app/
|-- someFile.py
|-- Procfile
|-- readme.md
|-- .ebextensions/
|   |-- 01_write_custom_ng_conf.config        
`-- .platform/
    |-- hooks
        `-- postdeploy
            `-- 01_update_ng_conf.sh        # Executed post deployment

Create a .config file at the root of .ebextensions.

01_write_custom_ng_conf.config

Create your modified nginx.conf file and place it in /tmp.

files:
  "/tmp/custom_nginx.conf":
     mode: "000644"
     owner: root
     group: root
     content: |
       # your .conf file

Create a small script in .platform/hooks/postdeploy and change permissions to 755.

01_update_ng_conf.sh

#!/usr/bin/bash

# Replace the original nginx.conf by our custom one
sudo mv /tmp/custom_nginx.conf /etc/nginx/nginx.conf

# Restart nginx to apply modifications
sudo service nginx restart

This worked perfectly fine for me on a Amazon Linux 2 Python 3.7 environment. Not sure about yours but this should be the same.

Make sure that your .config files indentation is perfect as errors are not always detected by the parser and the code won't be executed.

like image 53
guimauve Avatar answered Nov 15 '22 02:11

guimauve


This answer applies to Linux 2. I spent a whole day trying to solve this, mainly because I was trying to use the old approach, not realizing things have changed for Linux 2. I then found guimauve's answer, but it did not work for me. After reading the official AWS documentation https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/platforms-linux-extend.html I found that it is not necessary to create a script in .platform/hooks/deploy. You just need to create a nginx directory in .platform and put your updated nginx.conf in there. First attempt with this approach, the nginx.conf was successfully overwritten, but my call still timed out. I then added:

proxy_connect_timeout 300;
proxy_send_timeout 300;
proxy_read_timeout 300;
fastcgi_send_timeout 300;
fastcgi_read_timeout 300;

in the server part of the nginx.conf and it worked! The call completed with no timeouts.

like image 27
Forked2454 Avatar answered Nov 15 '22 02:11

Forked2454