Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js app with nginx 502 bad gateway error

Tags:

node.js

nginx

i am configuring my node.js app with nginx. It is working fine for http but it is not working for https. When i try to access secure domain. i get this error.

502 Bad Gateway
nginx/1.4.6 (Ubuntu)

Here is my nginx conf file

    upstream node_app_dev {
        server 127.0.0.1:3000;
    }

    upstream node_app_production {
        server 127.0.0.1:3000;
    }

server {
    listen 80;
    server_name mydomain.com;
    access_log /var/log/nginx/dev.log;
    error_log /var/log/nginx/dev.error.log debug;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $http_host;
        proxy_pass http://node_app_dev;
        proxy_redirect off;
    }
}


server {
    listen 443 ssl;
    server_name mydomain.com;
    access_log /var/log/nginx/secure.log;
    error_log /var/log/nginx/secure.error.log debug;    

    ssl on;
    ssl_certificate certs/mycert.crt;
    ssl_certificate_key certs/mykey.key;

    location / {
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarder-For $proxy_add_x_forwarded_for;
        proxy_set_header Host $host;        
        proxy_pass https://node_app_production;
        proxy_redirect off;

    }    
}
like image 524
mabc224 Avatar asked Oct 23 '15 00:10

mabc224


People also ask

Why do I keep getting a 502 Bad gateway message?

A 502 bad gateway message indicates that one server got an invalid response from another. In essence, you've connected with some kind of interim device (like an edge server) that should fetch all of the bits you need to load the page. Something about that process went wrong, and the message indicates the problem.

Is a 502 Bad gateway my fault?

There is a good chance that if you have run into this error message while browsing, you're not at fault. Typically, an Error 502 bad gateway indicates that there is an issue with the website's server, rather than anything on your end.


1 Answers

Replace

proxy_pass https://node_app_production;

with

proxy_pass http://node_app_production;

Restart the nginx and you should be all set. See nginx proxy pass Node, SSL?

like image 181
jpaljasma Avatar answered Oct 01 '22 03:10

jpaljasma