I want to configure my staging environment in Elastic Beanstalk to always disallow all spiders. The nginx directive would look like this:
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
I understand that I would want to create a file under the .ebextensions/ folder, such as 01_nginx.config, but I'm not sure how to structure the YAML inside it such that it would work. My goal is to add this location directive to existing configuration, not have to fully replace any existing configuration files which are in place.
I wanted to do the same thing. After a lot of digging, I found 2 ways to do it:
Option 1. Use an ebextension to replace the nginx configuration file with your custom configuration
I used this option because it is the simplest one.
Following the example given by Amazon in Using the AWS Elastic Beanstalk Node.js Platform - Configuring the Proxy Server - Example .ebextensions/proxy.config, we can see that they create an ebextension that creates a file named /etc/nginx/conf.d/proxy.conf. This file contains the same content as the original nginx configuration file. Then, they delete the original nginx configuration file using container_commands.
You need to replace the Amazon example with the contents of your current nginx configuration file. Note that the nginx configuration files to be deleted in the containter command must be updated too. The ones I used are:
Therefore, the final ebextension that worked for me is as follows:
/.ebextensions/nginx_custom.config
Once you deploy this change, you have to reload the nginx server. You can connect to your server using eb ssh your-environment-name and then run sudo service nginx reload
Option 2. Use an ebextension to modify the nginx configuration file generator, so that it includes your custom locations in the final nginx configuration file
The second option is based on this post: jabbermarky's answer in Amazon forums
He explains this method very well in his answer, so I encourage you to read it if you want to implement it. If you are going to implement this answer, you need to update the location of the nginx file configuration generator.
Note that I have not tested this option.
In summary, he adds a shell script to be executed before the nginx configuration file is generated. In this shell script, he modifies the nginx configuration file generator to include the server block locations he wants in the generated nginx configuration file. Finally, he adds a file containing the locations he wants in the server block of the final nginx configuration file.
This is achievable using .ebextension config files, however I'm having difficulty kicking nginx to restart after a change to its configuration files.
Now, I've done similar to add a file to kick the nginx tyres, however for some odd reason it's not executing:
It seems that the mentioned approaches dont work anymore. The new approach is to place nginx .conf files into a subfolder in .ebextensions:
Source
This does not require a restart of nginx either as Elastic Beanstalk will take care of that.
Mmmmm!
.ebextensions
!You're probably easiest off creating a shell script to change your configuration, and then running that. Don't really know nginx, but try something along the lines of:
I.e. first create a schell script that does what you need, then run it.
Oh, and be careful there are no tabs in your YAML! Only spaces are allowed... Check the log file
/var/log/cfn_init.log
for errors...Good luck!