使用 NGINX 上游,是否可以代理传递到同一上游中的 HTTP 和 HTTPS 后端?

With NGINX upstreams, is it possible to proxy pass to both HTTP and HTTPS backends in the same upstream?

假设我想将部分流量代理到远程后端而不是服务器上的本地侦听器。例如:

upstream backends {
    server 127.0.0.1:8080 weight=20;  # local process (HTTP)
    server other-remote-backend.company-internal.com:443;  # remote server (HTTPS)
}


location / {
    # ...other stuff...
    proxy_pass http://backends;
}

在上面的配置中,每 20 个左右的请求 NGINX 将尝试路由到仅侦听 SSL 的 http://other-remote-backend.company-internal.com:443

有没有办法让上游定义自己的协议方案?现在,如果不将本地侦听器进程也更改为 SSL(这是一个不太理想的更改),这似乎是不可撤销的。

谢谢

像往常一样,我已经找出了我自己的问题,而且它很明显。如果你想完成上述的技巧就很简单了。

  1. 首先创建一个新的 NGINX 虚拟主机来侦听 HTTP 并 proxy_passes 到您的远程 HTTPS 后端,如下所示:

/etc/nginx/sites-available/remote_proxy

upstream remote {
        server other-remote-backend.company-internal.com:443;
}

server {

        # other-remote-backend.company-internal.com:443;

        listen 8181;

        server_name my_original_server_name;

        location / {
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-For $remote_addr;
            proxy_pass https://remote;
        }

}
  1. 您现在可以在监听 443 的原始配置中仅使用 http 作为您的上游:

/etc/nginx/sites-available/default

upstream backends {
    server 127.0.0.1:8080 weight=20;  # local process (HTTP)
    server 127.0.0.1:8181 # local nginx proxying to HTTPS remote
}


location / {
    # ...other stuff...
    proxy_pass http://backends;
}

现在只需启用您的新站点并重新启动 $ ln -s /etc/nginx/sites-available/remote_proxy /etc/nginx/sites-enabled/ && systemctl restart nginx