skip to Main Content

I’m serving my node js application with pm2.

nginx.conf file is

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 {
        }
    }
}

and inside the **conf.d/**folder I have create configuration file like below (with the name of 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;
     }
}

when I stop the node application nginx server the ELB Health status is 502 and start when I start the nginx server then nginx server the ELB Health status is 404

I’m not able to find what is the issue. Could any one please help me to understand and solve

even I checked nginx error logs as below

[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" "-"

3

Answers


  1. When setting up a health check, the ALB does NOT include a HOST: header value.

    If you modify the log you will see that the ELBHealthCheck requests come from a private IP address.

    So when nginx is running, it gets a request from a private IP, which does not match any server elements. Since you don’t have a default_server set, it defaults to the first one, which in this case will be the proxy to your node app.

    Are there any documents in /usr/share/nginx/html? Try putting an index.html in there, and adding a default_server to the port configuration:

          listen    80 default_server;
    
    Login or Signup to reply.
  2. I’m too late here but still adding if someone gets the same issue. My Setup is

    AWS ALB –> AWS EC2 Installed Nginx on EC2 and getting the same issue as above:
    "GET /var/www/html/index.nginx-debian.html HTTP/1.1" 404 134 "-" "ELB-HealthChecker/2.0"

    The mistake I did was I have added the full path in the Health checks tab as /var/www/html/index.nginx-debian.html and due to this, I was getting the above error.

    Solution: Only need to add /index.nginx-debian.html in the Health Checks tab and it will start getting 200s.

    Hope it helps some one and get an Upvote 🙂

    Login or Signup to reply.
  3. For rest endpoint users, you can define an endpoint for health check.Return status code 200 in response.
    Firstly we need to configure Health check end point in AWS.
    EC2–>Load Balancing–>Target Groups–>Select Target Group–>Actions–>Edit Health check Settings.
    Define a health check path like /health.
    save changes.
    Now create an endpoint /health in your application.I did it in sprinboot so pasting same for reference.

    @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());
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search