14

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;

    }    
}
mabc224
  • 722
  • 2
  • 10
  • 18
  • Possible duplicate of http://stackoverflow.com/questions/10375659/nginx-proxy-pass-node-ssl : If you're using nginx to handle SSL, then your node server will just be using http. – jpaljasma Oct 23 '15 at 01:36
  • my node.js server is simple http server. i am handling https with nginx and it is pointing to simple http server. – mabc224 Oct 23 '15 at 01:48
  • I am glad it worked out for you. Would you be willing to up vote my answer? – jpaljasma Oct 23 '15 at 01:55

2 Answers2

18

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?

Community
  • 1
  • 1
jpaljasma
  • 1,612
  • 16
  • 21
1

I've resolved the issue with 2 steps.

  1. Check /var/log/nginx/error.log
connect() failed (111: Connection refused) while connecting to upstream, client: *.*.*.*, server: *.*.*.*, request: "GET / HTTP/1.1", upstream: "http://127.0.0.1:8000/", host: "*.*.*.*"

       Upstream was still 127.0.0.1:8000 even if I set upstream to 127.0.0.1:3000 in nginx conf file.

  1. Replace server 127.0.0.1:8000 with server 127.0.0.1:3000 in /etc/nginx/conf.d/virtual.conf and restart nginx.
server {
    listen       80;
    server_name  SERVER_IP_ADDRESS;

    location / {
        proxy_pass http://127.0.0.1:3000;
    }
}
sudo /etc/init.d/nginx restart 

Finally, it works with no 502 error.

Denis Wang
  • 21
  • 4