【问题标题】:Nginx error in my aws elastic beanstalk log files我的aws弹性beantalk日志文件中的Nginx错误
【发布时间】:2023-03-11 01:08:01
【问题描述】:

我已经在配置了 nginx 服务器的弹性 beanstalk 上部署了我的 nodejs rest API,我收到以下错误:

2021/03/03 15:01:15 [error] 18614#0: *1381 upstream prematurely closed connection while reading response header from upstream, client: 172.31.17.180, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.43.117"

2021/03/03 15:01:30 [error] 18614#0: *1385 upstream prematurely closed connection while reading response header from upstream, client: 172.31.17.180, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.43.117"

2021/03/03 15:01:45 [error] 18614#0: *1389 upstream prematurely closed connection while reading response header from upstream, client: 172.31.17.180, server: , request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8080/", host: "172.31.43.117"

我已经尝试了很多东西,还在我的根目录中添加了一个 .ebextensions 文件夹,其中包含一个名为 nginx.config 的文件,该文件具有以下配置,由 aws 文档提供,但它似乎仍然不起作用。我花了 3 天时间试图解决这个问题,但我无法解决。如果有人能帮我解决这个问题,我将不胜感激

我的nginx配置如下:

files:
/etc/nginx/conf.d/proxy.conf:
mode: "000644"
owner: root
group: root
content: |
  upstream nodejs {
    server 127.0.0.1:5000;
    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;
    access_log  /var/log/nginx/access.log  main;

    location / {
        proxy_pass  http://nodejs;
        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;
    }

    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;

    location /static {
        alias /var/app/current/static;
    }

  }

/opt/elasticbeanstalk/hooks/configdeploy/post/99_kill_default_nginx.sh:
mode: "000755"
owner: root
group: root
content: |
  #!/bin/bash -xe
  rm -f /etc/nginx/conf.d/00_elastic_beanstalk_proxy.conf
  service nginx stop 
  service nginx start

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"

【问题讨论】:

    标签: node.js amazon-web-services nginx


    【解决方案1】:

    您尝试使用的 nginx 设置 (/etc/nginx/conf.d/proxy.conf) 适用于 Amazon Linux 1

    由于您可能使用的是 Amazon Linux 2 (AL2),因此您应该使用不同的文件来设置 nginx。对于 AL2,nginx 设置应在.platform/nginx/conf.d/ 中,而不是在.ebextentions 中,如docs 中所示。或者,如果您想完全覆盖主要的 nginx.conf,您应该使用 .platform/nginx/nginx.conf 提供其自定义版本。

    假设你的 nginx 配置没问题,你可以尝试创建文件.platform/nginx/conf.d/myconf.conf,内容为:

      upstream nodejs {
        server 127.0.0.1:5000;
        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;
        access_log  /var/log/nginx/access.log  main;
    
        location / {
            proxy_pass  http://nodejs;
            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;
        }
    
        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;
    
        location /static {
            alias /var/app/current/static;
        }
    
      }
    

    【讨论】:

    • 我明白你在说什么,如果你能告诉我该怎么做,我会很高兴。据我了解,您告诉我删除 ebextesions 文件夹并在根目录中创建一个文件夹名称“.platfrom”,其中包含一个带有文件 conf.d 的 nginx 文件夹。这是你说的吗?
    • @FaheelMohammad 是的。在 AL2 中,nginx 是使用 .platform 而不是 .ebextesions 设置的。我无法建议那里文件的实际内容,因为您的 nginx 配置本身可能不正确。
    • 好吧,我试着在你定义的路径中复制上面的配置,如果一切正常,你的问题会被标记为已接受。谢谢
    • @FaheelMohammad 我用你可以尝试的方法更新了答案。
    • 是的。我正在调查它
    猜你喜欢
    • 2016-10-27
    • 2020-11-01
    • 2015-05-22
    • 1970-01-01
    • 2013-01-18
    • 2021-11-20
    • 1970-01-01
    • 2015-12-10
    • 1970-01-01
    相关资源
    最近更新 更多