无意中发现了一条 openssl
命令,可以一键生成 https 所需要的私钥和证书,命令如下
openssl req \ -newkey rsa:4096 \ -x509 \ -nodes \ -keyout localhost.key \ -new \ -out localhost.crt \ -subj /CN=localhost \ -reqexts SAN \ -extensions SAN \ -config <(cat /System/Library/OpenSSL/openssl.cnf \ <(printf '[SAN]\nsubjectAltName=DNS:localhost')) \ -sha256 \ -days 3650
这条命令会生成 localhost.key
私钥文件和 localhost.crt
证书文件
而对于你来说,所需要的更改就是把 /CN=localhost
和 DNS:localhost
中的 localhost
改成你自己的域名
浏览器提示不受信任
如果浏览器提示不受信任,那么只需要将 localhost.crt
导入受信任列表即可。
对于 Mac 用户来说,最简单,只要直接使用 Safari 浏览器打开网站,然后选择信任即可。
Nginx https 服务器配置
刚刚在演示 HTTP/2 服务器端推送时,配置本地 localhost
也是废了好长时间,除了使用上面的一键生成证书外,还需要做如下的配置
server { listen 443 ssl; server_name localhost; root /Users/yufei/www/htdocs; http2_push_preload on; ssl on; ssl_certificate /usr/local/etc/nginx/cert/localhost.crt; ssl_certificate_key /usr/local/etc/nginx/cert/localhost.key; ssl_protocols TLSv1 TLSv1.1 TLSv1.2; ssl_prefer_server_ciphers on; ssl_ciphers "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:ECDHE-RSA-AES256-SHA384:ECDHE-RSA-AES128-SHA256:ECDHE-RSA-AES256-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-RSA-AES128-SHA:ECDHE-RSA-DES-CBC3-SHA:EDH-RSA-DES-CBC3-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:HIGH:!aNULL:!eNULL:!EXPORT:!DES:!MD5:!PSK:!RC4"; ssl_session_timeout 60m; ssl_session_cache builtin:1000 shared:SSL:10m; ssl_stapling on; ssl_stapling_verify on; add_header Strict-Transport-Security "max-age=31536000;includeSubdomains;preload"; add_header X-Frame-Options "SAMEORIGIN"; location / { root /Users/yufei/www/htdocs; index index.html index.htm index.php; } }
如果需要配置 http 301 跳转到 https 的话,可以做如下的配置
server { listen 80; server_name localhost; rewrite ^(.*)$ https://$host$1 permanent; }
目前尚无回复