2

因此,我有一篇长的文章与将 elixir 应用程序部署到 Elastic Beanstalk 相关(这里:HTTP 500 Deploying Elixir/Phoenix to AWS Elastic Beanstalk)。

我终于把问题归结为我的 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"

我的应用程序开始重新启动

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

4

0 回答 0