如何在Elastic Beanstalk AWS上修改NGINX路由?

时间:2017-11-18 01:11:31

标签: amazon-web-services nginx amazon-ec2 elastic-beanstalk ebextensions

所以我有一篇非常长篇文章,涉及到Elastic Beanstalk(此处:HTTP 500 Deploying Elixir/Phoenix to AWS Elastic Beanstalk)部署elixir应用程序。

我终于将问题归结为我的NGINX错误配置路由流量。它发布了这个:

2017/11/18 00:52:45 [error] 2811#0: *15 connect() failed (113: No route to host) 
while connecting to upstream, client: 172.31.25.36, server: ,
request: "GET / HTTP/1.1",
upstream: "http://172.17.0.2:4000/", host: "172.31.17.239"

我已经在网上寻找可能的解决方案并找到了(我还需要添加使用这些代码看起来最接近的websockets的功能:https://scopestar.com/blog/aws/elasticbeanstalk/websockets/2016/10/21/enable-websockets-on-elasticbeanstalk-nginx-proxy.html

到目前为止我已经得到了以下内容 - 它删除了之前的配置,用websockets写了一个新配置。

files:
  "/etc/nginx/conf.d/01_nginx_websocket.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        server 127.0.0.1:8081;
        keepalive 256;
      }

      server {
        listen 8080;

        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/healthd/application.log.$year-$month-$day-$hour healthd;


        location / {
          proxy_pass  http://nodejs;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          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;
        }

      gzip on;
      gzip_comp_level 4;
      gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

      }

container_commands:
 removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

但我对实际路由的方式有点困惑。是否有一个很好的例子来说明如何更改上游和服务器路由以便它们正确握手并解决我得到的错误?

编辑:

本网站:https://dennisreimann.de/articles/phoenix-nginx-config.html

建议我按照我的Dockerfile上的图像并将所有流量路由到localhost:myport,然后将其路由到AWSEB:80,这看起来很简单。

我在.ebextensions中创建了类似.conf:

的内容
files:
  "/etc/nginx/conf.d/01_nginx_websocket.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        server localhost:4000;
        keepalive 256;
      }

      server {
        listen 80 default_server;
        listen [::]:80 default_server;
        server_name newslyproduction3.us-west-2.elasticbeanstalk.com;

        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/healthd/application.log.$year-$month-$day-$hour healthd;


        location / {
          proxy_pass  http://nodejs;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          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;
        }

      gzip on;
      gzip_comp_level 4;
      gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

      }

container_commands:
 removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

然而,这会产生不同的错误,我不确定我是否越来越近或移动得越来越远。

INFO: Successfully built aws_beanstalk/staging-app
ERROR: Failed to start nginx, abort deployment
ERROR: [Instance: i-0c48e103c226ca0a9] Command failed on instance. Return code: 1 Output: (TRUNCATED)...enabled/elasticbeanstalk-nginx-docker-proxy.conf:11
nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 64
nginx: configuration file /etc/nginx/nginx.conf test failed
Failed to start nginx, abort deployment. 

这篇文章Nginx getting error with specific domain name表明我需要增加http块中的名称大小,但我不知道它在AWS EB的nginx中的位置。

编辑:

我从这里找到的代码段https://dennisreimann.de/articles/phoenix-nginx-config.html(以及其他地方我将我的文件重新编写为如下所示):

files:
  "/etc/nginx/conf.d/01_nginx_websocket.conf":
    mode: "000644"
    owner: root
    group: root
    content: |
      upstream nodejs {
        server localhost:4000;
        keepalive 256;
      }

      # log_format healthd '$msec"$uri"'
      #          '$status"$request_time"$upstream_response_time"'
      #          '$http_x_forwarded_for';

      server {
        listen 80;
        server_name _ localhost;

        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/healthd/application.log.$year-$month-$day-$hour healthd;

        access_log    /var/log/nginx/access.log;


        location / {
          proxy_pass http://newslyproduction3.us-west-2.elasticbeanstalk.com;
          proxy_set_header Upgrade $http_upgrade;
          proxy_set_header Connection "upgrade";
          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;
        }

      gzip on;
      gzip_comp_level 4;
      gzip_types text/html text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript;

      }

container_commands:
 removeconfig:
    command: "rm -f /tmp/deployment/config/#etc#nginx#conf.d#00_elastic_beanstalk_proxy.conf /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf"

所以现在它在docker机器上引用localhost并路由到我的实例。或者我想。

此上传正常,但随后NGINX的以下日志失败:

-------------------------------------
/var/log/nginx/access.log
-------------------------------------
172.31.35.70 - - [18/Nov/2017:04:12:30 +0000] "GET / HTTP/1.1" 463 0 "-" "ELB-HealthChecker/2.0"
172.31.6.206 - - [18/Nov/2017:04:12:30 +0000] "GET / HTTP/1.1" 463 0 "-" "ELB-HealthChecker/2.0"
172.31.35.70 - - [18/Nov/2017:04:12:30 +0000] "GET / HTTP/1.1" 463 0 "-" "ELB-HealthChecker/2.0"
172.31.6.206 - - [18/Nov/2017:04:12:30 +0000] "GET / HTTP/1.1" 463 0 "-" "ELB-HealthChecker/2.0"

我的应用程序启动了powercycling

-------------------------------------
/var/log/eb-docker/containers/eb-current-app/20c6293b969d-stdouterr.log
-------------------------------------

** (exit) exited in: :gen_server.call(#PID<0.221.0>, {:checkout, #Reference<0.0.1.3329>, true, :infinity}, 5000)
    ** (EXIT) time out
    (db_connection) lib/db_connection/poolboy.ex:112: DBConnection.Poolboy.checkout/3
    (db_connection) lib/db_connection.ex:920: DBConnection.checkout/2
    (db_connection) lib/db_connection.ex:742: DBConnection.run/3
    (db_connection) lib/db_connection.ex:1133: DBConnection.run_meter/3
    (db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4

** (exit) exited in: :gen_server.call(#PID<0.221.0>, {:checkout, #Reference<0.0.1.3329>, true, :infinity}, 5000)
    ** (EXIT) time out
    (db_connection) lib/db_connection/poolboy.ex:112: DBConnection.Poolboy.checkout/3
    (db_connection) lib/db_connection.ex:920: DBConnection.checkout/2
    (db_connection) lib/db_connection.ex:742: DBConnection.run/3
    (db_connection) lib/db_connection.ex:1133: DBConnection.run_meter/3
    (db_connection) lib/db_connection.ex:584: DBConnection.prepare_execute/4

dockerfile编译并在本地运行(使用非生产配置),我有生产配置和dockerfile,我正在使用的教程说(https://robots.thoughtbot.com/deploying-elixir-to-aws-elastic-beanstalk-with-docker),所以我唯一可以认为这必须是某种NGINX错误。

我觉得我离我更近了,尝试将localhost路由到AWS EB端点是正确的方向。

编辑:

我也在ServerFault上提出了一个问题,因为它们可能是一个更具针对性的社区,并且有一些想法https://serverfault.com/questions/884036/how-do-i-modify-nginx-routing-on-elastic-beanstalk-aws-so-x-post

0 个答案:

没有答案