【问题标题】:AWS ELB keeps returning 404 Health checks failed with these codes: [404]AWS ELB 不断返回 404 健康检查失败,代码如下:[404]
【发布时间】:2022-01-25 04:16:13
【问题描述】:

我正在使用 pm2 为我的节点 js 应用程序提供服务。

nginx.conf 文件是

user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

# Load dynamic modules. See /usr/share/doc/nginx/README.dynamic.
include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

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

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    # Load modular configuration files from the /etc/nginx/conf.d directory.
    # See http://nginx.org/en/docs/ngx_core_module.html#include
    # for more information.
    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        listen       [::]:80;
        server_name  _;
        root         /usr/share/nginx/html;
        # Load configuration files for the default server block.
        #include /etc/nginx/default.d/*.conf;

        error_page 404 /404.html;
            location = /40x.html {
        }

        error_page 500 502 503 504 /50x.html;
            location = /50x.html {
        }
    }
}

在 **conf.d/** 文件夹中,我创建了如下配置文件(名称为 api.conf)

server {
    listen 80;
    server_name dev.xxxxxxx.com;
    underscores_in_headers on;
      #  return 301 https://$host$request_uri;
        location / {
        proxy_pass http://localhost:3004/;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection 'upgrade';
        proxy_set_header Host $host;
        proxy_cache_bypass $http_upgrade;
     }
}

当我停止节点应用程序 nginx server the ELB Health status is 502 并在我启动 nginx 服务器时启动然后 nginx server the ELB Health status is 404

我无法找到问题所在。谁能帮我理解和解决

即使我检查了如下的 nginx 错误日志

[02/Feb/2021:17:21:29 +0000] "GET / HTTP/1.1" 502 157 "-" "ELB-HealthChecker/2.0" "-"
[02/Feb/2021:17:21:38 +0000] "GET / HTTP/1.1" 404 60 "-" "ELB-HealthChecker/2.0" "-"

【问题讨论】:

  • 应用程序是什么样的,是否有任何日志?您在应用程序运行时从 502 切换到 404 的事实意味着应用程序本身正在发回 404 错误。
  • 当您 ssh 到实例并从内部卷曲它时,应用程序是否工作?问题仅在于 ELB 健康检查,还是没有任何实际作用?
  • @Marcin 是的,该应用与 http 一起工作正常,但当它与 https 一起使用时,它不工作
  • @tedivm 是的,我在问题中提到的日志相同。当我停止并重新启动 nginx 时,我收到这些 502 和 404 错误
  • 你能告诉我们来自应用程序的日志,而不仅仅是来自 nginx 的日志吗?

标签: amazon-web-services nginx load-balancing amazon-elb aws-application-load-balancer


【解决方案1】:

设置运行状况检查时,ALB 不包含 HOST: 标头值。

如果您修改日志,您将看到 ELBHealthCheck 请求来自私有 IP 地址。

所以当 nginx 运行时,它会从一个不匹配任何服务器元素的私有 IP 获取请求。由于您没有设置 default_server,它默认为第一个,在这种情况下,它将是您的节点应用程序的代理。

/usr/share/nginx/html 中有没有文件?尝试将 index.html 放在那里,并将 default_server 添加到端口配置中:

      listen    80 default_server;

【讨论】:

    【解决方案2】:

    我来得太晚了,但如果有人遇到同样的问题,我仍然会补充。我的设置是

    AWS ALB --> AWS EC2 在 EC2 上安装 Nginx 并遇到与上述相同的问题: "GET /var/www/html/index.nginx-debian.html HTTP/1.1" 404 134 "-" "ELB-HealthChecker/2.0"

    我犯的错误是我在Health checks 选项卡中将完整路径添加为/var/www/html/index.nginx-debian.html,因此,我收到了上述错误。

    解决方案:只需要在Health Checks标签中添加/index.nginx-debian.html,就会开始得到200s。

    希望它对某人有所帮助并获得支持:-)

    【讨论】:

      【解决方案3】:

      对于休息端点用户,您可以定义一个端点进行健康检查。返回状态码 200 作为响应。 首先,我们需要在 AWS 中配置健康检查端点。 EC2-->Load Balancing-->Target Groups-->Select Target Group-->Actions-->Edit Health check Settings。 定义一个健康检查路径,如 /health。 保存更改。 现在在您的应用程序中创建一个端点 /health。我在 sprinboot 中完成了它,因此粘贴相同以供参考。

      @CrossOrigin(value = "*")
      @RequestMapping(value = "/health",method = RequestMethod.GET)
      public ResponseEntity<?> health() throws Exception {
          try {
              return ResponseEntity.status(200).body("Ok");
          } catch (Exception e) {
              return (ResponseEntity<?>) ResponseEntity.internalServerError().body(e.getMessage());
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2020-12-18
        • 1970-01-01
        • 2019-02-09
        • 2019-10-25
        • 2014-10-02
        • 2018-10-29
        • 2020-05-29
        • 2016-11-29
        • 1970-01-01
        相关资源
        最近更新 更多