Linux命令:nginx啓用SSL功能設置及虛擬主機


首先配置nginx配置文件/etc/nginx/nginx.conf,啓用ssl功能配置如下:

[root@lamp ~]# vim /etc/nginx/nginx.conf

wKiom1mFMmfyn-i8AAS1umLdSKM106.jpg

上圖中的證書路徑都是使用了相對路徑,爲了避免出現錯誤,把相對路徑修改爲絕對路徑如下圖:

wKioL1mFM3-TCIUBAADDJ6dFPVg197.jpg

編輯證書openssl.cnf,確保證書路徑正確,然後保存退出:

[root@lamp ~]# vim /etc/pki/tls/openssl.cnf 

wKiom1mFNBfAHI77AADpYCi2xmc865.jpg

確認CA路徑下證書相關目錄是否都有:

[root@lamp ~]# cd /etc/pki/CA

[root@lamp CA]# ls  #下面四個目錄必須都存在(nginx服務器默認已建立)

certs  crl  newcerts  private

[root@lamp CA]# ls private/  #查看是否有私鑰文件

[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該私鑰證書生成一個自簽證書。相關內容輸入如下

wKiom1mFP-mzQUh6AASu4HKzWpw169.jpg

[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 

wKiom1mFX4vg30JsAAWLL7DfjFo887.jpg

[root@lamp ssl] openssl ca -in nginx.csr -out nginx.crt -days 3650

wKioL1mFYHTQalLeAATbMrPRi-k304.jpg

[root@lamp ssl]# service nginx restart

wKioL1mFYqeyA_jkAASqL5Dv36o621.jpg

[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

然後通過客戶端訪問結果如下:

wKioL1mFasyQ9fkTAAD3NhlWPOU865.jpg

wKioL1mFas3QICbOAACMZG9THBs200.jpg

nginx域名訪問功能,只要在nginx.conf配置文件中修改server字段,如下:


wKiom1mFgi7wxw41AAKzCD9qhPg473.jpg

增加nginx虛擬主機功能,配置nginx.conf文件增加server字段

wKiom1mFgyXT7TnhAAEQhdZRbYM833.jpg

重啓nginx服務,並在訪問的主機host裏面增加以下兩行域名解析:

10.109.134.252     www.c.com

10.109.134.252     www.a.com

測試結果如下:

wKiom1mH_cOjsHqYAABlIZPnIlQ624.jpg

wKiom1mH_0iiN5usAAC2mZ6X8Yw525.jpg







發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章