zoukankan      html  css  js  c++  java
  • 基于nginx结合openssl实现https

    在未使用SSL证书对服务器数据进行加密认证的情况下,用户的数据将会以明文的形式进行传输,这样一来使用抓包工具是可以获取到用户密码信息的,非常危险。而且也无法验证数据一致性和完整性,不能确保数据在传输过程中没被改变。所以网站如果有涉及用户账户等重要信息的情况下通常要配置使用SSL证书,实现https协议。

    在生产环境中的SSL证书都需要通过第三方认证机构购买,分为专业版OV证书(浏览器地址栏上不显示企业名称)和高级版EV(可以显示企业名称)证书,证书所保护的域名数不同也会影响价格(比如只对www认证和通配*认证,价格是不一样的),且不支持三级域名。测试中可以自己作为证书颁发机构来制作证书,浏览器会显示为红色,代表证书过期或者无效,如果是黄色的话代表网站有部分连接使用的仍然是http协议。

    不管使用哪种方法,在拿到证书后对Nginx的配置都是一样的,所以这里以搭建OpenSSL并制作证书来进行完整说明

    一、准备环境

    1)nginx服务

    2)ssl模块

    1 [root@ns3 ~]# systemctl stop firewalld
    2 [root@ns3 ~]# iptables -F
    3 [root@ns3 ~]# setenforce 0
    4 [root@ns3 ~]# yum -y install pcre zlib pcre-devel zlib-devel 
    5 [root@ns3 ~]# tar xf nginx-1.16.0.tar.gz -C /usr/src/ 
    6 [root@ns3 ~]#cd /usr/src/nginx-1.16.0
    7 [root@ns3 ~]#./configure --prefix=/usr/local/nginx --user=nginx --group=nginx --with-http_stub_status_module --with-http_ssl_module --with-http_flv_module --with-http_gzip_static_module&&make && make install #后续需要的模块一次性安装

    3)检测openssl是否安装

    1 [root@ns3 ~]# rpm -qa openssl 2 openssl-1.0.1e-42.el7.x86_64

     若没有安装

    1 [root@ns3 ~]# yum -y install openssl openssl-devel

    二、创建根证书CA

    1、生成CA私钥

    1 [root@ns3 ~]# cd zhengshu/
    2 [root@ns3 zhengshu]# openssl genrsa -out local.key 2048
    3 Generating RSA private key, 2048 bit long modulus
    4 ...........................................................................................................................................................................................................................+++
    5 ............................................................................................................................................................................................+++
    6 e is 65537 (0x10001)
    7 [root@ns3 zhengshu]# ls
    8 local.key

     2、生成CA证书请求

     1 [root@ns3 zhengshu]# openssl req -new -key local.key -out local.csr
     2 You are about to be asked to enter information that will be incorporated
     3 into your certificate request.
     4 What you are about to enter is what is called a Distinguished Name or a DN.
     5 There are quite a few fields but you can leave some blank
     6 For some fields there will be a default value,
     7 If you enter '.', the field will be left blank.
     8 -----
     9 Country Name (2 letter code) [XX]:CN  #国家
    10 State or Province Name (full name) []:BJ   #省份
    11 Locality Name (eg, city) [Default City]:BJ  #城市
    12 Organization Name (eg, company) [Default Company Ltd]:
    13 Organizational Unit Name (eg, section) []:test   #部门
    14 Common Name (eg, your name or your server's hostname) []:test   #主机名
    15 Email Address []:test@test.com  #邮箱
    16 
    17 Please enter the following 'extra' attributes
    18 to be sent with your certificate request
    19 A challenge password []:wuminyan  #密码
    20 An optional company name []:wuminyan  #姓名
    21 [root@ns3 zhengshu]# ls
    22 local.csr  local.key
    1 req: 这是一个大命令,提供生成证书请求文件,验证证书,和创建根CA
    2 -new: 表示新生成一个证书请求
    3 -x509: 直接输出证书
    4 -key: 生成证书请求时用到的私钥文件
    5 -out:输出文件

    3、生成CA根证书

    1 这个生成CA证书的命令会让人迷惑
    2 1.通过秘钥 生成证书请求文件
    3 2.通过证书请求文件 生成最终的证书 
    4  -in 使用证书请求文件生成证书,-signkey 指定私钥,这是一个还没搞懂的参数
    5 [root@ns3 zhengshu]# openssl x509 -req -in local.csr -extensions v3_ca -signkey local.key -out local.crt
    6 Signature ok
    7 subject=/C=CN/ST=BJ/L=BJ/O=Default Company Ltd/OU=test/CN=test/emailAddress=test@test.com
    8 Getting Private key

    三、根据CA证书创建server端证书

    1、生成server私匙

    1 [root@ns3 zhengshu]# openssl genrsa -out my_server.key 2048 
    2 Generating RSA private key, 2048 bit long modulus
    3 .................................+++
    4 .........................................+++
    5 e is 65537 (0x10001)
    6 [root@ns3 zhengshu]# ls
    7 local.crt  local.csr  local.key  my_server.key

    2、生成server证书请求

     1 [root@ns3 zhengshu]# openssl x509 -req -in local.csr -extensions v3_ca -signkey local.key -out local.crt
     2 Signature ok
     3 subject=/C=CN/ST=BJ/L=BJ/O=Default Company Ltd/OU=test/CN=test/emailAddress=test@test.com
     4 Getting Private key
     5 [root@ns3 zhengshu]# openssl genrsa -out my_server.key 2048 
     6 Generating RSA private key, 2048 bit long modulus
     7 .................................+++
     8 .........................................+++
     9 e is 65537 (0x10001)
    10 [root@ns3 zhengshu]# openssl req -new -key my_server.key -out my_server.csr
    11 You are about to be asked to enter information that will be incorporated
    12 into your certificate request.
    13 What you are about to enter is what is called a Distinguished Name or a DN.
    14 There are quite a few fields but you can leave some blank
    15 For some fields there will be a default value,
    16 If you enter '.', the field will be left blank.
    17 -----
    18 Country Name (2 letter code) [XX]:CN
    19 State or Province Name (full name) []:BJ
    20 Locality Name (eg, city) [Default City]:BJ
    21 Organization Name (eg, company) [Default Company Ltd]:
    22 Organizational Unit Name (eg, section) []:test
    23 Common Name (eg, your name or your server's hostname) []:test
    24 Email Address []:test@test.com
    25 
    26 Please enter the following 'extra' attributes
    27 to be sent with your certificate request
    28 A challenge password []:wuminyan
    29 An optional company name []:wuminyan
    30 [root@ns3 zhengshu]# ls
    31 local.crt  local.csr  local.key  my_server.csr  my_server.key

    3、生成server证书

    1 [root@ns3 zhengshu]# openssl x509 -days 365 -req -in my_server.csr -extensions v3_req -CAkey local.key -CA local.crt -CAcreateserial -out my_server.crt
    2 Signature ok
    3 subject=/C=CN/ST=BJ/L=BJ/O=Default Company Ltd/OU=test/CN=test/emailAddress=test@test.com
    4 Getting CA Private Key

    四、配置nginx支持SSL

     1 [root@ns3 ~]# vim /etc/nginx.cof      #这里设置了一个软连接:lln -s /usr/local/nginx/sbin/nginx /usr/local/sbin/
     2 server {
     3         listen 80;
     4         listen       443 default  ssl;  #监听433端口
     5                 keepalive_timeout 100;  #开启keepalive 激活keepalive长连接,减少客户端请求次数
     6 
     7                    ssl_certificate      /root/zhengshu/local.crt;   #server端证书位置
     8                    ssl_certificate_key  /root/zhengshu/local.key;   #server端私钥位置
     9 
    10                         ssl_session_cache    shared:SSL:10m;         #缓存session会话
    11                         ssl_session_timeout  10m;                    # session会话    10分钟过期
    12 
    13                    ssl_ciphers  HIGH:!aNULL:!MD5;
    14                    ssl_prefer_server_ciphers  on;
    15 
    16         server_name   test.com;
    17         charset utf-8;
    18 
    19         location / {
    20             root   html;
    21             index  index.html index.htm;
    22         }
    23 
    24     }
    25 }

    五、测试

    输入https://192.168.200.115

  • 相关阅读:
    ubuntu中source insight打不开,报错pagefault的解决方法
    第六次团队作业——Alpha冲刺之事后诸葛亮
    Alpha阶段总结
    第五次团队作业——第一次项目冲刺——Alpha版本
    第四次团队作业——系统设计
    团队项目——需求规格说明书
    第二次团队作业——预则立&&他山之石
    团队项目时间规划2016
    第二次结对编程作业——毕设导师智能匹配
    第二次团队作业——团队选题报告
  • 原文地址:https://www.cnblogs.com/qiyueqi/p/11551238.html
Copyright © 2011-2022 走看看