如何为nginx位置指令配置.ebextensions? [英] How to configure .ebextensions for nginx location directive?

查看:121
本文介绍了如何为nginx位置指令配置.ebextensions?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Elastic Beanstalk中配置我的暂存环境以始终禁止所有蜘蛛. nginx指令如下所示:

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: /";
}

我知道我想在.ebextensions/文件夹下创建一个文件,例如01_nginx.config,但是我不确定如何在其中构造YAML以使其起作用.我的目标是将此位置指令添加到现有配置中,而不必完全替换任何已存在的现有配置文件.

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:

我使用了此选项,因为它是最简单的选项.

I used this option because it is the simplest one.

按照Amazon在使用AWS Elastic Beanstalk Node.js平台-配置代理服务器-示例.ebextensions/proxy.config ,我们可以看到他们创建了ebextension,该扩展名创建了一个名为/etc/nginx/conf.d/proxy.conf 的文件.该文件包含与原始nginx配置文件相同的内容.然后,他们使用 container_commands 删除原始的nginx配置文件.

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.

您需要用当前nginx配置文件的内容替换Amazon示例.注意,在containter命令中要删除的nginx配置文件也必须更新.我使用的是:

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:

  • nginx配置文件1 :/opt/elasticbeanstalk/support/conf/webapp_healthd.conf
  • nginx配置文件2 :/etc/nginx/conf.d/webapp_healthd.conf
  • nginx configuration file 1: /opt/elasticbeanstalk/support/conf/webapp_healthd.conf
  • nginx configuration file 2: /etc/nginx/conf.d/webapp_healthd.conf

因此,对我有用的最后一次伸肌如下:

Therefore, the final ebextension that worked for me is as follows:

/.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 reload

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

第二种选择基于此帖子:

The second option is based on this post: jabbermarky's answer in Amazon forums

他在回答中很好地解释了这种方法,因此,如果您想实现它,我建议您阅读它.如果要实现此答案,则需要更新nginx文件配置生成器的位置.

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.

总而言之,他添加了要在生成nginx配置文件之前执行的shell脚本.在此shell脚本中,他修改了nginx配置文件生成器,以将所需的服务器块位置包括在生成的nginx配置文件中.最后,他在最终的nginx配置文件的服务器块中添加了一个包含所需位置的文件.

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.

这篇关于如何为nginx位置指令配置.ebextensions?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆