Nginx reverse proxying to upstream getting 502 Bad Gateway

502-errornginxreverse-proxysslUbuntu

My current Application deployment is receiving traffic on HTTP port and Nginx redirect it to HTTPS. This is working fine.

Now we are planning to have another Nginx server act like a Load balancer to these existing Nginx server aka Upstreams.

Below is configurations that I was trying

upstream web_rack {
    server x.x.x.x;
}
server {

    location / {
        proxy_pass https://web_rack;
    }
}

But getting 502 Bad Gateway error with below log:

2017/03/14 06:23:49 [error] 2402#2402: 5 SSL_do_handshake() failed (SSL: error:140770FC:SSL routines:SSL23_GET_SERVER_HELLO:unknown protocol) while SSL handshaking to upstream, client: 189.166.35.243, server: , request: "GET / HTTP/1.0", upstream: "https://x.x.x.x:80/"

Thanks in advance.

Best Answer

It seems as if you there may be something wrong with your upstream or server settings.

Your error message shows that you are trying to speak https to the upstream on port 80: upstream: "https://x.x.x.x:80/".

The following question relates to yours, and provides an (almost) working config to achieve the same as what you are trying to do: Configure Nginx as reverse proxy with upstream SSL.

At very least, you may want to amend your upstream to read:

upstream web_rack {
    server x.x.x.x:443;
}