nginx 同一个IP上配置多个HTTPS主机

默北 Nginx ttlsa教程系列 web应用 原创教程2392,47427字数 1934阅读6分26秒阅读模式

最近公司域名更变,同时,又要新旧域名同时运行。 那么,对于https的域名在同一个IP上如何同时存在多个虚拟主机呢?遂,查看了下nginx手册,有这么一段内容,如下:

如果在同一个IP上配置多个HTTPS主机,会出现一个很普遍的问题:文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

server {
    listen          443;
    server_name     www.example.com;
    ssl             on;
    ssl_certificate www.example.com.crt;
    ...
}

server {
    listen          443;
    server_name     www.example.org;
    ssl             on;
    ssl_certificate www.example.org.crt;
    ...
}

使用上面的配置,不论浏览器请求哪个主机,都只会收到默认主机www.example.com的证书。这是由SSL协议本身的行为引起的——先建立SSL连接,再发送HTTP请求,所以nginx建立SSL连接时不知道所请求主机的名字,因此,它只会返回默认主机的证书。文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

最古老的也是最稳定的解决方法就是每个HTTPS主机使用不同的IP地址:文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

server {
    listen          192.168.1.1:443;
    server_name     www.example.com;
    ssl             on;
    ssl_certificate www.example.com.crt;
    ...
}

server {
    listen          192.168.1.2:443;
    server_name     www.example.org;
    ssl             on;
    ssl_certificate www.example.org.crt;
    ...
}

那么,在同一个IP上,如何配置多个HTTPS主机呢?文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

nginx支持TLS协议的SNI扩展(Server Name Indication,简单地说这个扩展使得在同一个IP上可以以不同的证书serv不同的域名)。不过,SNI扩展还必须有客户端的支持,另外本地的OpenSSL必须支持它。文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

如果启用了SSL支持,nginx便会自动识别OpenSSL并启用SNI。是否启用SNI支持,是在编译时由当时的 ssl.h 决定的(SSL_CTRL_SET_TLSEXT_HOSTNAME),如果编译时使用的OpenSSL库支持SNI,则目标系统的OpenSSL库只要支持它就可以正常使用SNI了。文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

nginx在默认情况下是TLS SNI support disabled。文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

启用方法:文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

需要重新编译nginx并启用TLS。步骤如下:文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

# wget http://www.openssl.org/source/openssl-1.0.1e.tar.gz
# tar zxvf openssl-1.0.1e.tar.gz 
# ./configure --prefix=/usr/local/nginx --with-http_ssl_module \
--with-openssl=./openssl-1.0.1e \
--with-openssl-opt="enable-tlsext" 
# make
# make install

查看是否启用:文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

# /usr/local/nginx/sbin/nginx -V
TLS SNI support enabled

这样就可以在 同一个IP上配置多个HTTPS主机了。文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

实例如下:文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

server  {
        listen 443;
        server_name   www.ttlsa.com;
        index index.html index.htm index.php;
        root  /data/wwwroot/www.ttlsa.com/webroot;
        ssl on;
        ssl_certificate "/usr/local/nginx/conf/ssl/www.ttlsa.com.public.cer";
        ssl_certificate_key "/usr/local/nginx/conf/ssl/www.ttlsa.com.private.key";   
		......
}		

server  {
        listen 443;
        server_name   www.heytool.com;
        index index.html index.htm index.php;
        root  /data/wwwroot/www.heytool.com/webroot;
        ssl on;
        ssl_certificate "/usr/local/nginx/conf/ssl/www.heytool.com.public.cer";
        ssl_certificate_key "/usr/local/nginx/conf/ssl/www.heytool.com.private.key";   
		......
}

这样访问每个虚拟主机都正常。文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

转载请注明来自运维生存时间: https://www.ttlsa.com/html/4288.html文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/ 文章源自运维生存时间-https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/

weinxin
我的微信
微信公众号
扫一扫关注运维生存时间公众号,获取最新技术文章~
默北
  • 本文由 发表于 04/01/2014 09:30:01
  • 转载请务必保留本文链接:https://www.ttlsa.com/web/multiple-https-host-nginx-with-a-ip-configuration/
  • https
  • nginx
  • openssl
  • SNI
  • ssl
  • ssl_certificate
  • ssl_certificate_key
  • TLS
  • 虚拟主机
评论  23  访客  17
    • 彗星骑士
      彗星骑士 0

      大佬我想问一下,如果我想同IP多个网站同时支持HTTP/2和SSL的话应该怎么写Nginx的conf。

      • LeeJon
        LeeJon 0

        碰到一个问题望指点。我打算是这样:http访问国内主机,https访问国外主机(主要机器),是不是向上面一样{listen 国内IP:80;和国外IP:443}这样来设置?因为没搞清楚所以不敢贸然下手,貌似有点复杂啊。我访问过别人的加个s就是另一个完全不同的博客,谢谢。 :mrgreen:

          • 匿名
            匿名 9

            @ LeeJon http和https端口不同,ip是相同的。

          • 王鹏飞_Cloud
            王鹏飞_Cloud 1

            博主你好,我把我VPS的LNMP上配置openssl后,使用https只能访问html页面,PHP页面访问后直接把PHP文件下载下来了。http访问正常,这是怎么回事?谢谢~~

            • Aceslup
              Aceslup 9

              不知道–with-openssl-opt="enable-tlsext"这个作用是什么。

                • 小北
                  小北 9

                  @ Aceslup tlsext启用这个扩展功能

                    • Aceslup
                      Aceslup 9

                      @ 小北 tlsext是一种加密算法么?测试不加这个,nginx也能使用ssl呢。

                        • 运维生存时间
                          运维生存时间 7

                          @ Aceslup OpenSSL通过“–enable-tlsext”配置选项加入SNI支持,enable是启用tls ext,便是启用tls扩展。

                            • 默北
                              默北

                              @ 运维生存时间 也就是基于域名的ssl虚拟主机

                              • Aceslup
                                Aceslup 9

                                @ 默北 文章说出现有TLS SNI support enabled,就能支持。我没加“–enable-tlsext”,也出现了这句话。是否也算是支持ssl的虚拟主机啦?

                                • 默北
                                  默北

                                  @ Aceslup nginx -V 出现TLS SNI support enabled 就是支持的。 默认是TLS SNI support disable

                            • 匿名
                              匿名 9

                              @ Aceslup 。。。哈

                            • 默北
                              默北

                              Client Support

                              Most current web browsers and command line user agents support SNI.

                              Desktop

                              Chrome 5 or higher
                              Chrome 6 or higher on Windows XP
                              Firefox 2 or higher
                              Internet Explorer 7 or higher, running on Windows Vista/Server 2008 or higher
                              Internet Explorer on Windows XP does not support SNI regardless of IE version
                              Konqueror 4.7 or higher
                              Opera 8 or higher (may require TLS 1.1 enabled to function)
                              Mobile

                              Android Browser on 3.0 Honeycomb or higher
                              iOS Safari on iOS 4 or higher
                              Windows Phone 7 or higher
                              Command Line

                              cURL 7.18.1 or higher
                              wget 1.14 or higher (Distributions may have backported a patch for SNI support)
                              No Support

                              BlackBerry Browser
                              Internet Explorer (any version) on Windows XP

                              • 默北
                                默北

                                SNI is available only on IIS 8 and may be future versions. It is not available on any of the previous versions of IIS like IIS 7.x, IIS6 etc.

                                http://blogs.msdn.com/b/kaushal/archive/2012/09/04/server-name-indication-sni-in-iis-8-windows-server-2012.aspx

                              评论已关闭!