Nginx07-nginx中https使用方法、https优化和监控

目录

写在前面

这是Nginx的第七篇,内容是Nginx中如何使用https,以及https的优化和监控

上篇笔记 Nginx06-rewrite模块详解与实验

使用https的步骤

  1. 申请https证书 (ca证书)
    • 可以用公有云(aws、华为云等)申请https证书,个人用户一年有20个免费证书权限。(前提:必须有已经备案通过的域名 && 使用和申请证书的域名必须相同
    • 通过openssl手动创建证书,不过该证书不被认可,会在使用时提示不安全(可以忽略或在浏览器中信任签发证书的主机) #创建私钥

      openssl genrsa -idea -out server.key 2048

      #根据私钥创建 证书

      openssl req -days 36500 -x509 -sha256 -nodes -newkey

      rsa:2048 -keyout server.key -out server.crt

  2. nginx server模块中调用证书 listen 443 ssl;

    ssl_certificate /etc/nginx/ssl_keys/server.pem;

    ssl_certificate_key /etc/nginx/ssl_keys/server.key;

  3. 若有跳转或负载均衡,根据需求修改

单纯使用https

shell 复制代码
# 将http请求重定向到https,$request_uri即携带uri
server {
 listen 80;
 server_name ssl.test.cn; 
 return 301 https://ssl.test.cn$request_uri;
 # 另一种写法
 # rewrite ^(.*)$ https://ssl.test.cn$1 permanent
}
server {
 listen 443 ssl; #指定443承载于ssl之上
  #ssl on ; nginx1.15.0之前需要启用,后续不用
 server_name ssl.test.cn; 
 root /app/code/ssl; 
 #ssl key 调用
 ssl_certificate   /etc/nginx/ssl_keys/ssl.test.cn.pem;
 ssl_certificate_key /etc/nginx/ssl_keys/ssl.test.cn.key;
 location / {
   index index.html;
 }
}

负载均衡和web均用https

  • 整个过程中,lb需要转换http请求重定向为https
  • web仅需处理https请求
shell 复制代码
# web
[root@web01 ~]# cat /etc/nginx/conf.d/ssl.test.cn.conf 
server {
 listen 443 ssl; 
 server_name ssl.test.cn; 
 root /app/code/ssl; 
 ssl_certificate     /etc/nginx/ssl_keys/ssl.test.cn.pem;
 ssl_certificate_key /etc/nginx/ssl_keys/ssl.test.cn.key;
 location / {
   index index.html;
 }
}

# lb
[root@lb01 ~]# cat /etc/nginx/conf.d/ssl.test.cn.conf
upstream ssl_pools {
 server  192.168.100.148:443 ; # 使用https访问,所以指定端口为443
}
server {
   listen 80;
   server_name ssl.test.cn;
   return 301 https://ssl.test.cn$request_uri;
}
server {
   listen 443 ssl;
   server_name ssl.test.cn;
   ssl_certificate     /etc/nginx/ssl_keys/ssl.test.cn.pem;
   ssl_certificate_key /etc/nginx/ssl_keys/ssl.test.cn.key;
   location / {
     proxy_pass https://ssl_pools;
     proxy_set_header Host $http_host;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Real-Ip $remote_addr;
   }
}

负载均衡https,web用http(外网访问https,内网互访http)

shell 复制代码
# web
[root@web01 ~]# cat /etc/nginx/conf.d/ssl.test.cn.conf 
server {
 listen 80; 
 server_name ssl.test.cn; 
 root /app/code/ssl; 
 location / {
   index index.html;
 }
}

# lb
[root@lb01 ~]# cat /etc/nginx/conf.d/ssl.test.cn.conf
upstream ssl_pools {
 server  192.168.100.148:80; # 因为内网使用http互访,所以upstream也只用指定端口80
}
server {
   listen 80;
   server_name ssl.test.cn;
   return 301 https://ssl.test.cn$request_uri; 
}
server {
   listen 443 ssl http2; # 若指定使用http2只需添加该参数
   server_name ssl.test.cn;
   ssl_certificate     /etc/nginx/ssl_keys/ssl.test.cn.pem;
   ssl_certificate_key /etc/nginx/ssl_keys/ssl.test.cn.key;
   location / {
     proxy_pass https://ssl_pools;
     proxy_set_header Host $http_host;
     proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; 
     proxy_set_header X-Real-Ip $remote_addr;
   }
}

部分加密时,访问动态资源会出现问题

对php动态资源的模块添加fastcgi_param HTTPS on;,表明前面部分的请求是https

shell 复制代码
server {
	listen 80;
	server_name blog.test.cn;
	root /app/code/blog;
	error_log /var/log/nginx/blog-error.log notice; 
	access_log /var/log/nginx/blog-access.log main;
	location / {
	 index index.php;
	}
	location ~* \.(html|js|css|jpg|png|jpeg)$ {
	 expires max;
	}
	location ~ \.php$ {
	 fastcgi_pass  127.0.0.1:9000;
	 fastcgi_index index.php; 
	 fastcgi_param HTTPS on;  # 添加该条
	 fastcgi_param SCRIPT_FILENAME  $document_root$fastcgi_script_name;
	 include   fastcgi_params;
	}
}

优化

shell 复制代码
server {
       listen              443 ssl;
       # 会话保持时间
       keepalive_timeout   70;
       #指定ssl加密协议的版本
       ssl_protocols       TLSv1 TLSv1.1 TLSv1.2; 
       #加密算法. 需要排除算法前用!
       #排除null空算法, md5算法
       ssl_ciphers         AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5; 
       ssl_certificate     /usr/local/nginx/conf/cert.pem;
       ssl_certificate_key /usr/local/nginx/conf/cert.key;
        #设置https 会话缓存 10MB大小的缓存空间
       ssl_session_cache   shared:SSL:10m;
        #超时时间 10分钟
       ssl_session_timeout 10m;
       ...
}

监控

监控https证书是否过期

本地证书过期时间

shell 复制代码
openssl x509 -in 证书文件路径 -nnout -dates

任意url的证书

如何获得过期时间

shell 复制代码
# | 默认标准输出,即输出正确内容;curl的过程信息不被认为为正确内容
# |& 错误输出,正确错误内容均输出,可以查看到curl的过程信息
curl -vL https://www.baidu.com |& grep 'expire date'

*  expire date: Aug  6 01:51:05 2024 GMT

# 只要日期信息
[root@e ~]# curl -vL https://www.baidu.com |& grep 'expire date' | awk -F': ' '{print $2}'
Aug  6 01:51:05 2024 GMT

编写脚本,获取过期时间

shell 复制代码
# 编写脚本
[root@ecm-98ce ~]# vim /server/scripts/check_ssl.sh
#!/bin/bash

url=https://www.baidu.com

expire_date_ori=`curl -vL https://www.baidu.com |& grep 'expire date' | awk -F'date:|GMT' '{print $2}'`
expire_date_opt=`date -d "$expire_date_ori" +%F`

echo 原始格式的过期时间 $expire_date_ori
echo 处理后的过期时间   $expire_date_opt


# 测试
[root@ecm-98ce ~]# sh /server/scripts/check_ssl.sh
原始格式的过期时间 Aug 6 01:51:05 2024
处理后的过期时间   2024-08-06

最终脚本,计算过期时间

shell 复制代码
# 编写脚本
[root@ecm-98ce ~]# cat /server/scripts/check_ssl.sh
#!/bin/bash
# author: tassel
# desc: 检查指定url,https证书过期时间

url=https://www.baidu.com

expire_date_ori=`curl -vL https://www.baidu.com |& grep 'expire date' | awk -F'date:|GMT' '{print $2}'`
expire_date_opt=`date -d "$expire_date_ori" +%F`

echo 原始格式的过期时间 $expire_date_ori
echo 处理后的过期时间   $expire_date_opt

# 过期时间-当前时间 秒数
expire_date_opt=`date -d "$expire_date_ori" +%s`
date_now_second=`date +%s`
expire_days=`echo "($expire_date_opt - $date_now_second )/(60*60*24)"|bc`

echo "--------------------"
echo "网站$url证书过期倒计时:还有 $expire_days 天"
echo "网站过期日期是:`date -d "$expire_date_ori" +%F`"

# 测试
[root@ecm-98ce ~]# sh /server/scripts/check_ssl.sh
原始格式的过期时间 Aug 6 01:51:05 2024
处理后的过期时间   2024-08-06
--------------------
网站https://www.baidu.com证书过期倒计时:还有 43 天
网站过期日期是:2024-08-06
相关推荐
一心0924 小时前
ubuntu 20.04.6 sudo 源码包在线升级到1.9.17p1
运维·ubuntu·sudo·漏洞升级
好好学习啊天天向上4 小时前
世上最全:ubuntu 上及天河超算上源码编译llvm遇到的坑,cmake,ninja完整过程
linux·运维·ubuntu·自动性能优化
你想考研啊4 小时前
三、jenkins使用tomcat部署项目
运维·tomcat·jenkins
代码老y5 小时前
Docker:容器化技术的基石与实践指南
运维·docker·容器
典学长编程5 小时前
Linux操作系统从入门到精通!第二天(命令行)
linux·运维·chrome
DuelCode6 小时前
Windows VMWare Centos Docker部署Springboot 应用实现文件上传返回文件http链接
java·spring boot·mysql·nginx·docker·centos·mybatis
你想考研啊8 小时前
四、jenkins自动构建和设置邮箱
运维·jenkins
Code blocks8 小时前
使用Jenkins完成springboot项目快速更新
java·运维·spring boot·后端·jenkins
饥饿的半导体9 小时前
Linux快速入门
linux·运维
还是奇怪11 小时前
Linux - 安全排查 2
linux·运维·安全