首页 > 解决方案 > 在弹性 beantalk 上自定义 nginx.conf

问题描述

我需要自定义文件中的keepalive_timeout设置/etc/nginx/nginx.conf,默认为65当前在弹性 beanstalk ec2 实例上。

我遵循了以下描述,但是当我部署新代码时,出现 nginx 错误,例如:

[emerg] 4551#0:“keepalive_timeout”指令在 /etc/nginx/conf.d/proxy.conf:2 中重复

后来我尝试使用 sed 直接更新 nginx.conf 如下

files:
  "/etc/nginx/conf.d/proxy.conf" :
    mode: "000755"
    owner: root
    group: root
    content: |
      client_max_body_size 200M;
      client_header_timeout   300;
      client_body_timeout     300;
      send_timeout            300;
      proxy_connect_timeout   300;
      proxy_read_timeout      300;
      proxy_send_timeout      300;
container_commands:
  01_update_nginx:
    command: "sudo sed -i 's/keepalive_timeout  65;/keepalive_timeout  360;/g' /etc/nginx/nginx.conf"
  02_restart_nginx:
    command: "sudo service nginx reload"

哪个不再起作用(值没有被替换)。所以我正在寻找更新/自定义nginx.conf文件的正确方法。我尝试了这样的事情 ,这给了我一个错误,比如:

服务:AmazonCloudFormation,消息:[/Resources/AWSEBAutoScalingGroup/Metadata/AWS::CloudFormation::Init/prebuild_0_appname/files//opt/elasticbeanstalk/#etc#nginx#custom-nginx.conf] 'null' 值不允许在模板

标签: nginxamazon-elastic-beanstalk

解决方案


如果你想走这sed条路,命令应该是这样的:

01_update_nginx:
  command: "sudo sed -i 's/keepalive_timeout  65;/keepalive_timeout  360;/g' /tmp/deployment/config/#etc#nginx#nginx.conf"

那是 Beanstalk 用来替换默认文件的实际nginx.conf(以及其他,例如)的位置。00_elastic_beanstalk_proxy.conf

不需要该02_restart_nginx命令,您可以删除该命令(截至 2019 年 8 月)。

我在 Beanstalk 环境中测试了这个,Node.js 在 64 位 Amazon Linux/4.8.2 上运行。总的来说,它看起来有点老套,您可能需要咨询 AWS 支持以了解自定义 nginx.conf 的“官方”方式。


推荐阅读