问题
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
回答1:
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
- Create a directory that will store your script.
commands:
create_post_dir:
command: "mkdir /opt/elasticbeanstalk/hooks/appdeploy/post"
ignoreErrors: true
- 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 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.
回答2:
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.
来源:https://stackoverflow.com/questions/62299458/how-to-modify-nginx-configuration-on-deploying-app-elastic-beanstalk