目前公司有2个域名,其中这次涉及到3个子域名需要更改为HTTPS传输,分别为:
passport.abc.com
www.test.com
admin.test.com
那么就涉及到购买ssl证书的问题,由于价格问题使用3个不同的证书(每个域名一个)。
由于实验环境,我们就手动生成3个ssl证书
建立目录,及进入目录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<code>[root@gz122haproxy95 ~]</code><code># mkdir ~/keys</code>
<code>[root@gz122haproxy95 keys]</code><code># cd ~/keys</code>
<code>[root@gz122haproxy95 keys]</code><code># openssl genrsa -out passport.abc.com.key 2048</code>
<code>[root@gz122haproxy95 keys]</code><code># openssl req -new -key passport.abc.com.key -out passport.abc.com.csr</code>
<code>You are about to be asked to enter information that will be incorporated</code>
<code>into your certificate request.</code>
<code>What you are about to enter is what is called a Distinguished Name or a DN.</code>
<code>There are quite a few fields but you can leave some blank</code>
<code>For some fields there will be a default value,</code>
<code>If you enter </code><code>'.'</code><code>, the field will be left blank.</code>
<code>-----</code>
<code>Country Name (2 letter code) [GB]:CN </code><code>#国家</code>
<code>State or Province Name (full name) [Berkshire]:GuangDong </code><code>#省份</code>
<code>Locality Name (eg, city) [Newbury]:ShenZhen </code><code>#城市</code>
<code>Organization Name (eg, company) [My Company Ltd]:Test.Inc </code><code>#公司名称</code>
<code>Organizational Unit Name (eg, section) []:passport.abc.com </code><code>#组织名称</code>
<code>Common Name (eg, your name or your server's </code><code>hostname</code><code>) []:passport.abc.com </code><code>#域名</code>
<code>Email Address []:[email protected]</code>
<code>Please enter the following </code><code>'extra'</code> <code>attributes</code>
<code>to be sent with your certificate request</code>
<code>A challenge password []:</code>
<code>An optional company name []:</code>
<code>[root@gz122haproxy95 keys]</code><code># openssl x509 -req -days 3650 -in passport.abc.com.csr -signkey passport.abc.com.key -out passport.abc.com.crt</code>
按照以上方法,把名称替换掉再制作2份,最后的结果就是
<code>[root@gz122haproxy95 keys]</code><code># ls -l</code>
<code>total 36</code>
<code>-rw-r--r-- 1 root root 1354 Dec 4 16:54 admin.</code><code>test</code><code>.com.crt</code>
<code>-rw-r--r-- 1 root root 1050 Dec 4 16:54 admin.</code><code>test</code><code>.com.csr</code>
<code>-rw-r--r-- 1 root root 1675 Dec 4 16:52 admin.</code><code>test</code><code>.com.key</code>
<code>-rw-r--r-- 1 root root 1354 Dec 4 16:48 passport.abc.com.crt</code>
<code>-rw-r--r-- 1 root root 1078 Dec 4 16:44 passport.abc.com.csr</code>
<code>-rw-r--r-- 1 root root 1675 Dec 4 16:41 passport.abc.com.key</code>
<code>-rw-r--r-- 1 root root 1354 Dec 4 16:52 www.</code><code>test</code><code>.com.crt</code>
<code>-rw-r--r-- 1 root root 1062 Dec 4 16:52 www.</code><code>test</code><code>.com.csr</code>
<code>-rw-r--r-- 1 root root 1679 Dec 4 16:51 www.</code><code>test</code><code>.com.key</code>
现在就是Nginx和OpenSSL的安装与配置(这里注意,一般情况下一个IP只支持一个SSL证书,那么我们现在要在一个IP上实现多个SSL证书,就必须让Nginx支持TLS SNI,由于默认的OpenSSL是没有打开TLS SNI的)
<code>[root@gz122haproxy95 ~]</code><code># wget </code>
<code>[root@gz122haproxy95 ~]</code><code># tar zxf openssl-0.9.8zh.tar.gz </code>
<code>[root@gz122haproxy95 ~]</code><code># wget http://nginx.org/download/nginx-1.8.0.tar.gz</code>
<code>[root@gz122haproxy95 ~]</code><code># tar zxf nginx-1.8.0.tar.gz </code>
<code>[root@gz122haproxy95 ~]</code><code># cd nginx-1.8.0</code>
<code>[root@gz122haproxy95 nginx-1.8.0]</code><code># ./configure --prefix=/usr/local/nginx1.8.0 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=../openssl-0.9.8zh </code>
<code>[root@gz122haproxy95 nginx-1.8.0]</code><code># make && make install</code>
<code>#上面只需要解压openssl即可,然后在nginx的配置参数中添加--with-openssl=DIR指定路径即可,另外由于openssl需要编译,所以时间会较长。</code>
在编译安装nginx的时候可能会出现pcre库没找到或zlib没找到,在CentOS下可以使用
<code>yum -y </code><code>install</code> <code>pcre-devel zlib-devel</code>
在安装编译好Nginx后,执行
<code>[root@gz122haproxy95 nginx-1.8.0]</code><code># /usr/local/nginx1.8.0/sbin/nginx -V</code>
<code>nginx version: nginx</code><code>/1</code><code>.8.0</code>
<code>built by gcc 4.1.2 20080704 (Red Hat 4.1.2-55)</code>
<code>built with OpenSSL 0.9.8zh 3 Dec 2015</code>
<code>TLS SNI support enabled </code><code>#可以看到TLS SNI support打开了</code>
<code>configure arguments: --prefix=</code><code>/usr/local/nginx1</code><code>.8.0 --user=www --group=www --with-http_stub_status_module --with-http_ssl_module --with-http_gzip_static_module --with-openssl=..</code><code>/openssl-0</code><code>.9.8zh</code>
然后配置nginx
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<code>upstream passport.abc.com {</code>
<code> </code><code>server 192.168.20.87:80;</code>
<code> </code><code>server 192.168.20.88:80;</code>
<code> </code>
<code>}</code>
<code> </code><code># HTTPS server</code>
<code> </code><code>#</code>
<code> </code><code>server {</code>
<code> </code><code>listen 443 ssl;</code>
<code> </code><code>server_name passport.abc.com;</code>
<code> </code><code>ssl_certificate </code><code>/root/keys/passport</code><code>.abc.com.crt;</code>
<code> </code><code>ssl_certificate_key </code><code>/root/keys/passport</code><code>.abc.com.key;</code>
<code> </code><code>ssl_session_cache shared:SSL:1m;</code>
<code> </code><code>ssl_session_timeout 5m;</code>
<code> </code><code>ssl_ciphers HIGH:!aNULL:!MD5;</code>
<code> </code><code>ssl_prefer_server_ciphers on;</code>
<code> </code><code>location / {</code>
<code> </code><code>proxy_pass http:</code><code>//passport</code><code>.abc.com;</code>
<code> </code><code>}</code>
<code> </code><code>}</code>
<code> </code><code>upstream www.</code><code>test</code><code>.com {</code>
<code> </code><code>server 192.168.20.98:80;</code>
<code> </code><code>server 192.168.20.99:80;</code>
<code> </code><code>server_name www.</code><code>test</code><code>.com;</code>
<code> </code><code>ssl_certificate </code><code>/root/keys/www</code><code>.</code><code>test</code><code>.com.crt;</code>
<code> </code><code>ssl_certificate_key </code><code>/root/keys/www</code><code>.</code><code>test</code><code>.com.key;</code>
<code> </code><code>proxy_pass http:</code><code>//www</code><code>.</code><code>test</code><code>.com;</code>
通过以上即可实现nginx的HTTPS的多域名反向代理
本文转自 rong341233 51CTO博客,原文链接:http://blog.51cto.com/fengwan/1719708