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签名的公钥证书)的签名和验证原理(流程):
- Nginx上部署HTTPS
- 自签发证书:生成证书可以在其他机器上去执行,然后再将所生成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 证书结束;Web 浏览器已预先配置了一组浏览器自动信任的根 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 you won‘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 filedefault: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 filedefault: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 filedefault:none
context:main, server
- ssl_dhparam:Indicates file with Diffie-Hellman parameters in PEM format, utilized for negotiating TLS session keys.
syntax: ssl_dhparam filedefault: none
context: main, server
- ssl_ciphers:Directive describes the permitted ciphers. Ciphers are assigned in the formats supported by OpenSSL.
syntax: ssl_ciphers filedefault: 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:sizedefault: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 timedefault: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