Nginx上部署HTTPS依賴OpenSSL庫和包含文件,即須先安裝好libssl-dev(或者OpenSSL),且ln -s /usr/lib/x86_64-linux-gnu/libssl.so /usr/lib/,而後在編譯配置Nginx時要指定--with-http_ssl_module和--with-http_v2_module。另外,若要在本地運行openssl命令,要安裝OpenSSL包,本人用的OpenSSL-1.0.2g。注:本文采用Ubuntu 16.04上的操做實例。html
下圖展現了數字證書(HTTPS中使用的由CA簽名的公鑰證書)的簽名和驗證原理:linux
下圖是HTTP2 Frame 格式:RFC7540 - Hypertext Transfer Protocol Version 2 (HTTP/2)
nginx
$ 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 #證書籤名
worker_processes 1; server { server_name YOUR_DOMAINNAME_HERE; listen 443 ssl http2;# http2 is available only since OpenSSL version 1.0.2 listen 80; if ($scheme = http) { #rewrite ^(.*)$ https://$server_name$1 permanent;
return 301 https://$server_name$request_uri;
} ssl_certificate server.crt; ssl_certificate_key server.key; keepalive_timeout 70; }
另外,本人在Chromium 58.0.3029.110 和 Firefox 53.0.3 下均證明了HTTP2被成功啓用:算法
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 you won't use the chain cert file for anything else, you just point Nginx to the main certificate file
下圖展現了證書鏈的工做原理:瀏覽器
syntax:ssl [on|off]安全
default:ssl off服務器
context:main, serversession
syntax:ssl_certificate file併發
default:ssl_certificate cert.pempost
context:main, server
syntax:ssl_certificate_key file
default:ssl_certificate_key cert.pem
context:main, server
syntax:ssl_client_certificate file
default:none
context:main, server
syntax: ssl_dhparam file
default: none
context: main, server
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
syntax: ssl_prefer_server_ciphers [on|off]
default: ssl_prefer_server_ciphers off
context: main, server
syntax: ssl_protocols [SSLv2] [SSLv3] [TLSv1]
default: ssl_protocols SSLv2 SSLv3 TLSv1
context: main, server
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;
syntax:ssl_session_timeout time
default:ssl_session_timeout 5m
context:main, server