nginx開啓ssl配置https證書和密鑰過程

起初是在部署系統時,用掃描漏洞工具掃描系統,發現網站訪問不安全,要求使用https安全認證訪問web,而nginx支持https技術,所以取巧就在nginx配置了個https;在踩了一大堆的教程坑後,終於配置成功,然後記錄下來,按照以下的教程,可以配置出https需要的網站證書認證。
本教程是在Centos7上配置,其他版本的linux改一下對應的命令即可。供參考

配置完成訪問瀏覽器後,網站前面會出現紅色的叉,這是因爲在網絡服務器上找不到對應的證書廠商,不妨礙使用。

Nginx 開啓SSL 配置證書和私鑰,需要安裝openssl
這裏以nginx1.16.1爲例

獲取安裝包
[root@~]curl -O http://nginx.org/download/nginx-1.16.1.tar.gz
或者 wget http://nginx.org/download/nginx-1.16.1.tar.gz

解壓
[root@~] tar -zxvf nginx-1.16.1.tar.gz

編譯模塊
[root@~] cd nginx-1.16.1
./configure --prefix=/usr/local/nginx --with-http_stub_status_module --with-https_ssl_module

如果之前已經安裝nginx的,這裏就不再make install,否則會覆蓋掉之前的安裝和配置
[root@ nginx-1.16.1]make

備份原先的啓動文件
[root@~]cp /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak

查看模塊是否加載
[root@~]/usr/local/nginx/sbin/nginx -V
nginx version: nginx/1.16.1
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)
built with OpenSSL 1.0.2k-fips 26 Jan 2017
TLS SNI support enabled
configure arguments: --prefix=/usr/local/nginx --with-http_ssl_module # 模塊已加載

加載ssl模塊後,會在nginx.conf加上配置文件HTTPS SERVER 後面的ssl信息,
注意:一般生成的目錄,應該放在nginx/conf/ssl目錄,可以自定義
生成證書和密鑰 -des3: CBC模式的DES加密 以下示例生成一個1024位的RSA私鑰
[root@~]openssl genrsa -des3 -out server.key 1024

輸入密碼,2次
kpy@hw
Generating RSA private key, 1024 bit long modulus
…++++++
…++++++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:

創建服務器證書的申請文件 server.csr
[root@~]openssl req -new -key server.key -out server.csr
Enter pass phrase for server.key: # 輸入上面的密碼
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter ‘.’, the field will be left blank.
填寫下面的信息
Country Name (2 letter code) [XX]:CN # 國家縮寫
State or Province Name (full name) []:GuangDong #省份
Locality Name (eg, city) [Default City]:GuangZhou # 市
Organization Name (eg, company) [Default Company Ltd]:lw666.cn # 公司名
Organizational Unit Name (eg, section) []: # 組織名,可以不填
Common Name (eg, your name or your server’s hostname) []: #公共名,可以不填
Email Address []:[email protected] # 郵箱地址,可以不填
Please enter the following ‘extra’ attributes
to be sent with your certificate request
A challenge password []: # 加強的密碼,可以不填
An optional company name []: # 可以不填

備份文件,跳過證書驗證密碼,生成server.crt文件
[root@~]cp server.key server.key.org
[root@~]openssl rsa -in server.key.org -out server.key
Enter pass phrase for server.key.org: # 輸入上面的密碼
writing RSA key

生成證書, 證書有效天數(如果輸入9999表示永久) 簽名,開啓雙向認證
[root@~]openssl x509 -req -days 180 -in server.csr -signkey server.key -out server.crt
Signature okbr/>subject=/C=CN/ST=GuangDong/L=GuangZhou/O=lwops.cn/[email protected]
Getting Private key

vi nginx.conf,註釋掉HTTPS SERVER,單個server,直接把listen到最後一行ssl替換原來的listen 80,然後測試配置文件
參考官方配置資料:
http://nginx.org/en/docs/http/configuring_https_servers.html#single_http_https_server

單個server可以80和433共存,但80的還是http

HTTPS server

server {
listen 443 ssl;
server_name localhost;

ssl on; nginx1.15版本之前需要加,之後的不用加

證書路徑和密鑰路徑

技術交流歡迎加入Q羣:177428068

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