简体   繁体   English

"如何在运行 docker 实例的亚马逊弹性 beanstalk 中更改 nginx 配置"

[英]How to change nginx config in amazon elastic beanstalk running a docker instance

After i login and the cookie is set I get error 502. When i read the log i get the error:在我登录并设置 cookie 后,我收到错误 502。当我阅读日志时,我收到错误:

014/05/17 01:54:43 [error] 11013#0: *8 upstream sent too big header while reading response
header from upstream, client: 83.248.134.236, server: , request: "GET /administration
HTTP/1.1", upstream:

Amazon actually recommends editing the staging version of the nginx deployment file. 亚马逊实际上建议编辑nginx部署文件的暂存版本。 There are several located at /tmp/deployment/config/ , one for editing the general 'http' context, and then a few for configuring different aspects of the server. 有几个位于/tmp/deployment/config/ ,一个用于编辑一般的'http'上下文,然后一些用于配置服务器的不同方面。

I wanted to attach caching functionality to the default proxy server, so I wrote an .ebextensions config file to replace #etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf , which is then copied over to /etc/nginx/conf.d during deployment. 我想将缓存功能附加到默认代理服务器,因此我编写了一个.ebextensions配置文件来替换#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf ,然后将其复制到/etc/nginx/conf.d期间。部署。 You can inline the file if its simple enough, but I put mine in S3 so that different applications and pull it down and use it. 如果文件足够简单,你可以内联文件,但是我把它放在S3中,以便不同的应用程序将其拉下并使用它。 Here's the config file: 这是配置文件:

commands: 
  01-get-nginx-conf-file:
    command: aws s3 cp s3://<bucket-name>/custom-nginx.conf /home/ec2-user

container_commands:
  01-replace-default-nginx-config:
    command: mv -f /home/ec2-user/custom-nginx.conf /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf

I also needed to modify the nginx configuration. 我还需要修改nginx配置。

  1. Create a script that modifies the nginx configuration (probably you want /etc/nginx/sites-enabled/elasticbeanstalk-nginx-docker.conf ) and restarts the nginx service ( service nginx restart ). 创建一个修改nginx配置的脚本(可能需要/etc/nginx/sites-enabled/elasticbeanstalk-nginx-docker.conf )并重新启动nginx服务( service nginx restart )。
  2. You need to execute that script after this nginx config file is written which is after normal ebextensions are executed. 在写入nginx配置文件之后,您需要执行该脚本,这是在执行正常的ebextensions之后。 This is undocumented, but Evan shared how to do this here : essentially you use an ebextension to copy the script into a directory with hooks that gets executed at the proper time. 这是没有记录的,但Evan 在这里分享了如何做到这一点 :基本上你使用ebextension将脚本复制到一个带有钩子的目录,这些钩子在适当的时候被执行。

An example ebextension config is .ebextensions/01modify_nginx.config : ebextension配置示例是.ebextensions/01modify_nginx.config

container_commands:
  copy:
    command: "cp .ebextensions/01rewrite_nginx_config.py /opt/elasticbeanstalk/hooks/appdeploy/enact/"
  make_exe:
    command: "chmod +x /opt/elasticbeanstalk/hooks/appdeploy/enact/01rewrite_nginx_config.py"

This is working nicely now for my project ( here is the source where you can see it in action). 现在这对我的项目很有效( 这里是您可以看到它的动作源)。

Another way to extend Elastic Beanstalk nginx config is to create a file in the .ebextensions directory, named for example nginx.config with the following content : 扩展Elastic Beanstalk nginx配置的另一种方法是在.ebextensions目录中创建一个文件,例如nginx.config ,其名称如下:

    files:
      "/etc/nginx/conf.d/000_my_config.conf":
      content: |
        upstream nodejsserver {
          server 127.0.0.1:8081;
          keepalive 256;
        }

        server {
          listen 8080;

          location / {
            proxy_pass  http://nodejsserver;
            proxy_set_header   Connection "";
            proxy_http_version 1.1;
            proxy_set_header        Host            $host;
            proxy_set_header        X-Real-IP       $remote_addr;
            proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
          }

          location /myconfig {
            proxy_pass http://my_proxy_pass_host;
          }
        }

/etc/nginx/conf.d/000_my_config.conf is the filename which will be created on the Elastic Beanstalk EC2 instances. /etc/nginx/conf.d/000_my_config.conf是将在Elastic Beanstalk EC2实例上创建的文件名。 By default this configuration is in the file /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf . 默认情况下,此配置位于文件/etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf So if you prefix with 000, it guarantees you that your configuration will be taken into account first. 因此,如果您使用000作为前缀,则可以保证您的配置将首先考虑在内。

The content has been copied from the default nginx configuration ( /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf again), then customized with my own configuration. 内容已从默认的nginx配置( /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf再次)复制,然后使用我自己的配置进行自定义。

A cleaner approach (if you're using the Java or Go platform on Elastic Beanstalk) is to have nginx .conf files with your wished changes in a subfolder in .ebextensions: 更简洁的方法(如果您在Elastic Beanstalk上使用Java或Go平台)是在.ebextensions中的子文件夹中包含您希望的更改的nginx .conf文件:

You can now place an nginx.conf file in the .ebextensions/nginx folder to override the Nginx configuration. 您现在可以在.ebextensions / nginx文件夹中放置nginx.conf文件以覆盖Nginx配置。 You can also place configuration files in the .ebextensions/nginx/conf.d folder in order to have them included in the Nginx configuration provided by the platform. 您还可以将配置文件放在.ebextensions / nginx / conf.d文件夹中,以便将它们包含在平台提供的Nginx配置中。

Source 资源

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM