rga*_*eth 6 nginx amazon-elastic-beanstalk
我想在Elastic Beanstalk中配置我的暂存环境,以便始终禁止所有蜘蛛.nginx指令看起来像这样:
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
Run Code Online (Sandbox Code Playgroud)
我知道我想在.ebextensions /文件夹下创建一个文件,例如01_nginx.config,但我不知道如何在其中构建YAML以便它可以工作.我的目标是将此位置指令添加到现有配置,而不必完全替换现有的任何现有配置文件.
Mar*_*ris 10
我想做同样的事情.经过大量挖掘后,我找到了两种方法:
我使用这个选项是因为它是最简单的选项.
按照亚马逊在使用AWS Elastic Beanstalk Node.js平台 - 配置代理服务器 - 示例.ebextensions/proxy.config中给出的示例,我们可以看到他们创建了一个ebextension,创建了一个名为/ etc/nginx/conf的文件. d/proxy.conf.此文件包含与原始nginx配置文件相同的内容.然后,他们使用container_commands删除原始的nginx配置文件.
您需要使用当前nginx配置文件的内容替换Amazon示例.请注意,必须更新要在containter命令中删除的nginx配置文件.我使用的是:
因此,对我有用的最终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"
Run Code Online (Sandbox Code Playgroud)
部署此更改后,您必须重新加载nginx服务器.您可以使用eb ssh your-environment-name连接到您的服务器,然后运行sudo service nginx reload
第二个选项基于这篇文章:jabbermarky在亚马逊论坛中的回答
他在答案中很好地解释了这个方法,所以如果你想实现它,我鼓励你阅读它.如果要实现此答案,则需要更新nginx文件配置生成器的位置.
请注意,我还没有测试过这个选项.
总之,他添加了一个在生成nginx配置文件之前执行的shell脚本.在这个shell脚本中,他修改了nginx配置文件生成器,以在生成的nginx配置文件中包含他想要的服务器块位置.最后,他在最终的nginx配置文件的服务器块中添加了一个包含他想要的位置的文件.
有一种方法.platform/nginx在 Amazon Linux 2 上使用更新的配置扩展(而不是旧的 AMI)。
默认nginx.conf包括整个nginx.conf文件的两个位置的配置部分。一个是直接在http块内,所以你不能location在这里放置额外的块,因为这在语法上是不合法的。server不过,第二个在块内,这就是我们所需要的。
第二个位置的部分文件包含在一个特殊的子目录.platform/nginx/conf.d/elasticbeanstalk. 将您的位置片段放在这里以添加位置块,如下所示:
# .platform/nginx/conf.d/elasticbeanstalk/packs.conf
location /packs {
alias /var/app/current/public/packs;
gzip_static on;
gzip on;
expires max;
add_header Cache-Control public;
}
Run Code Online (Sandbox Code Playgroud)
嗯嗯!.ebextensions!
您可能最简单的方法是创建一个 shell 脚本来更改配置,然后运行它。不太了解 nginx,但尝试一下以下内容:
files:
"/root/setup_nginx.sh" :
mode: "000750"
owner: root
group: root
content: |
#!/bin/sh
# Configure for NGINX
grep robots.txt <your_config_file> > /dev/null 2>&1
if [ $? -eq 1 ] ; then
echo < EOF >> <your_config_file>
location /robots.txt {
return 200 "User-agent: *\nDisallow: /";
}
EOF
# Restart any services you need restarting
fi
container_commands:
000-setup-nginx:
command: /root/setup_nginx.sh
Run Code Online (Sandbox Code Playgroud)
即,首先创建一个执行您需要的操作的 schel 脚本,然后运行它。
哦,请小心 YAML 中没有选项卡!只允许使用空格...检查日志文件中/var/log/cfn_init.log是否有错误...
祝你好运!
| 归档时间: |
|
| 查看次数: |
11376 次 |
| 最近记录: |