nginx ssl

爲nginx配置https並自簽名證書

一、把證書準備好。

步驟與使用OpenSSL自簽發服務器https證書所述大同小異。在這裏再重複一次。

1、製作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

2、製作生成網站的證書並用CA簽名認證

在這裏,假設網站域名爲blog.creke.net

生成blog.creke.net證書私鑰:

openssl genrsa -des3 -out blog.creke.net.pem 1024

製作解密後的blog.creke.net證書私鑰:

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都準備完畢。接下來開始配置服務端。

二、配置nginx

新開一個虛擬主機,並在server{}段中設置:

listen 443;

ssl on;

ssl_certificate /path/to/blog.creke.net.crt;

ssl_certificate_key /path/to/blog.creke.net.key;

其中的路徑是剛剛生成的網站證書的路徑。

然後使用一下命令檢測配置和重新加載nginx:

檢測配置:

nginx -t

重新加載:

nginx -s reload

三、優化nginx配置

1、優化nginx性能

在http{}中加入:

ssl_session_cache shared:SSL:10m;

ssl_session_timeout 10m;

據官方文檔所述,cache中的1m可以存放4000個session。

在配置https的虛擬主機server{}中加入:

keepalive_timeout 70;

2、有時候,會發現,在phpMyAdmin等程序登入後會錯誤地跳轉http的問題。解決方法是定位至“location ~ .*\.(php|php5)?${}”在include fcgi.conf;或者在fastcgi_param配置後面加上:

fastcgi_param HTTPS on;

fastcgi_param HTTP_SCHEME https;

這裏是nginx官方的關於https的文檔,可以作爲參考。


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