首先配置nginx配置文件/etc/nginx/nginx.conf,啓用ssl功能配置以下:
html
[root@lamp ~]# vim /etc/nginx/nginx.confnginx
上圖中的證書路徑都是使用了相對路徑,爲了不出現錯誤,把相對路徑修改成絕對路徑以下圖:vim
編輯證書openssl.cnf,確保證書路徑正確,而後保存退出:服務器
[root@lamp ~]# vim /etc/pki/tls/openssl.cnf ide
確認CA路徑下證書相關目錄是否都有:測試
[root@lamp ~]# cd /etc/pki/CAspa
[root@lamp CA]# ls #下面四個目錄必須都存在(nginx服務器默認已創建)rest
certs crl newcerts privateserver
[root@lamp CA]# ls private/ #查看是否有私鑰文件htm
[root@lamp CA]# (umask 077; openssl genrsa 2048 > private/cakey.pem) #生成一個私鑰證書
Generating RSA private key, 2048 bit long modulus
.............+++
............+++
e is 65537 (0x10001)
[root@lamp CA]# openssl req -new -x509 -key private/cakey.pem -out cacert.pem #針對cakey.pem該私鑰證書生成一個自簽證書。相關內容輸入以下
[root@lamp CA]# ls
cacert.pem certs crl newcerts private
[root@lamp CA]# touch serial
[root@lamp CA]# echo 01 > serial
[root@lamp CA]# touch index.txt
[root@lamp CA]# cd /etc/nginx
[root@lamp nginx]# mkdir ssl
[root@lamp nginx]# cd ssl
[root@lamp ssl]# (umask 077;openssl genrsa 1024 > nginx.key) #生成一個私鑰
Generating RSA private key, 1024 bit long modulus
...........................++++++
..++++++
e is 65537 (0x10001)
[root@lamp ssl]# openssl req -new -key nginx.key -out nginx.csr
[root@lamp ssl] openssl ca -in nginx.csr -out nginx.crt -days 3650
[root@lamp ssl]# service nginx restart
[root@lamp ssl]# cd /usr/html/ #進入網頁保存目錄,新建ssl
[root@lamp html]# ls
50x.html index.html test test1 test2 test3
[root@lamp html]# mkdir ssl
[root@lamp html]# ls
50x.html index.html ssl test test1 test2 test3
[root@lamp html]# cd ssl
[root@lamp ssl]# vim index.html #編輯ssl主頁
<h1> SSL 10.109.134.252 SSL </h1>
[root@lamp ssl]# service nginx restart
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
Stopping nginx: [ OK ]
Starting nginx: [ OK ]
*網頁存放路徑爲/usr/html/ssl,因此/etc/nginx/nginx.conf配置文件中SSL服務中的location中的
root路徑爲/usr/html/ssl
而後經過客戶端訪問結果以下:
nginx域名訪問功能,只要在nginx.conf配置文件中修改server字段,以下:
增長nginx虛擬主機功能,配置nginx.conf文件增長server字段
重啓nginx服務,並在訪問的主機host裏面增長如下兩行域名解析:
10.109.134.252 www.c.com
10.109.134.252 www.a.com
測試結果以下: