nginxssl加密

愿君学长松,慎勿作桃李。这篇文章主要讲述nginxssl加密相关的知识,希望能为你提供帮助。
1、生成SSL证书ssl证书可以通过在公有云服务商申请或者自建的方式获取,我这里直接自建
1.1、查看nginx是否安装ssl模块,如果不存在ssl模块,需要编译添加,yum安装的nginx默认支持

nginx -V

1.2 生成key密钥
[root@localhost ~]# cd /etc/nginx #创建证书目录 [root@localhost nginx]# mkdir ssl& & cd ssl #系统性能足够好,可使用4096位密钥 [root@localhost ssl]# openssl genrsa -idea -out nginx.key 1024 Generating RSA private key, 1024 bit long modulus ................++++++ ...............................++++++ e is 65537 (0x10001) Enter pass phrase for nginx.key: #输入不少于4字符的密码 Verifying - Enter pass phrase for nginx.key: #验证密码 [root@localhost ssl]# ls nginx.key

1.3 生成证书签名请求文件
[root@localhost ssl]# openssl req -new -key nginx.key -out nginx.csr Enter pass phrase for nginx.key: You are about to be asked to enter information that will be incorporated into your certificate request. What you are about to enter is what is called a Distinguished Name or a DN. There are quite a few fields but you can leave some blank For some fields there will be a default value, If you enter \'.\', the field will be left blank. ----- Country Name (2 letter code) [XX]:CN #输入国家 State or Province Name (full name) []:Shanghai #输入省份 Locality Name (eg, city) [Default City]:Shanghai #输入城市 Organization Name (eg, company) [Default Company Ltd]:gass #输入公司名称 Organizational Unit Name (eg, section) []:zhian #输入部门名 Common Name (eg, your name or your server\'s hostname) []:*.domain.com #输入域名,此处应当填写你要部署的域名,如果是单个则直接添加即可,如果不确定,使用*,表示可以对所有domain.com的子域名做认证 Email Address []:admin@domain.com #输入邮箱地址Please enter the following \'extra\' attributes to be sent with your certificate request A challenge password []: #输入密码,可直接跳过 An optional company name []: #可选公司名称,直接跳过 [root@localhost ssl]# ls nginx.csrnginx.key

【nginxssl加密】1.4 创建不需要输入密码的RSA证书,否则每次reload、restart都需要输入密码
[root@localhost ssl]# openssl rsa -in nginx.key -out nginx_nopass.key Enter pass phrase for nginx.key:#之前RSA秘钥创建时的密码 writing RSA key

1.5 生成证书签名文件(CA文件),当前方式为自己签发,实际应该将自己生成的csr文件提交给SSL认证机构认证
#有效期设置了 10 年 [root@localhost ssl]# openssl x509 -req -days 3650 -in jesonc.csr -signkey jesonc.key -out jesonc.crt Signature ok subject=/C=CN/ST=ShangHai/L=ShangHai/O=ACBC/OU=Tech/CN=*.domain.com/emailAddress=admin@domain.com Getting Private key Enter pass phrase for nginx.key:#RSA创建时的密码

2、配置nginx
server { listen443 ssl http2 default_server; listen[::]:443 ssl http2 default_server; server_nametest.domain.com; #与申请时的域名保持一致,否则会报错 root/usr/share/nginx/html; ssl_certificate "/etc/nginx/ssl/nginx.crt"; #crt证书存放地址 ssl_certificate_key "/etc/nginx/ssl/nginx_nopass.key"; ssl_session_cache shared:SSL:1m; ssl_session_timeout10m; ssl_ciphers HIGH:!aNULL:!MD5; ssl_prefer_server_ciphers on; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; location / { }error_page 404 /404.html; location = /40x.html { }error_page 500 502 503 504 /50x.html; location = /50x.html { } }

配置详解:
  • ssl_certificate证书其实是个公钥,它会被发送到连接服务器的每个客户端,ssl_certificate_key私钥是用来解密的,所以它的权限要得到保护但nginx的主进程能够读取。当然私钥和证书可以放在一个证书文件中,这种方式也只有公钥证书才发送到client。
  • ssl_session_timeout 客户端可以重用会话缓存中ssl参数的过期时间,内网系统默认5分钟太短了,可以设成30m即30分钟甚至4h。
  • ssl_protocols指令用于启动特定的加密协议,nginx在1.1.13和1.0.12版本后默认是ssl_protocols SSLv3 TLSv1 TLSv1.1 TLSv1.2,TLSv1.1与TLSv1.2要确保OpenSSL > = 1.0.1 ,SSLv3 现在还有很多地方在用但有不少被公鸡的漏洞。
  • ssl_ciphers选择加密套件,不同的浏览器所支持的套件(和顺序)可能会不同。这里指定的是OpenSSL库能够识别的写法,你可以通过 openssl -v cipher ‘RC4:HIGH:!aNULL:!MD5’(后面是你所指定的套件加密算法) 来看所支持算法。
  • ssl_prefer_server_ciphers on设置协商加密算法时,优先使用我们服务端的加密套件,而不是客户端浏览器的加密套件。
    3、测试
    #查看nginx配置语法 nginx -t #重启nginx nginx -s reload

    http访问跳转httpsnginx增添以下配置
    server { listen80 default_server; listen[::]:80 default_server; server_nametest.domain.com; root/usr/share/nginx/html; # Load configuration files for the default server block. include /etc/nginx/default.d/*.conf; return301 https://$server_name$request_uri; #在80监听端口 配置跳转

    重启nginx

    推荐阅读