Nginx上游有http&https

dpn*_*pnz 6 ssl https reverse-proxy http nginx

我有一些关于nginx与http和https绕过的问题,在上游块

上游区块:

upstream bypass{
      server 192.168.99.1:80; #http
      server 192.168.99.2:443 backup; #https
}
Run Code Online (Sandbox Code Playgroud)

当http 80出现问题(服务器关闭等)时,我想重定向到https 443,

这个块对我不起作用.

位置块:

location / {
      proxy_pass https://bypass;
      proxy_redirect off;
}
Run Code Online (Sandbox Code Playgroud)

我该如何解决这个问题?

Afr*_*ndr 1

在黑暗中拍摄。假设您在上游混合 HTTP 和 HTTPS 时遇到问题,您可以在location块中尝试以下操作:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

如果这不起作用,请将bypass上游块拆分为bypass1bypass2,并在相应的位置块中相应地引用它们:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

第三个选项是在端口 80 上引用它们,并确保第二个上游服务器将 HTTP 请求重定向到 HTTPS。