Nginx upstream with http & https

httphttpsnginxreverse-proxyssl

I have some problem about nginx with http and https bypass, In upstream block

upstream block:

upstream bypass{
      server 192.168.99.1:80; #http
      server 192.168.99.2:443 backup; #https
}

When http 80 have a problem (server down, etc), I want to redirect to https 443,

This block does not work for me.

location block:

location / {
      proxy_pass https://bypass;
      proxy_redirect off;
}

How can I resolve this?

Best Answer

Taking a shot in the dark. Assuming you were having issues mixing HTTP and HTTPS in the upstream, you could try this in the location block:

location {
    try_files @bypass-http @bypass-https =404;

    location @bypass-http {
        proxy_pass http://bypass;
        proxy_redirect off;
    }

    location @bypass-https {
        proxy_pass https://bypass;
        proxy_redirect off;
    }
}

And if that didn't work, split the bypass upstream block into bypass1 and bypass2 and reference them accordingly in their corresponding location blocks:

upstream bypass1{
      server 192.168.99.1:80; #http
}

upstream bypass2{
      server 192.168.99.2:443; #https
}

location {
    try_files @bypass-http @bypass-https =404;

    location @bypass-http {
        proxy_pass http://bypass1;
        proxy_redirect off;
    }

    location @bypass-https {
        proxy_pass https://bypass2;
        proxy_redirect off;
    }
}

A third option would be reference them both on port 80, and ensure the second upstream server redirects HTTP requests to HTTPS.