简体   繁体   中英

Nginx 502 Bad Gateway Node JS

I have a ELB setup in Amazon that's pointing to two servers that are running Nginx. Nginx is then forwarding the request locally to a different port that runs our Node JS application.

As I'm testing for redundancy, if I stop the Node JS application from running, Nginx will return a 502 Bad Gateway which I fully expect. The problem I'm having is how can I have the ELB determine that this is a bad page and that it should stop sending requests to a problematic server? The ELB seems to see the Nginx 502 error as a valid HTTP request and thus won't remove it.

the ELB monitor doesn't show any 500 errors either

EDIT:

Nginx config:
server {
  listen 80;
  server_name  *.domain.com XX.XX.XX.XX;
  access_log  off;
  client_max_body_size 2048M;
  rewrite ^(.*) https://$host$1 permanent;
  add_header X-Whom USE1A-01;
}

server {
 listen 443;
 ssl on;
 ssl_certificate /ssl.crt;
 ssl_certificate_key /ssl.key;
 server_name *.domain.com XX.XX.XX.XX;

 access_log  off;
 client_max_body_size 2048M;

 location / {
   proxy_set_header Host $host;
   proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
   proxy_set_header X-Forwarded-Proto https;
   proxy_set_header Host $host;
   proxy_pass https://127.0.0.1:XXXX;
 }
}

What my nginx access log is showing for the health check when the app is NOT running:

10.50.101.244 - - [07/Jan/2014:21:31:24 +0000] "-" 400 0 "-" "-"
10.50.101.244 - - [07/Jan/2014:21:31:34 +0000] "GET /healthCheck HTTP/1.1" 200 151 "-" "ELB-HealthChecker/1.0"
10.50.101.244 - - [07/Jan/2014:21:31:54 +0000] "-" 400 0 "-" "-"
10.50.101.244 - - [07/Jan/2014:21:32:04 +0000] "GET /healthCheck HTTP/1.1" 200 151 "-" "ELB-HealthChecker/1.0"
10.50.101.244 - - [07/Jan/2014:21:32:24 +0000] "-" 400 0 "-" "-"
10.50.101.244 - - [07/Jan/2014:21:32:34 +0000] "GET /healthCheck HTTP/1.1" 200 151 "-" "ELB-HealthChecker/1.0"
10.50.101.244 - - [07/Jan/2014:21:32:54 +0000] "-" 400 0 "-" "-"
10.50.101.244 - - [07/Jan/2014:21:33:04 +0000] "GET /healthCheck HTTP/1.1" 200 151 "-" "ELB-HealthChecker/1.0"
10.50.101.244 - - [07/Jan/2014:21:33:24 +0000] "-" 400 0 "-" "-"
10.50.101.244 - - [07/Jan/2014:21:33:34 +0000] "GET /healthCheck HTTP/1.1" 200 151 "-" "ELB-HealthChecker/1.0

Per 502 Bad Gateway Deploying Express Generator Template on Elastic Beanstalk ,

"Solved. I believe the issue here was that AWS was doing node app.js BEFORE npm start. node app.js doesn't give an error, but it doesn't open any ports. So the solution was to rename app.js to anything else (I used main.js) and reference that in bin/www. It's now working correctly."

Per https://www.ibm.com/cloud/blog/node-js-502-bad-gateway-issues-and-how-to-resolve-them , adding:

"engines": {
    "node": "^7.10.0"
}

to my package.json (and then re-running npm install for good measure and restarting my app) seemed to solve the problem, probably, since I'm now experiencing a "Not Found" error.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM