将NGINX配置为反向代理(以分派)到已配置SSL的后端服务

时间:2020-06-02 20:02:29

标签: ssl nginx nginx-reverse-proxy

我有一台物理服务器和一台IP address。在该服务器上,我配置了三个(数量为3)后端服务,分别为拥有自己的公共DNS域名各自拥有自己的Let's Encrypt SSL certificates 显示:

 - svc01.example.com:1443  # Own DNS domain-name and SSL certificates.
 - svc02.example.com:2443  # Own DNS domain-name and SSL certificates.
 - svc03.example.com:3443  # Own DNS domain-name and SSL certificates.

它们共存于同一台物理服务器上,因此每个配置为使用自己的Port;但是public IP-Address的{​​{1}}是相同的。

请记住,这些后端服务已经配置了 SSL (我无法撤消该操作),如何配置svc01|02|03.example.com来实现以下简单转发(几乎就像调度员):

NGINX

可悲的是,我不是一个 svc01.example.com:443 --> svc01.example.com:1443 svc02.example.com:443 --> svc02.example.com:2443 svc03.example.com:443 --> svc03.example.com:3443 人,因此深切感谢每个人的完整配置文件:

NGINX

提前谢谢!

1 个答案:

答案 0 :(得分:1)

由于@SteffenUllrich在上面的注释中提供的提示,解决方案如下所示。不是一个nginx的人,我不确定是否还有其他最佳实践来使此配置更安全或更可靠(可以自由评论),但这只是一个开始。

/etc/nginx/nginx.conf

load_module /usr/lib/nginx/modules/ngx_stream_module.so;
  
events {}

stream {    
  map $ssl_preread_server_name $targetBackend {
    svc01.example.com svc01.example.com:1443
    svc02.example.com svc02.example.com:2443
    svc03.example.com svc03.example.com:3443    
  }

  server {
    listen 8443;    
    # ==============================================================
    # http://nginx.org/en/docs/stream/ngx_stream_proxy_module.html
    # ==============================================================
    proxy_connect_timeout 10s;
    proxy_timeout 600s;
    resolver 8.8.8.8 1.1.1.1 8.8.4.4 1.0.0.1;
    proxy_pass $targetBackend;
    # ==============================================================

    # ==============================================================
    # http://nginx.org/en/docs/stream/ngx_stream_ssl_preread_module.html
    # ==============================================================
    ssl_preread on;
    # ==============================================================
  }
}

我希望这对其他人有帮助。 (◠﹏◠)