如何为nginx位置指令配置.ebextensions?

我想在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配置文件的服务器块中添加了一个包含他想要的位置的文件.

相关文章

文章浏览阅读3.7k次,点赞2次,收藏5次。Nginx学习笔记一、N...
文章浏览阅读1.7w次,点赞14次,收藏61次。我们在使用容器的...
文章浏览阅读1.4k次。当用户在访问网站的过程中遇到404错误时...
文章浏览阅读2.7k次。docker 和 docker-compose 部署 nginx+...
文章浏览阅读1.3k次。5:再次启动nginx,可以正常启动,可以...
文章浏览阅读3.1w次,点赞105次,收藏182次。高性能:Nginx ...