我想在Elastic Beanstalk中配置我的暂存环境,以便始终禁止所有蜘蛛. Nginx指令看起来像这样:
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
我知道我想在.ebextensions /文件夹下创建一个文件,例如01_Nginx.config,但我不知道如何在其中构建YAML以便它可以工作.我的目标是将此位置指令添加到现有配置,而不必完全替换现有的任何现有配置文件.
选项1.使用ebextension将Nginx配置文件替换为自定义配置
我使用这个选项是因为它是最简单的选项.
按照Amazon在Using the AWS Elastic Beanstalk Node.js Platform – Configuring the Proxy Server – Example .ebextensions/proxy.config中给出的示例,我们可以看到它们创建了一个ebextension,它创建了一个名为/etc/Nginx/conf.d/proxy.conf的文件.此文件包含与原始Nginx配置文件相同的内容.然后,他们使用container_commands删除原始的Nginx配置文件.
您需要使用当前Nginx配置文件的内容替换Amazon示例.请注意,必须更新要在containter命令中删除的Nginx配置文件.我使用的是:
> Nginx配置文件1:/opt/elasticbeanstalk/support/conf/webapp_healthd.conf
> Nginx配置文件2:/etc/Nginx/conf.d/webapp_healthd.conf
因此,对我有用的最终ebextension如下:
/.ebextensions/Nginx_custom.config
# Remove the default Nginx configuration generated by elastic beanstalk and
# add a custom configuration to include the custom location in the server block.
# Note that the entire Nginx configuration was taken from the generated /etc/Nginx/conf.d/webapp_healthd.conf file
# and then,we just added the extra location we needed.
files:
/etc/Nginx/conf.d/proxy_custom.conf:
mode: "000644"
owner: root
group: root
content: |
upstream my_app {
server unix:///var/run/puma/my_app.sock;
}
log_format healthd '$msec"$uri"'
'$status"$request_time"$upstream_response_time"'
'$http_x_forwarded_for';
server {
listen 80;
server_name _ localhost; # need to listen to localhost for worker tier
if ($time_iso8601 ~ "^(\d{4})-(\d{2})-(\d{2})T(\d{2})") {
set $year $1;
set $month $2;
set $day $3;
set $hour $4;
}
access_log /var/log/Nginx/access.log main;
access_log /var/log/Nginx/healthd/application.log.$year-$month-$day-$hour healthd;
location / {
proxy_pass http://my_app; # match the name of upstream directive which is defined above
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location /assets {
alias /var/app/current/public/assets;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
location /public {
alias /var/app/current/public;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
}
container_commands:
# Remove the default Nginx configuration generated by elastic beanstalk
removeconfig:
command: "rm -f /opt/elasticbeanstalk/support/conf/webapp_healthd.conf /etc/Nginx/conf.d/webapp_healthd.conf"
部署此更改后,您必须重新加载Nginx服务器.您可以使用eb ssh your-environment-name连接到您的服务器,然后运行sudo service Nginx reload
选项2.使用ebextension修改Nginx配置文件生成器,以便它在最终的Nginx配置文件中包含您的自定义位置
第二个选项基于这篇文章:jabbermarky’s answer in Amazon forums
他在答案中很好地解释了这个方法,所以如果你想实现它,我鼓励你阅读它.如果要实现此答案,则需要更新Nginx文件配置生成器的位置.
请注意,我还没有测试过这个选项.
总之,他添加了一个在生成Nginx配置文件之前执行的shell脚本.在这个shell脚本中,他修改了Nginx配置文件生成器,以在生成的Nginx配置文件中包含他想要的服务器块位置.最后,他在最终的Nginx配置文件的服务器块中添加了一个包含他想要的位置的文件.