zoukankan      html  css  js  c++  java
  • 16. Nginx HTTPS服务

    自签证书

    生成key文件

    #genrsa 使用idea算法,生成rsa证书,证书名为 debug.siguoya.name.key,1024表示位数 
    openssl genrsa -idea -out debug.siguoya.name.key 1024 
    #设置证书密码,在生成csr与crt文件的时候需要用到
    #Enter pass phrase for debug.siguoya.name.key:
    #Verifying - Enter pass phrase for debug.siguoya.name.key:
    

    通过key文件,生成csr文件

    openssl req -new -key debug.siguoya.name.key -out debug.siguoya.name.csr
    Enter pass phrase for debug.siguoya.name.key:
    #输入刚才创建的key文件时设置的密码即可
    Country Name (2 letter code) [XX]:CN
    State or Province Name (full name) []:GuangDong
    Locality Name (eg, city) [Default City]:GuangZhou
    Organization Name (eg, company) [Default Company Ltd]:company
    Organizational Unit Name (eg, section) []:section
    Common Name (eg, your name or your servers hostname) []:debug.siguoya.name
    Email Address []:924714558@qq.com
    
    Please enter the following extra attributes
    to be sent with your certificate request
    #与上述创建key文件的密码不一样,直接留空即可
    A challenge password []:
    An optional company name []:
    

    将key文件与csr文件进行打包,生成crt文件

    #-days指定证书的过期时间,以下指定为10年
    openssl x509 -req -days 3650 -in debug.siguoya.name.csr -signkey debug.siguoya.name.key -out debug.siguoya.name.crt
    

    配置Nginx服务器

        server{
            listen 443 ssl;
            server_name debug.siguoya.name;
            ssl_certificate /etc/nginx/debug.siguoya.name.crt;
            ssl_certificate_key /etc/nginx/debug.siguoya.name.key;
            location / {
                root /path/to/project;
                index index.html;
            }
        }
    

    上述配置对于crt证书、pem证书,都适用。配置完之后,需要 nginx -s stop && nginx。如果访问时报错 https Connection refused,可以使用nmap检查一下服务器是否开放了443端口。

    配置完成之后,发现每次重启nginx,都会要求我们输入证书的密码,这个可以通过如下方式来解决

    openssl rsa -in ./debug.siguoya.name.key -out ./debug.siguoya.name.nopass.key
    

    然后修改证书文件为免密码证书文件

    ssl_certificate_key /etc/nginx/debug.siguoya.name.nopass.key;
    

    生成符合苹果要求的证书

    检测地址1:https://myssl.com/ats.html
    检测地址2:https://www.qcloud.com/product/ssl#userDefined10

    • 不低于TLS v1.2,这个需要用到版本大于 1.0.2 的openssl
    • sha256以上的哈希算法签名
    • RSA 2048 或 ECC 256以上的公钥算法
    • 使用前向加密技术

    升级 centos7 默认的 openssl 1.0.1 版本

    wget https://www.openssl.org/source/openssl-1.0.2k.tar.gz
    tar -zxvf openssl-1.0.2k.tar.gz
    cd openssl-1.0.2k
    ./config --prefix=/usr/local/openssl/1.0.2
    make && make install
    mv /usr/bin/openssl /usr/bin/openssl.bak
    mv /usr/include/openssl /usr/include/openssl.bak
    ln -s /usr/local/openssl/1.0.2/bin/openssl /usr/bin/openssl
    ln -s /usr/local/openssl/1.0.2/include/openssl /usr/include/openssl
    echo '/usr/local/openssl/1.0.2/lib' >> /etc/ld.so.conf
    ldconfig -v
    openssl version
    
    #查看openssl版本,OpenSSL 1.0.1e-fips 11 Feb 2013
    openssl version
    #查看哈希算法签名,sha1WithRSAEncryption
    openssl x509 -noout -text -in ./debug.siguoya.name.crt
    

    如果生成crt文件时,直接使用keyout选项,则无需在 nginx 重启的时候,输入证书密码

    openssl req -x509 -days 3650 -sha256 -nodes -newkey rsa:2048 -keyout debug.siguoya.name.key -out debug.siguoya.name.crt
    

    https常用配置及优化

    keepalive_timeout 100;
    ssl_session_cache shared:SSL:1m;
    ssl_session_timeout 5m;
    ssl_ciphers HIGH:!aNULL:!MD5;
    ssl_prefer_server_ciphers on;
    

    强制HTTPS

    如果我们不想让用户通过HTTP来访问,则可以通过如下配置来强制HTTP访问为HTTPS访问

    server {
        listen 80;
        server_name debug.siguoya.name;
        location / {
            return 301 https://debug.siguoya.name$request_uri;
        }
    }
    

    查看centos系统已安装的根证书

    ##查看证书列表
    openssl crl2pkcs7 -nocrl -certfile /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem | openssl pkcs7 -print_certs -text -noout
    
    #查看证书Common Name列表
    openssl crl2pkcs7 -nocrl -certfile /etc/pki/ca-trust/extracted/pem/tls-ca-bundle.pem | openssl pkcs7 -print_certs -text -noout | grep 'CN=' | grep 'Subject'
    

    专题阅读

  • 相关阅读:
    学习WCF4.0
    C++/CLI几点说明:
    微软StockTrader 4.0 学习笔记配置服务实现示例指南(二)
    PHP register_shutdown_function函数详解
    ASP.NET 中做柱状图和饼图
    如何获取重载了operator &的类对象的实际地址?
    面试题:按单词逆转字符串(2010/10/09)
    如何让模板类跟友元模板函数一一对应起来
    非递归中序输出二叉搜索树
    卖电影票
  • 原文地址:https://www.cnblogs.com/zy108830/p/12600381.html
Copyright © 2011-2022 走看看