具有多个子域的 nginx 反向 ssl 代理

Bri*_*anM 11 nginx reverse-proxy ssl-certificate

我正在尝试为我目前的情况找到一个高级配置示例。我们有一个通配符 SSL 证书,用于多个内部 IIS 服务器上的多个子域。

site1.example.com (X.X.X.194) -> IISServer01:8081
site2.example.com (X.X.X.194) -> IISServer01:8082
site3.example.com (X.X.X.194) -> IISServer02:8083
Run Code Online (Sandbox Code Playgroud)

我希望通过一个服务器条目处理传入的 SSL 流量,然后将特定域传递给内部 IIS 应用程序。看来我有两个选择:

  1. 为每个子域编码一个位置部分(从我发现的例子来看似乎很混乱)

  2. 将未加密的流量转发回相同的 nginx 服务器,为每个子域主机名配置不同的服务器条目。(至少这似乎是一种选择)。

我的最终目标是将我们的大部分 SSL 流量整合到 nginx 中,以便我们可以使用 HAProxy 对服务器进行负载平衡。

如果我正确设置了 proxy_set_header 条目,方法 #2 会在 nginx 中工作吗?

我在我的最终配置文件中设想了一些类似的东西(使用方法 #2):

server {
  listen Y.Y.Y.174:443; #Internally routed IP address
  server_name *.example.com;

  proxy_pass http://Y.Y.Y.174:8081;
}

server {
  listen Y.Y.Y.174:8081;
  server_name site1.example.com;

  -- NORMAL CONFIG ENTRIES --

  proxy_pass http://IISServer01:8081;
}

server {
  listen Y.Y.Y.174:8081;
  server_name site2.example.com;

  -- NORMAL CONFIG ENTRIES --

  proxy_pass http://IISServer01:8082;
}

server {
  listen Y.Y.Y.174:8081;
  server_name site3.example.com;

  -- NORMAL CONFIG ENTRIES --

  proxy_pass http://IISServer02:8083;
}
Run Code Online (Sandbox Code Playgroud)

这似乎是一种方式,但我不确定这是否是最好的方式。我错过了一个更简单的方法吗?

小智 14

我会做这样的事情(用 nginx 1.4.2 测试,似乎有效):

server {
  listen 127.0.0.1:443 ssl;
  server_name site1.example.com;

  include common.conf;

  location / {
    proxy_pass http://127.0.0.2:8081;
  }
}

server {
  listen 127.0.0.1:443 ssl;
  server_name site2.example.com;

  include common.conf;

  location / {
    proxy_pass http://127.0.0.2:8082;
  }
}

server {
  listen 127.0.0.1:443 ssl;
  server_name site3.example.com;

  include common.conf;

  location / {
    proxy_pass http://127.0.0.3:8083;
  }
}
Run Code Online (Sandbox Code Playgroud)

至少有这个common.conf

ssl on;
ssl_certificate  /path/to/cert;
ssl_certificate_key  /path/to/key;
Run Code Online (Sandbox Code Playgroud)

  • 你可以只使用“listen 443 ssl”而不使用本地ip吗?不是隐含了“127.0.0.1”吗? (2认同)

Aar*_*ams 8

信不信由你,你可以这样做:

ssl_session_cache shared:SSL:2m;
ssl_session_timeout 5m;

server {
    listen Y.Y.Y.174:443 default_server ssl;
    server_name _;
    ssl_certificate /etc/pki/tls/certs/server.chained.crt;
    ssl_certificate_key /etc/pki/tls/private/server.key;
}

server {
    listen Y.Y.Y.174:443 ssl;
    server_name site1.example.com;
    [...]
    proxy_pass http://IISServer01:8081;
}

server {
    listen Y.Y.Y.174:443 ssl;
    server_name site2.example.com;
    [...]
    proxy_pass http://IISServer01:8082;
}

server {
    listen Y.Y.Y.174:443 ssl;
    server_name site3.example.com;
    [...]
    proxy_pass http://IISServer02:8083;
}
Run Code Online (Sandbox Code Playgroud)

不包含,证书只加载到内存中一次,并且会话甚至应该在用户从子域移动到子域时保持缓存,从而为您节省大量的握手能力。

除了此服务器故障帖子之外,我找不到任何文档来说明为什么会这样,但我可以向您保证确实如此。