標籤:gns 計算 sha cti org client 電腦 out res
Nginx上部署HTTPS依賴OpenSSL庫和包含檔案,即須先安裝好libssl-dev,且ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/,然後在編譯配置Nginx時要指定--with-http_ssl_module。另外,要在Shell中運行openssl命令,還要安裝openssl包,本人用的OpenSSL-1.0.2g。註:本文採用Ubuntu 16.04上的操作執行個體。
展示了數位憑證(HTTPS中使用的由CA簽名的密鑰憑證)的簽名和驗證原理(流程):
- 自簽發認證:產生認證可以在其他機器上去執行,然後再將所產生server.crt和server.key複製一份至Nginx的/usr/local/nginx/conf下即可
$ cd /usr/local/nginx/conf$ openssl genrsa -des3 -out server.key 1024 #建議:2048$ openssl req -new -key server.key -out server.csr #認證簽章要求(CSR)$ cp server.key server.key.org$ openssl rsa -in server.key.org -out server.key$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
- 修改設定檔nginx.conf:為減少CPU負載,建議只運行一個背景工作處理序,並且開啟keep-alive。另外,version 0.6.7以上的Nginx ssl_certificate和ssl_certificate_key的預設關聯目錄為nginx.conf所在的目錄,預設檔案名稱都為cert.pem
worker_processes 1;server { server_name YOUR_DOMAINNAME_HERE; listen 443 ssl; listen 80; if ($scheme = http) { rewrite ^(.*)$ https://$server_name$1 permanent; } ssl_certificate server.crt; ssl_certificate_key server.key; keepalive_timeout 70;}
- 重啟Nginx:HTTPS在Nginx上的部署至此已近完畢,然後就可以通過https://YOUR_DOMAINNAME_HERE來訪問了。由於本例中採用自簽發認證(不同於CA自簽名的root認證),在Chrome下將看到警告資訊,表明該認證不受信任。瀏覽器在預設情況下內建了一些CA機構的認證,使得這些機構頒發的認證受到信任。
- 私密金鑰保護:私密金鑰是重要的財產,儘可能限制能接觸到私密金鑰的人
- 在一台可信的電腦上產生私密金鑰和CSR(Certificate Signing Requests)。有一些CA會為你產生密鑰和CSR,但這樣做明顯不妥
- 受密碼保護的密鑰可以阻止在備份系統中被截獲
- 在發現被截獲後,撤回老的認證,產生新的密鑰和認證
- 每年更新認證,總是使用最新的私密金鑰
- 部署憑證鏈結:憑證鏈結(Certificate Chain)包括信任錨(CA 憑證)和簽署憑證,是由一系列 CA 憑證發出的認證序列,最終以根 CA 憑證結束;網頁瀏覽器已預先配置了一組瀏覽器自動信任的根 CA 憑證,來自其他認證授權機構的所有認證都必須附帶憑證鏈結,以檢驗這些認證的有效性。在很多部署情境中,單一的伺服器憑證顯得不足,而多個認證則需要建立一個信任鏈結。一個常見的問題是正確的配置了伺服器憑證但卻搞忘了包含其他所需要的認證。此外,雖然其他認證通常有很長的有效期間,但她們也會到期,如果她們到期就會影響整個鏈條。你的CA應該提供所有額外需要的認證。一個無效憑證鏈結會導致伺服器憑證失效和用戶端瀏覽器警示告,這個問題有時候不是那麼容易被檢測到,因為有些瀏覽器可以自己重構一個完整的信任鏈結而有些則不行。關於Nginx上部署憑證鏈結:
if you have a chain certificate file (sometimes called an intermediate certificate)you don‘t specify it separately like you do in Apache. Instead you need to add the information from the chain cert to the end of your main certificate file. This can be done by typing "cat chain.crt >> mysite.com.crt" on the command line. Once that is done youwon‘t use the chain cert file for anything else, you just point Nginx to the main certificate file
展示了憑證鏈結的工作原理:
- Nginx上SSL配置指令說明:下邊只列舉了部分,更多配置項可參考 http://www.nginx.cn/doc/optional/ssl.html。
- ssl:開啟HTTPS
syntax:ssl [on|off]
default:ssl off
context:main, server
- ssl_certificate:認證檔案,預設認證和密鑰都位於cert.pem中,該檔案還可以包含其他認證。自version 0.6.7起,ssl_certificate的預設關聯目錄為nginx.conf所在的目錄。
syntax:ssl_certificate file
default:ssl_certificate cert.pem
context:main, server
- ssl_certificate_key:認證密鑰檔案,預設密鑰位於cert.pem中。自version 0.6.7起,ssl_certificate_key的預設關聯目錄為nginx.conf所在的目錄。
syntax:ssl_certificate_key file
default:ssl_certificate_key cert.pem
context:main, server
- ssl_client_certificate:Indicates file with certificates CA in PEM format, utilized for checking the client certificates.
syntax:ssl_client_certificate file
default:none
context:main, server
- ssl_dhparam:Indicates file with Diffie-Hellman parameters in PEM format, utilized for negotiating TLS session keys.
syntax: ssl_dhparam file
default: none
context: main, server
- ssl_ciphers:Directive describes the permitted ciphers. Ciphers are assigned in the formats supported by OpenSSL.
syntax: ssl_ciphers file
default: ssl_ciphers ALL:!ADH:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP
context: main, server
ssl_ciphers ALL:!ADH:!EXPORT56:RC4+RSA:+HIGH:+MEDIUM:+LOW:+SSLv2:+EXP;
Complete list can be looked with the following command:
openssl ciphers
- ssl_prefer_server_ciphers:Requires protocols SSLv3 and TLSv1 server ciphers be preferred over the client‘s ciphers.
syntax: ssl_prefer_server_ciphers [on|off]
default: ssl_prefer_server_ciphers off
context: main, server
- ssl_protocols:Directive enables the protocols indicated. TLS v1.0以上的版本是比較安全的,最好是棄用SSLv3以下的版本,SSLv2堅決不用
syntax: ssl_protocols [SSLv2] [SSLv3] [TLSv1]
default: ssl_protocols SSLv2 SSLv3 TLSv1
context: main, server
- ssl_session_cache:The directive sets the types and sizes of caches to store the SSL sessions.
syntax:ssl_session_cache off|none|builtin:size and/or shared:name:size
default:ssl_session_cache off
context:main, server
ssl_session_cache builtin:1000 shared:SSL:10m;
- ssl_session_timeout:Assigns the time during which the client can repeatedly use the parameters of the session, which is stored in the cache.
syntax:ssl_session_timeout time
default:ssl_session_timeout 5m
context:main, server
- SSL/TLS部署最佳實務:http://www.techug.com/post/ssl-tls.html
- Nginx HttpSSL:http://www.nginx.cn/doc/optional/ssl.html
Nginx上部署HTTPS