微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何configurationnginx位置指令的.ebextensions?

我想在Elastic Beanstalk中configuration我的临时环境,以始终禁止所有的蜘蛛。 Nginx指令看起来像这样:

location /robots.txt { return 200 "User-agent: *ndisallow: /"; }

我知道我想在.ebextensions /文件夹下创build一个文件,例如01_Nginx.config,但是我不确定如何在它内部构buildYAML以使其可以工作。 我的目标是将此位置指令添加到现有configuration中,而不必完全replace现有的任何现有configuration文件

静态文件显示为未压缩,即使在web服务器configuration为gzip压缩时也是如此

如何通过ebextensionsconfiguration文件添加Nginxcaching控制标头?

MMMMM! .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

也就是说,首先创建一个你需要的Schell脚本,然后运行它。

哦,小心你的YAML没有标签! 只允许空格…检查日志文件/var/log/cfn_init.log是否有错误

祝你好运!

这是可以使用.ebextension配置文件,但是我很难踢Nginx重新启动后,更改其配置文件

# .ebextensions/Nginx.config files: "/etc/Nginx/conf.d/robots.conf": mode: "000544" owner: root group: root content: | location /robots.txt { return 200 "User-agent: *ndisallow: /"; } encoding: plain

现在,我已经做了类似的添加一个文件Nginx轮胎,但由于一些奇怪的原因,它不执行:

"/opt/elasticbeanstalk/hooks/appdeploy/enact/03_restart_Nginx.sh": mode: "000755" owner: root group: root content: | #!/usr/bin/env bash . /opt/elasticbeanstalk/containerfiles/envvars sudo service Nginx restart ps aux | grep Nginx > /home/ec2-user/Nginx.times.log true encoding: plain

我想做同样的事情。 经过大量的挖掘,我找到了两种方法来做到这一点:

选项1.使用ebextension将Nginx配置文件替换为您的自定义配置

我使用这个选项是因为它是最简单的。

根据Amazon在使用AWS Elastic Beanstalk Node.js平台 – 配置代理服务器 – 例如.ebextensions / proxy.config中给出的示例 ,我们可以看到它们创建了一个ebextension,它创建了一个名为/ etc / Nginx / conf的文件。 d / proxy.conf 。 该文件包含与原始Nginx配置文件相同的内容。 然后,他们使用container_commands删除原始的Nginx配置文件

您需要将Amazon示例替换为当前Nginx配置文件内容。 请注意,要在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服务Nginx重新加载

选项2.使用ebextension修改Nginx配置文件生成器,以便在最终的Nginx配置文件中包含您的自定义位置

第二个选项是基于这个职位: jabbermarky在亚马逊论坛的答案

他在回答中很好地解释了这个方法,所以如果你想要实现的话,我鼓励你阅读它。 如果你要实现这个答案,你需要更新Nginx文件配置生成器的位置。

请注意,我没有测试过这个选项。

总之,他添加一个shell脚本,在生成Nginx配置文件之前执行。 在这个shell脚本中,他修改Nginx配置文件生成器,在生成Nginx配置文件中包含了他想要的服务器块位置。 最后,他在最终的Nginx配置文件的服务器块中添加一个包含他想要的位置的文件

似乎提到的方法不工作了。 新的方法是将Nginx .conf文件放入.ebextensions中的子文件夹中:

您现在可以在.ebextensions / Nginx文件夹中放置一个Nginx.conf文件来覆盖Nginx配置。 您也可以将配置文件放在.ebextensions / Nginx / conf.d文件夹中,以便将它们包含在平台提供的Nginx配置中。

资源

这不需要重新启动Nginx,因为Elastic Beanstalk会照顾到这一点。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐