How to modify NGINX configuration on Deploying app Elastic beanstalk

Question:

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

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:

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

No luck with that

Answer:

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.

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

  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.

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:

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.

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

01_update_ng_conf.sh

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.

Leave a Reply