当前位置:   article > 正文

SSL/TLS深度解析--在 Nginx 上部署 TLS

nginx tls

利用 openssl 源代码安装 Nginx

  1. [root@localhost software]# tar xf nginx-1.15.5.tar.gz
  2. [root@localhost software]# cd nginx-1.15.5/
  3. [root@localhost nginx-1.15.5]# groupadd nginx
  4. [root@localhost nginx-1.15.5]# useradd nginx -M -s /sbin/nologin -g nginx
  5. [root@localhost nginx-1.15.5]# mkdir -p /project/nginx1.15.0
  6. [root@localhost nginx-1.15.5]# mkdir -p /project/{logs/nginx,cache/nginx}
  7. [root@localhost nginx-1.15.5]# ll /project/
  8. 总用量 0
  9. drwxr-xr-x. 3 root root 19 11月 1 21:48 cache
  10. drwxr-xr-x. 3 root root 19 11月 1 21:48 logs
  11. drwxr-xr-x. 2 root root 6 11月 1 21:48 nginx1.15.0
  12. [root@localhost nginx-1.15.5]# ./configure --prefix=/project/nginx1.15.0 --with-openssl=/opt/software/openssl-1.1.1 --with-openssl-opt="enable-ec_nistp_64_gcc_128" --with-http_ssl_module --user=nginx --group=nginx --error-log-path=/project/logs/nginx/error.log --http-log-path=/project/logs/nginx/access.log --http-client-body-temp-path=/project/cache/nginx/client_temp --http-proxy-temp-path=/project/cache/nginx/proxy_temp --http-fastcgi-temp-path=/project/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/project/cache/nginx/uwsgi_temp --http-scgi-temp-path=/project/cache/nginx/scgi_temp --with-file-aio --with-http_v2_module
  13. [root@localhost nginx-1.15.5]# make -j 2
  14. [root@localhost nginx-1.15.5]# make install
  15. [root@localhost nginx-1.15.5]# make clean
  16. rm -rf Makefile objs
  17. [root@localhost nginx-1.15.5]# cd ..
  18. [root@localhost software]# cd /project/nginx1.15.0/
  19. [root@localhost nginx1.15.0]# sbin/nginx
  20. [root@localhost nginx1.15.0]# netstat -tulnp |grep nginx
  21. tcp 0 0 0.0.0.0:80 0.0.0.0:*
  22. [root@localhost nginx1.15.0]# ps -ef |grep nginx
  23. root 7420 1 0 22:22 ? 00:00:00 nginx: master process sbin/nginx
  24. nginx 7421 7420 0 22:22 ? 00:00:00 nginx: worker process
  25. nginx 7422 7420 0 22:22 ? 00:00:00 nginx: worker process
  26. nginx 7423 7420 0 22:22 ? 00:00:00 nginx: worker process
  27. nginx 7424 7420 0 22:22 ? 00:00:00 nginx: worker process
  28. root 7430 1869 0 22:23 pts/0 00:00:00 grep --color=auto nginx
  29. [root@localhost nginx1.15.0]# mkdir conf/certs
  30. [root@localhost nginx1.15.0]# mkdir html/tls
  31. [root@localhost nginx1.15.0]# echo "Hello TLS" > html/tls/index.html
  32. [root@localhost nginx1.15.0]# cat html/tls/index.html
  33. Hello TLS
  34. [root@localhost nginx1.15.0]# vim conf/nginx.conf
  35. .......
  36. # HTTPS server
  37. #
  38. server {
  39. listen 443 ssl;
  40. server_name www.linuxplus.com;
  41. ssl_certificate certs/rsa_01cert.crt;
  42. ssl_certificate_key certs/rsa_2048prikey.pem;
  43. # ssl_session_cache shared:SSL:1m;
  44. # ssl_session_timeout 5m;
  45. ssl_ciphers HIGH:!aNULL:!MD5;
  46. ssl_prefer_server_ciphers on;
  47. location / {
  48. root html/tls;
  49. index index.html index.htm;
  50. }
  51. }
  52. [root@localhost nginx1.15.0]# cd conf/certs/
  53. [root@localhost certs]# openssl genrsa -out rsa_2048prikey.pem 2048
  54. Generating RSA private key, 2048 bit long modulus (2 primes)
  55. .........................+++++
  56. .....................................................................................+++++
  57. e is 65537 (0x010001)
  58. [root@localhost certs]# ll
  59. 总用量 4
  60. -rw-------. 1 root root 1679 10月 28 19:14 rsa_2048prikey.pem
  61. [root@localhost certs]# openssl req -new -key rsa_2048prikey.pem -out rsa_01cert.csr -subj /C=CN/ST=ShanDong/L=QingDao/O=Devops/OU=Devops/CN=www.linuxplus.com/emailAddress=admin@linuxplus.com
  62. [root@localhost certs]# ll
  63. 总用量 8
  64. -rw-r--r--. 1 root root 1066 10月 28 19:18 rsa_01cert.csr
  65. -rw-------. 1 root root 1679 10月 28 19:14 rsa_2048prikey.pem
  66. [root@localhost certs]# openssl ca -in rsa_01cert.csr -days 300 -md sha384 -out rsa_01cert.crt -batch -notext
  67. Using configuration from /usr/local/openssl/openssl.cnf
  68. Enter pass phrase for /usr/local/openssl/CA/private/root_prikey_ecdsa.pem:
  69. Check that the request matches the signature
  70. Signature ok
  71. Certificate Details:
  72. Serial Number:
  73. 3b:f9:bc:72:54:4e:25:a7:07:2d:92:42:06:a7:61:59
  74. Validity
  75. Not Before: Oct 28 11:20:49 2022 GMT
  76. Not After : Aug 24 11:20:49 2023 GMT
  77. Subject:
  78. countryName = CN
  79. stateOrProvinceName = ShanDong
  80. localityName = QingDao
  81. organizationName = Devops
  82. organizationalUnitName = Devops
  83. commonName = www.linuxplus.com
  84. emailAddress = admin@linuxplus.com
  85. X509v3 extensions:
  86. X509v3 Basic Constraints:
  87. CA:FALSE
  88. Netscape Comment:
  89. OpenSSL Generated Certificate
  90. X509v3 Subject Key Identifier:
  91. DB:39:F0:61:40:3D:0B:4A:0A:20:1C:02:AF:3C:49:2B:86:78:22:C6
  92. X509v3 Authority Key Identifier:
  93. keyid:9F:7A:42:AF:E4:36:0D:01:CD:FF:27:57:18:2A:3E:CC:8A:77:C0:D7
  94. Certificate is to be certified until Aug 24 11:20:49 2023 GMT (300 days)
  95. Write out database with 1 new entries
  96. Data Base Updated
  97. [root@localhost certs]# ll
  98. 总用量 12
  99. -rw-r--r--. 1 root root 1241 10月 28 19:20 rsa_01cert.crt
  100. -rw-r--r--. 1 root root 1066 10月 28 19:18 rsa_01cert.csr
  101. -rw-------. 1 root root 1679 10月 28 19:14 rsa_2048prikey.pem
  102. [root@localhost certs]# cd ../../sbin/
  103. [root@localhost sbin]# ./nginx -t
  104. nginx: the configuration file /project/nginx1.15.0/conf/nginx.conf syntax is ok
  105. nginx: configuration file /project/nginx1.15.0/conf/nginx.conf test is successful
  106. [root@localhost sbin]# ./nginx -s reload

SSL/TLS深度解析--在 Nginx 上部署 TLS

SSL/TLS深度解析--在 Nginx 上部署 TLS

TLS 协议的配置

Nginx协议配置有3个主要的配置项

  • ssl_protocols :用来指定所开启协议的版本,目前1.2是主流而且更高效。不安全的SSLv2 和 SSLv3 都要禁用。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
    目前最新的TLS是1.3版本,不过目前也没有大范围使用,不过要支持TLS1.3,要使用openssl的1.1.1系列版本,所以要使用TLS1.3,升级openssl 并且在编译安装nginx的时候指定。
    ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  • ssl_prefer_server_ciphers ,在服务器与客户端TLS握手时启用服务器算法优先,由
    服务器端选择算法,这样可以避免很多客户端被***或比较老旧而引起的安全问题。
ssl_prefer_server_ciphers  on;
  • ssl_ciphers ,指定使用的算法套件以及它们的优先顺序。
  1. ssl_ciphers "ECDHE-ECDSA-AES128-GCM-SHA256 ECDHE-ECDSA-AES256-GCM-SHA384 ECDHE-RSA-AES128-GCM-SHA256 ECDHE-RSA-AES256-GCM
  2. -SHA384 ECDHE-ECDSA-AES128-GCM-SHA256 ECDHE-RSA-AES128-SHA256 ECDHE-ECDSA-AES128-SHA ECDHE-ECDSA-AES256-SHA ECDHE-RSA-AES256-SHA
  3. AES128-SHA AES256-SHA ECDHE-RSA-RC4-SHA RC4-SHA"; #排在前面的优先使用
  • 通配符证书是证书的CN采用通配形式,即 *.linuxplus.com 这样的模式。如果确定采用全站https的话,这样的通配证书是可行的,很多大站点也是这样做的,例如淘宝。
  • 如果多个不同域名(二级域名)的站点使用同一个证书,可以把它们部署在同一个IP地址上。还是建议使用SNI,使每个站点有自己单独的证书。
配置双证书
  1. [root@localhost certs]# openssl ecparam -genkey -name prime256v1 -out ecdsa_01prikey.pem
  2. [root@localhost certs]# openssl req -new -key ecdsa_01prikey.pem -out ecc01.csr -subj /C=CN/ST=ShanDong/L=QingDao/O=Devops/OU=Devops/CN=www.linuxplus.com/emailAddress=admin@linuxplus.com
  3. [root@localhost certs]# openssl ca -in ecc01.csr -days 365 -out ecc_01cert.crt -batch -notext Using configuration from /usr/local/openssl/openssl.cnf
  4. Enter pass phrase for /usr/local/openssl/CA/private/root_prikey_ecdsa.pem:
  5. Check that the request matches the signature
  6. Signature ok
  7. Certificate Details:
  8. Serial Number:
  9. 92:f4:3b:df:f9:ac:3b:5c:aa:31:89:d6:61:c6:9a:fc
  10. Validity
  11. Not Before: Nov 10 14:32:15 2018 GMT
  12. Not After : Nov 10 14:32:15 2019 GMT
  13. Subject:
  14. countryName = CN
  15. stateOrProvinceName = ShanDong
  16. localityName = QingDao
  17. organizationName = Devops
  18. organizationalUnitName = Devops
  19. commonName = www.linuxplus.com
  20. emailAddress = admin@linuxplus.com
  21. X509v3 extensions:
  22. X509v3 Basic Constraints:
  23. CA:FALSE
  24. Netscape Comment:
  25. OpenSSL Generated Certificate
  26. X509v3 Subject Key Identifier:
  27. 67:7B:E7:71:A6:D5:63:C7:C3:E7:6F:E4:40:B4:06:1C:D5:B6:84:58
  28. X509v3 Authority Key Identifier:
  29. keyid:7A:15:85:5F:24:70:45:4C:86:C3:FD:AA:9A:88:3E:5B:E6:63:70:56
  30. Certificate is to be certified until Nov 10 14:32:15 2019 GMT (365 days)
  31. Write out database with 1 new entries
  32. Data Base Updated
  33. [root@localhost ~]# cd /project/nginx1.15.0/conf/
  34. [root@localhost conf]# vim nginx.conf
  35. ......
  36. ssl_certificate certs/ecc_01cert.crt;
  37. ssl_certificate_key certs/ecdsa_01prikey.pem;
  38. ssl_certificate certs/rsa_01cert.crt;
  39. ssl_certificate_key certs/rsa_2048prikey.pem;
  40. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  41. [root@localhost conf]# ../sbin/nginx -t
  42. nginx: the configuration file /project/nginx1.15.0/conf/nginx.conf syntax is ok
  43. nginx: configuration file /project/nginx1.15.0/conf/nginx.conf test is successful
  44. [root@localhost conf]# ../sbin/nginx -s reload

SSL/TLS深度解析--在 Nginx 上部署 TLS

客户端身份验证

客户端验证,是为了实现只有有证书的客户端才能访问那个站点或服务,证书由站点管理和颁发

  1. [root@www certs]# openssl genrsa -out client01.key 2048
  2. Generating RSA private key, 2048 bit long modulus (2 primes)
  3. ...........................................................................................................+++++
  4. ........................+++++
  5. e is 65537 (0x010001)
  6. [root@www certs]# openssl req -new -key client01.key -out client01.csr -subj /C=CN/ST=ShanXi/L=XiAn/O=Devops01/OU=Devops01/CN=www.linuxplus.com/emailAddress=adm@linuxplus.com
  7. [root@www certs]# openssl ca -in client01.csr -md sha384 -out client01_cert.crt -batch -notext
  8. Using configuration from /usr/local/openssl/openssl.cnf
  9. Enter pass phrase for /usr/local/openssl/CA/private/root_prikey_ecdsa.pem:
  10. Check that the request matches the signature
  11. Signature ok
  12. Certificate Details:
  13. Serial Number:
  14. 92:f4:3b:df:f9:ac:3b:5c:aa:31:89:d6:61:c6:9a:fd
  15. Validity
  16. Not Before: Nov 11 06:06:53 2018 GMT
  17. Not After : Nov 11 06:06:53 2019 GMT
  18. Subject:
  19. countryName = CN
  20. stateOrProvinceName = ShanXi
  21. localityName = XiAn
  22. organizationName = Devops01
  23. organizationalUnitName = Devops01
  24. commonName = www.linuxplus.com
  25. emailAddress = adm@linuxplus.com
  26. X509v3 extensions:
  27. X509v3 Basic Constraints:
  28. CA:FALSE
  29. Netscape Comment:
  30. OpenSSL Generated Certificate
  31. X509v3 Subject Key Identifier:
  32. AC:6C:C1:A7:5A:C5:91:BD:97:3C:4A:2D:CA:03:53:91:38:E9:3B:E6
  33. X509v3 Authority Key Identifier:
  34. keyid:7A:15:85:5F:24:70:45:4C:86:C3:FD:AA:9A:88:3E:5B:E6:63:70:56
  35. Certificate is to be certified until Nov 11 06:06:53 2019 GMT (365 days)
  36. Write out database with 1 new entries
  37. Data Base Updated
  38. [root@www certs]# openssl pkcs12 -export -clcerts -passout pass:123456 -in client01_cert.crt -inkey client01.key -out client01.p12
  39. [root@www ~]# cd /project/nginx1.15.0/conf/
  40. [root@www conf]# vim nginx.conf
  41. ssl_certificate certs/ecc_01cert.crt;
  42. ssl_certificate_key certs/ecdsa_01prikey.pem;
  43. ssl_certificate certs/rsa_01cert.crt;
  44. ssl_certificate_key certs/rsa_2048prikey.pem;
  45. ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
  46. # 开启客户端身份验证
  47. ssl_verify_client on;
  48. # 指定客户端证书到根证书的深度
  49. # ssl_verify_depth 2;
  50. # 指定签发客户端证书的CA证书
  51. ssl_client_certificate /usr/local/openssl/CA/root_cacert_ecc.pem;
  52. # 完整证书链中需要包含的其他CA证书
  53. # ssl_trusted_certificate rootca.crt;
  54. # 证书吊销列表,有更新时Nginx需要重新加载
  55. ssl_crl /usr/local/openssl/CA/crl.pem;
  56. #on:是开启只接有客户端证书的请求。如果请求未包含证书或者证书校验失败,nginx会返回一个400错误响应。
  57. #off:是关闭
  58. #optional:不会强制阻断访问,不返回400。可在 $ssl_client_verify 变量中查看各种状态, NONE表示没有证书, FAILED表示证书未通过验证, SUCCESS 表示证书有效。
  59. [root@www conf]# ../sbin/nginx -t
  60. nginx: the configuration file /project/nginx1.15.0/conf/nginx.conf syntax is ok
  61. nginx: configuration file /project/nginx1.15.0/conf/nginx.conf test is successful
  62. [root@www conf]# ../sbin/nginx -s reload
  • 没有导入证书
    SSL/TLS深度解析--在 Nginx 上部署 TLS

  • Firefox 导入证书
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS

  • 360浏览器导入证书
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
    SSL/TLS深度解析--在 Nginx 上部署 TLS
  1. [root@www certs]# openssl ca -in rsa_01cert.csr -days 300 -md sha384 -out rsa_01cert.crt -batch -notext^C
  2. [root@www certs]# openssl req -new -key client02.key -out client02.csr -subj /C=CN/ST=ShanXi/L=XiAn/O=Devops02/OU=Devops02/CN=www.linuxplus.com/emailAddress=adm@linuxplus.com
  3. [root@www certs]# openssl ca -in client02.csr -md sha384 -out client02_cert.crt -batch -notext
  4. Using configuration from /usr/local/openssl/openssl.cnf
  5. Enter pass phrase for /usr/local/openssl/CA/private/root_prikey_ecdsa.pem:
  6. Check that the request matches the signature
  7. Signature ok
  8. Certificate Details:
  9. Serial Number:
  10. 92:f4:3b:df:f9:ac:3b:5c:aa:31:89:d6:61:c6:9a:fe
  11. Validity
  12. Not Before: Nov 11 14:00:18 2018 GMT
  13. Not After : Nov 11 14:00:18 2019 GMT
  14. Subject:
  15. countryName = CN
  16. stateOrProvinceName = ShanXi
  17. localityName = XiAn
  18. organizationName = Devops02
  19. organizationalUnitName = Devops02
  20. commonName = www.linuxplus.com
  21. emailAddress = adm@linuxplus.com
  22. X509v3 extensions:
  23. X509v3 Basic Constraints:
  24. CA:FALSE
  25. Netscape Comment:
  26. OpenSSL Generated Certificate
  27. X509v3 Subject Key Identifier:
  28. 6D:D2:63:9D:21:B1:82:4A:0F:19:B8:76:0F:B5:EA:E8:F0:F6:A3:6F
  29. X509v3 Authority Key Identifier:
  30. keyid:7A:15:85:5F:24:70:45:4C:86:C3:FD:AA:9A:88:3E:5B:E6:63:70:56
  31. Certificate is to be certified until Nov 11 14:00:18 2019 GMT (365 days)
  32. Write out database with 1 new entries
  33. Data Base Updated
  34. [root@www certs]# openssl pkcs12 -export -clcerts -passout pass:123456 -in client02_cert.crt -inkey client02.key -out client02.p12
  35. #吊销证书
  36. [root@www certs]# openssl x509 -in client01_cert.crt -serial -noout
  37. serial=92F43BDFF9AC3B5CAA3189D661C69AFD
  38. [root@www certs]# openssl ca -revoke /usr/local/openssl/CA/newcerts/92F43BDFF9AC3B5CAA3189D661C69AFD.pem
  39. Using configuration from /usr/local/openssl/openssl.cnf
  40. Enter pass phrase for /usr/local/openssl/CA/private/root_prikey_ecdsa.pem:
  41. Revoking Certificate 92F43BDFF9AC3B5CAA3189D661C69AFD.
  42. Data Base Updated
  43. [root@www certs]# openssl ca -gencrl -out /usr/local/openssl/CA/crl.pem
  44. Using configuration from /usr/local/openssl/openssl.cnf
  45. Enter pass phrase for /usr/local/openssl/CA/private/root_prikey_ecdsa.pem:
  46. [root@www certs]# openssl crl -in /usr/local/openssl/CA/crl.pem -text
  47. Certificate Revocation List (CRL):
  48. Version 2 (0x1)
  49. Signature Algorithm: ecdsa-with-SHA256
  50. Issuer: C = CN, ST = BeiJing, L = BeiJing, O = CAdevops, OU = CAdevops, CN = root_ca, emailAddress = admin@linuxplus.com
  51. Last Update: Nov 11 14:33:36 2018 GMT
  52. Next Update: Dec 11 14:33:36 2018 GMT
  53. CRL extensions:
  54. X509v3 CRL Number:
  55. 1048577
  56. Revoked Certificates:
  57. Serial Number: 92F43BDFF9AC3B5CAA3189D661C69AFD
  58. Revocation Date: Nov 11 14:26:37 2018 GMT
  59. Signature Algorithm: ecdsa-with-SHA256
  60. 30:45:02:21:00:e3:76:00:d4:07:22:2a:7f:43:1f:aa:8c:f5:
  61. be:c7:f7:a9:bd:1f:fb:65:f0:0b:d8:0c:a0:15:7c:f3:37:5d:
  62. 63:02:20:08:d6:b8:4b:6b:3f:d2:7d:89:5f:2d:88:b5:ee:18:
  63. cd:81:6d:fe:80:4f:0c:ef:78:b8:81:c1:dc:ca:85:a3:9b
  64. -----BEGIN X509 CRL-----
  65. MIIBTjCB9QIBATAKBggqhkjOPQQDAjCBjTELMAkGA1UEBhMCQ04xEDAOBgNVBAgM
  66. B0JlaUppbmcxEDAOBgNVBAcMB0JlaUppbmcxETAPBgNVBAoMCENBZGV2b3BzMREw
  67. DwYDVQQLDAhDQWRldm9wczEQMA4GA1UEAwwHcm9vdF9jYTEiMCAGCSqGSIb3DQEJ
  68. ARYTYWRtaW5AbGludXhwbHVzLmNvbRcNMTgxMTExMTQzMzM2WhcNMTgxMjExMTQz
  69. MzM2WjAkMCICEQCS9Dvf+aw7XKoxidZhxpr9Fw0xODExMTExNDI2MzdaoBAwDjAM
  70. BgNVHRQEBQIDEAABMAoGCCqGSM49BAMCA0gAMEUCIQDjdgDUByIqf0Mfqoz1vsf3
  71. qb0f+2XwC9gMoBV88zddYwIgCNa4S2s/0n2JXy2Ite4YzYFt/oBPDO94uIHB3MqF
  72. o5s=
  73. -----END X509 CRL-----

SSL/TLS深度解析--在 Nginx 上部署 TLS
SSL/TLS深度解析--在 Nginx 上部署 TLS

  1. [root@www conf]# vim nginx.conf
  2. log_format tls "$ssl_client_verify $pid $scheme $server_name $time_local $remote_addr $connection $connection_requests $ssl_protocol $ssl_cipher $ssl_session_id $ssl_session_reused $ssl_curves";
  3. access_log /project/logs/nginx/access.log tls;
  4. #$scheme:使用哪种协议
  5. #$connection:TCP连接序号
  6. #$connection_requests:表示在一个连接(长连接)里面有多少次请求
  7. [root@www logs]# tail -f access.log
  8. SUCCESS 20481 https www.linuxplus.com 23/Nov/2018:23:12:03 +0800 172.16.216.181 315 1 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 - . X25519:prime256v1:secp384r1:secp521r1:0x0100:0x0101
  9. SUCCESS 20481 https www.linuxplus.com 23/Nov/2018:23:12:03 +0800 172.16.216.181 315 2 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 - . X25519:prime256v1:secp384r1:secp521r1:0x0100:0x0101
  10. SUCCESS 20481 https www.linuxplus.com 23/Nov/2018:23:12:28 +0800 172.16.216.181 315 3 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 - . X25519:prime256v1:secp384r1:secp521r1:0x0100:0x0101
  11. SUCCESS 20481 https www.linuxplus.com 23/Nov/2018:23:12:28 +0800 172.16.216.181 315 4 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 - . X25519:prime256v1:secp384r1:secp521r1:0x0100:0x0101
  12. ........
  13. #SUCCESS:表示成功
  14. #20481:Nginx的PID
  15. #ECDHE-ECDSA-AES128-GCM-SHA256:密钥套件

SSL/TLS深度解析--在 Nginx 上部署 TLS

会话缓存

  • 独立会话缓存
  1. [root@www nginx1.15.0]# vim conf/nginx.conf
  2. ssl_session_tickets off;
  3. ssl_session_cache shared:SSL:1m; #分配1MB的共享内存缓存,使用1 MB的内存可以缓存大约4000个会话
  4. ssl_session_timeout 5m; #设置会话缓存过期时间,默认的会话缓存过期时间只有5分钟
  5. SUCCESS 20574 https www.linuxplus.com 23/Nov/2018:23:43:22 +0800 172.16.216.181 331 1 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 20dfe772e67ea1fd9792ad5718cd416be900c51df38bf05ed87371049c1c41ed r -
  6. SUCCESS 20574 https www.linuxplus.com 23/Nov/2018:23:43:22 +0800 172.16.216.181 331 2 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 20dfe772e67ea1fd9792ad5718cd416be900c51df38bf05ed87371049c1c41ed r -
  7. SUCCESS 20574 https www.linuxplus.com 23/Nov/2018:23:43:24 +0800 172.16.216.181 331 3 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 20dfe772e67ea1fd9792ad5718cd416be900c51df38bf05ed87371049c1c41ed r -
  8. SUCCESS 20574 https www.linuxplus.com 23/Nov/2018:23:43:24 +0800 172.16.216.181 331 4 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 20dfe772e67ea1fd9792ad5718cd416be900c51df38bf05ed87371049c1c41ed r -
  9. SUCCESS 20574 https www.linuxplus.com 23/Nov/2018:23:43:24 +0800 172.16.216.181 331 5 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 20dfe772e67ea1fd9792ad5718cd416be900c51df38bf05ed87371049c1c41ed r -
  10. SUCCESS 20574 https www.linuxplus.com 23/Nov/2018:23:43:24 +0800 172.16.216.181 331 6 TLSv1.2 ECDHE-ECDSA-AES128-GCM-SHA256 20dfe772e67ea1fd9792ad5718cd416be900c51df38bf05ed87371049c1c41ed r -
  11. #r 则表示被重用,如果是.表示没有重用

SSL/TLS深度解析--在 Nginx 上部署 TLS

  • 配置项使用格式:

    ssl_session_cache  off | none | [builtin[:size]] [shared:name:size]

    默认选项是 none;
    off :禁用缓存 。
    none:禁用缓存,但是通知客户端可以重用会话(session),但是并不实际存储。
    builtin:内建,这个缓存只能被一个worker进程使用(nginx可以有过个worker进程),与builtin 配合的单位参数是个数 builtin:100 表示缓存100个session;如果没有写明数量 那么默认是20480个session。使用builtin会引起一些内存碎片。
    shared:共享, shared:xx_name:xxM;,在多个worker进程中共享session,单位是M(兆字节),1M可以存放大概4000个session,共享的缓存可以有1个名称,名称一样的缓存可以在多个 nginx 上配置的 server 块上共享使用。
    还可以混合使用 builtin 和 shared
    ssl_session_cache builtin:1000 shared:SSL:10m; 只用共享缓存而不用内置缓存应该更有效率。根据项目情况来决定。

  • 分布式会话票证
  1. [root@www nginx1.15.0]# cd conf/certs/
  2. [root@www certs]# openssl rand -out ticket48.key 48
  3. [root@www certs]# openssl rand -out ticket80.key 80
  4. [root@www certs]# ll -l ticket48.key
  5. -rw-r--r--. 1 root root 48 11月 24 14:23 ticket48.key
  6. [root@www certs]# ll -l ticket80.key
  7. -rw-r--r--. 1 root root 80 11月 24 14:31 ticket80.key
  8. [root@www certs]# cd ../..
  9. [root@www nginx1.15.0]# vim conf/nginx.conf
  10. ssl_session_tickets on;
  11. ssl_session_ticket_key certs/ticket48.key; #设置新密钥,用于新票证的加解密
  12. ssl_session_ticket_key certs/ticket80.key; #保留前一个密钥用于老票证的解密
  13. [root@www nginx1.15.0]# sbin/nginx -s reload
  14. [root@www nginx1.15.0]# openssl s_client -connect 172.16.216.188:443
  15. .......
  16. SSL-Session:
  17. Protocol : TLSv1.2
  18. Cipher : ECDHE-ECDSA-AES128-GCM-SHA256
  19. Session-ID: 0734C4A2519DD91A6B03BA6E1A572FA2E8DAB69CC23A41A249E4219B6B16934E
  20. Session-ID-ctx:
  21. Master-Key: AC6A686B930A886990E031117F1032F5829C57EAFA2C363D9917973E401FE420D5F566BA5F5CD5ED2E922F5E6E6E1F1B
  22. PSK identity: None
  23. PSK identity hint: None
  24. SRP username: None
  25. TLS session ticket lifetime hint: 300 (seconds)
  26. TLS session ticket:
  27. 0000 - 95 0a 94 18 66 9c fa fb-bb e7 79 81 19 46 a5 77 ....f.....y..F.w
  28. 0010 - ec a8 37 e6 6e a2 34 0d-4e 2b e2 ce 58 3c a8 23 ..7.n.4.N+..X<.#
  29. 0020 - 8f 73 59 fd 30 0a bf 37-e2 47 6d 9e 10 76 1a 90 .sY.0..7.Gm..v..
  30. 0030 - f5 5d 7c 8c e0 32 a5 d4-3a a5 c5 e9 dc 62 e5 eb .]|..2..:....b..
  31. 0040 - fc 7d c0 98 df dd 76 4c-29 d6 51 79 d9 6a c2 f7 .}....vL).Qy.j..
  32. 0050 - e2 a5 ec a3 46 d1 27 3c-75 12 38 18 ec 20 b1 18 ....F.'<u.8.. ..
  33. 0060 - 41 13 be 58 45 96 a5 1f-7a 90 aa a1 73 17 8b 27 A..XE...z...s..'
  34. 0070 - 89 7b 63 2a 2f ad 61 53-3d d8 4e 13 c6 41 97 1f .{c*/.aS=.N..A..
  35. 0080 - ec 75 d7 bf 7f 96 29 4d-cf f6 3e 0d 23 35 fc 9a .u....)M..>.#5..
  36. 0090 - 57 98 2a 81 2c e7 b0 e1-27 33 aa d7 fb 13 01 c3 W.*.,...'3......
  37. 00a0 - 91 86 f5 63 5c b5 be 1a-58 a5 99 61 1a 82 36 de ...c\...X..a..6.
  38. ......

SSL/TLS深度解析--在 Nginx 上部署 TLS

使用两个密钥轮转的方式,在密钥更新时服务器就不会丢弃更新前建立的会话。
在集群中实施会话票证密钥的轮替是不可靠的,因为无法完美的实现新的密钥在同一个时刻被所有节点同时更新。如果某个节点在其他节点前使用了新密钥,并给某个客户端生成了票据,随后客户端再次发送过来的请求被分配到其他节点处理,而其他节点可能无法解密数据(集群未采取流量保持机制),而导致SSL重新握手,这样会造成性能下降,甚至会出现一个瓶颈期;归根结底是各个节点在重新加载配置的时候会不可避免的存在时间差。如果选择使用会话票证,不要过于频繁的更新密钥,尽量在设计上会使用流量保持,把同一个用户分发到相同节点。
如果要完美地实现集群的会话票证密钥轮转,并且不介意操作两次集群配置,可以按以下步骤操作。
(1) 生成一个新密钥。
(2) 将新的密钥替换掉只用于解密的老密钥,重启各个节点;加载配置,使所有节点都只使用新密钥。
(3) 将两个密钥交换位置,新密钥作为加解密的密钥,之前的密钥作为只解密的老密钥。
可以从容的依次重启各个节点,因为所有节点在第一次配置中已经加载了新密钥,所以可以正常解密由新密钥加密的票据,不会有任何时间差带来的问题。

转载于:https://blog.51cto.com/stuart/2322041

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/886880
推荐阅读
相关标签
  

闽ICP备14008679号