1、爲nginx配置https並自簽名證書
一、製做CA證書
ca.key CA私鑰:
openssl genrsa -des3 -out ca.key 2048
製做解密後的CA私鑰(通常無此必要):
openssl rsa -in ca.key -out ca_decrypted.key
ca.crt CA根證書(公鑰):
openssl req -new -x509 -days 7305 -key ca.key -out ca.crt
二、製做生成網站的證書並用CA簽名認證
在這裏,假設網站域名爲blog.creke.net
生成blog.creke.net證書私鑰:
openssl genrsa -des3 -out blog.creke.net.pem 2048
製做解密後的blog.creke.net證書私鑰(注意:nginx配置中,nginx支持的是解密後的格式):
openssl rsa -in blog.creke.net.pem -out blog.creke.net.key
生成簽名請求:
openssl req -new -key blog.creke.net.pem -out blog.creke.net.csr
在common name中填入網站域名,如blog.creke.net便可生成該站點的證書,同時*也可使用泛域名如.creke.net來生成全部二級域名可用的網站證書。
用CA進行簽名:
openssl ca -policy policy_anything -days 1460 -cert ca.crt -keyfile ca.key -in blog.creke.net.csr -out blog.creke.net.crt
其中,policy參數容許簽名的CA和網站證書能夠有不一樣的國家、地名等信息,days參數則是簽名時限。
若是在執行簽名命令時,出現「I am unable to access the ../../CA/newcerts directory」
修改/etc/pki/tls/openssl.cnf中「dir = ./CA」
而後:
mkdir -p CA/newcerts
touch CA/index.txt
touch CA/serial
echo "01" > CA/serial
再從新執行簽名命令。
最後,把ca.crt的內容粘貼到blog.creke.net.crt後面。這個比較重要!**由於不這樣作,可能會有某些瀏覽器不支持。
好了,如今https須要用到的網站私鑰blog.creke.net.key和網站證書blog.creke.net.crt都準備完畢。接下來開始配置服務端。php
2、配置nginx
server {
listen 443 ssl;
server_name blog.creke.net;
keepalive_timeout 70;nginx
ssl_certificate blog.creke.net.crt; //網站證書存放的路徑 ssl_certificate_key blog.creke.net.key; //網站私鑰存放的路徑 ssl_session_cache shared:SSL:10m; ssl_session_timeout 5m; ssl_protocols TLSv1 TLSv1.1 TLSv1.2 TLSv1.3; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; ...
}瀏覽器
3、檢測配置和從新加載nginx配置
檢測配置:
nginx -t
從新加載:
nginx -s reloadsession
4、QA
有時候,會發現,在phpMyAdmin等程序登入後會錯誤地跳轉http的問題。解決方法是定位至「location ~ .*.(php|php5)?${}」在include fcgi.conf;或者在fastcgi_param配置後面加上:
fastcgi_param HTTPS on;
fastcgi_param HTTP_SCHEME https;
在這裏是nginx官方的關於https的文檔,能夠做爲參考。ide