nginx上配置https的條件:html
一、SSL證書和服務器私鑰文件linux
二、nginx支持SSL模塊nginx
1、獲取SSL證書web
網上有提供權威認證的SSL證書的網站,但多數是收費的,並且不便宜。在正式的生產環境中,強烈不建議使用免費的SSL證書,但咱們此次只是用於測試,因此決定使用下免費的SSL證書。算法
下面介紹幾個免費的SSL證書提供商:(如下內容非原創)瀏覽器
須要兩個文件:服務器
SSL證書:.crt測試
私鑰文件:.key網站
2、在nginx上配置支持SSL加密
一、檢查nginx是否安裝了SSL模塊
# nginx -V
configure arguments: --prefix=/usr/local/nginx --with-http_sysguard_module --with-http_realip_module
這個nginx是編譯安裝的,編譯時沒有指定安裝SSLL模塊,須要從新編譯,加上 --with-http_ssl_module
另外,nginx須要openssl提供ssl支持,也須要檢查是否已經安裝了openssl。
nginx重編譯的步驟:
1)找到安裝nginx的源碼根目錄,並查看原編譯參數。
# cd /root/tengine-1.5.2
# nginx -V
configure arguments: --prefix=/usr/local/nginx --with-http_sysguard_module --with-http_realip_module
2)從新編譯nginx
#./configure --prefix=/usr/local/nginx --with-http_sysguard_module --with-http_realip_module --with-http_ssl_module
# make
注意:千萬不要 make install ,不然就會覆蓋安裝了。
make 後,在源碼根目錄的objs目錄中多了一個nginx程序,就是新編譯的nginx程序。
3)備份舊的nginx程序
# cp -a /usr/local/nginx/sbin/nginx /usr/local/nginx/sbin/nginx.bak
4)用新的nginx程序覆蓋舊的
# cp -a objs/nginx /usr/local/nginx/sbin/nginx
5)測試新nginx程序是否正確
# nginx -t the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok configuration file /usr/local/nginx/conf/nginx.conf test is successful
6)平滑升級
# nginx -s reload
二、配置nginx server支持ssl
1)上傳SSL證書文件和私鑰文件到服務器
這裏咱們統一放到:/usr/local/nginx/conf/ssl 目錄
# ls /usr/local/nginx/conf/ssl/ ca.crt private.key
2)在特定的server中配置SSL支持
listen 443; server_name xxx.xxx.com; # 開啓ssl支持
ssl on;
# SSL 證書 ssl_certificate /usr/local/nginx/conf/ssl/ca.crt;
# 私鑰文件 ssl_certificate_key /usr/local/nginx/conf/ssl/private.key;
3)重啓nginx服務
# nginx -t
# nginx -s reload
4) 開放防火牆的 443 端口
5)web訪問,檢查https是否能用
常見的異常:
[emerg] 21482#0: PEM_read_bio_X509_AUX("/usr/local/nginx/conf/ssl/ca.crt") failed (SSL: error:0906D06C:PEM routines:PEM_read_bio:no start line:Expecting: TRUSTED CERTIFICATE)
[emerg] SSL_CTX_use_PrivateKey_file("/usr/local/nginx/conf/ssl/private.key") failed (SSL: error:0906D06C:PEM routines:PEM_read_bio:no start line:Expecting: ANY PRIVATE KEY error:140B0009:SSL routines:SSL_CTX_use_PrivateKey_file:PEM lib)