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'

专题阅读

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 浏览器地址栏标志着 HTTPS 的绿色小锁头从心理层面上可以给用户专业安全的心理暗示,本文简单总结一下如何在 Ng...
    QieZi阅读 38,581评论 0 10
  • 摘要 本文主要是在测试https服务时对搭建https服务器的一些实践总结。TLS协议的介绍部分来源于对RFC52...
    东方胖阅读 8,680评论 0 10
  • 1.每一天,都是崭新的一天。 2.不要放弃曾经坚持的梦想。 3.来者要惜,去者要放,多点真诚,少点套路和假装。 4...
    说你呢阅读 305评论 0 0
  • 不二微岩:《一地鸡毛与码字》 今天看到一位写作班同学写的一段话: 生活就算是一地鸡毛,也要从中找到最漂亮的羽毛,做...
    岩不二阅读 191评论 0 0
  • 《爱的教育》,我是一口气读完的,虽然我没有流泪,可是我的心已经承认这是一本洗涤心灵的书籍。吸引我的,似乎并不是其文...
    鱼小升阅读 376评论 0 1

友情链接更多精彩内容