在NGINX上使用共享设置多个SSL?

我有一个NGINX开发服务器。 我有大量的configuration指令驱动服务器的各种function。

我希望能够通过SSL访问服务器。 问题是我可能从不同的域名访问服务器。 例如,在我的局域网内,我可能使用192.168.1.100,但在互联网上(通过NAT转发),我会使用我的主域名,或者在某些特定情况下使用服务器的外部IP地址。

由于SSL取决于客户端请求的主机名,我希望能够根据服务器的访问方式生成并提供多个SSL证书。 例如,一个证书的CN将是“ https://192.168.1.100 ”,而另一个将是“ https://www.example.com ”,另一个可能是“ https://12.34.56.78 ”。

我认为这可能是通过复制这样的服务器块来实现的:

server { listen 443 ssl; server_name 192.168.1.10; ssl_certificate /etc/nginx/192.168.1.10.crt; ssl_certificate_key /etc/nginx/192.168.1.10.pem; location / { root /var/www/root; index index.html index.htm; } } server { listen 443 ssl; server_name www.example.com; ssl_certificate /etc/nginx/www.example.com.crt; ssl_certificate_key /etc/nginx/www.example.com.pem; location / { root /var/www/root; index index.html index.htm; } } ... 

问题是,我的服务器configuration中有几个 (即多于10个) location块,因为我正在同一台服务器上testing多个configuration,环境和Web应用程序。 大多数这些位置包括FastCGI传递和/或aliasrewrite指令。

复制所有的location块不仅枯燥乏味,而且如果我忘记更新每一个location块,可能会导致不一致。

此外,我还计划在将来以这种方式使用这个环境,以便实际使用子域,每个子域具有不同的位置块。 所以现在我们结束了这样的事情:

  • www.example.com使用location参数集1
  • 12.34.56.78使用location参数集1
  • test.example.com使用location参数集2
  • 192.168.1.100使用location参数集2

只要我不使用SSL,这是不是一个问题,因为服务器将始终服务于“默认”的服务器块。 但SSL似乎需要一个单独的server块为每个域名为了服务唯一证书…

理想情况下,我想要的是某种“命名分组/包含”,我可以在一个单独的部分中写入所有location块,然后它们包含在server块中。 我的“理想”解决scheme如果存在的话:

 config config1 { location / { root /var/www/root; index index.html index.htm; } location /testapp1 { include fastcgi_params; fastcgi_split_path_info ^(/testapp1)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_pass unix:/run/testapp1.sock; } } config config2 { location / { root /var/www/root2; index index.html index.htm; } location /testapp2 { include fastcgi_params; fastcgi_split_path_info ^(/testapp2)(.*)$; fastcgi_param PATH_INFO $fastcgi_path_info; fastcgi_pass unix:/run/testapp2.sock; } } server { listen 443 ssl; server_name 192.168.1.100; ssl_certificate /etc/nginx/192.168.1.10.crt; ssl_certificate_key /etc/nginx/192.168.1.10.pem; include config config1; } server { listen 443 ssl; server_name www.example.com; ssl_certificate /etc/nginx/www.example.com.crt; ssl_certificate_key /etc/nginx/www.example.com.pem; include config config1; } server { listen 443 ssl; server_name test.example.com; ssl_certificate /etc/nginx/test.example.com.crt; ssl_certificate_key /etc/nginx/test.example.com.pem; include config config2; } ... 

由于SSL指令只能在serverhttp上下文中使用,因此无法为每个证书写入一个块。

但是,您可以将总是相同的内容(?)移动到不同的文件中。 这个文件将被include在每个块中。 所以这几乎就像你设想的解决scheme。

您也可能想要考虑使用对多个主题有效的证书,使用“主题备用名称”字段。 但是,在商业上,这是相当昂贵的。

我将使用使用主题替代名称的多域名证书。 然后,您只需要一个服务器块为您的各种主机名。 我经常创build这些通配符主机规范(* .domain.com),除了我需要的任何其他域。 这使得很容易使用相同的证书www.domain.com,dev1.domain.com,staging.domain.com等

而为了不同的目的,这个问题的过程是:

如何为IIS网站创build具有主题备用名称(SAN)的自签名SSL证书