kylin-https证书的配置

https及其证书

模拟网站篡改

web01

[root@web01 conf.d]# cat test.conf 
server {
         listen 80;
         server_name test.dezyan.com;
         root /code/test;
         index index.html;
 }
 
[root@web01 conf.d]# ll /code/test/

[root@web01 conf.d]# cat /code/test/index.html
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我是title</title>
</head>
<body>
<article>
  <header>
    <h1>我是妹妹</h1>
    <p>创建时间:<time pubdate="pubdate">2025/5/20</time></p>
  </header>
  <p>
    <b>Aticle</b>第一次用h5写文章,好他*的紧张...
  </p>
  <footer>
    <p><small>版权所有!</small></p>
  </footer>
</article>
</body>
</html>

web02配置劫持test.dezyan.com

[root@web02 conf.d]# cat jc.conf 
upstream jiechi {
        server 10.0.0.7:80;
}

server {
        listen 80;
        server_name test.dezyan.com;

        location / {
                proxy_pass http://jiechi;
                proxy_set_header Host $http_host;
                sub_filter '<h1>我是妹妹' '<h1>我是哥哥';
                #sub_filter '<small>版权所有' ' <small>开源';
        }
}

windows 的本地hosts解析需要

https加密流程

面试题: 说一下https的加密流程
1、浏览器请求网站,网站返回证书信息包含证书公钥。

2、浏览器验证证书的合法性,如果不合法提示警告信息。如果合法

3、浏览器生成一个随机数R,并使用网站公钥对R进行加密。

4、浏览器将加密的R传送给服务器。

5、服务器用自己的私钥解密得到R。

6、服务器以R为密钥使用了对称加密算法加密网页内容并传输给浏览器。

7、浏览器以R为密钥使用之前约定好的解密算法获取网页内容。

http证书

1.生成http证书

服务器本地生成证书(假的,互联网不认可)

第一步:创建存放证书的目录
[root@web01 ~]# mkdir -p /etc/nginx/ssl_key

第二步:进入目录,生成证书
[root@web01 ~]# cd /etc/nginx/ssl_key/
[root@web01 ssl_key]# openssl genrsa -idea -out server.key 2048
	输入密码
	确认密码
#执行完后会生成一个私钥文件
[root@web01 ssl_key]# ll
total 0
-rw------- 1 root root 0 Dec 17 14:48 server.key

[root@web01 ssl_key]# openssl req -days 36500 -x509 \
>-sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
	输入必要信息
#执行完后会生成一个公钥文件
[root@web01 ssl_key]# ll
-rw-r--r-- 1 root root 1334 Dec 17 14:51 server.crt

向CA机构购买证书

可在阿里云-数字证书管理服务处购买

如何查看证书的有效时间?(面试题)

方法1:
在证书购买处查询
方法2:
在服务器使用命令查询
[root@web01 ssl_key]# openssl x509 -in server.crt -noout -enddate
notAfter=Nov 23 00:40:38 2124 GMT

2.单台http证书的配置

第一步:编写配置文件
[root@dzy conf.d]# vim myfile.conf
server {
        listen 443 ssl;
        server_name my.dezyan.cn;
        #指定证书信息和公钥
        ssl_certificate   ssl_key/my.dezyan.cn.pem;
        ssl_certificate_key  ssl_key/my.dezyan.cn.key;
        location / {
                root /code/myfile;
                index index.html;
        autoindex on;
        autoindex_format html;
        autoindex_exact_size off;
        charset utf-8,gbk;
        autoindex_localtime on;
        }
}
#配置将用户访问http请求强制跳转https
server {
        listen 80;
        server_name my.dezyan.cn;
        return 302 https://$server_name$request_uri;
} 

第二步:重启服务
[root@dzy conf.d]# nginx -t 
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@dzy conf.d]# systemctl restart nginx

第三步:浏览器访问测试
https://my.dezyan.cn/

3.集群实现HTTPS证书

web01配置静态页面

server {
        listen 80;
        server_name blog.dezyan.cn;
        location / {
                root /code/test;
                index index.html;
        }
}


web02配置静态页面

server {
        listen 80;
        server_name blog.dezyan.cn;
        location / {
                root /code/test;
                index index.html;
        }
}

LB负载均衡配置证书

[root@lb01 conf.d]# cat test.conf 
upstream web {
     server 172.16.1.7:80;
     server 172.16.1.8:80;
}
server {
	listen 443 ssl;
	server_name blog.dezyan.cn;
	ssl_certificate   ssl_key/server.crt;
    ssl_certificate_key  ssl_key/server.key;
     
     ssl_session_cache shared:SSL:1m;
     ssl_session_timeout 5m;
	 
     #自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)
     #TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差。
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
     ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;

	location / {
	proxy_pass http://web;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
	}
}
server {
        listen 80;
        server_name test.linuxnc.com;
        return 302 https://$server_name$request_uri;
}



#将证书拷贝到LB01服务器
[root@lb01 nginx]# scp -r 10.0.0.7:/etc/nginx/ssl_key .

[root@lb01 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@lb01 nginx]# systemctl restart nginx

hosts解析到10.0.0.5

配置WordPress实现https证书

第一步: 配置负载均衡

[root@lb01 conf.d]# cat wp.conf 
upstream wp {
     server 172.16.1.7:80;
     server 172.16.1.8:80;
}
server {
	listen 443 ssl;
	server_name www.wp.com;
	ssl_certificate   ssl_key/server.crt;
       ssl_certificate_key  ssl_key/server.key;
     
     ssl_session_cache shared:SSL:1m;
     ssl_session_timeout 5m;
	 
     #自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)
     #TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差。
     ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
     ssl_protocols TLSv1.1 TLSv1.2 TLSv1.3;

	location / {
	proxy_pass http://wp;
	include proxy_params;
	proxy_next_upstream error timeout http_500 http_502 http_503 http_504;
	}
}
server {
        listen 80;
        server_name www.wp.com;
        return 302 https://$server_name$request_uri;
}

第二步: WEB01和WEB02开启PHP支持HTTPS

root@web01 conf.d]# cat wp.conf 
server {
        listen 80;
        server_name www.wp.com;
        root /code/wordpress;

        location / {
                index index.php index.html;
        }

        location ~ \.php$ {
                fastcgi_pass 127.0.0.1:9000;
                fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
		include fastcgi_params;
		fastcgi_param HTTPS on;		# 开机PHP对HTTPS的支持
        }
}

阿里云实现https

单台实现

1.购买一台ECS服务器
点点点 按量付费

2.安装Nginx服务
[root@web01 ~]# yum -y install nginx

配置Nginx
[root@web01 nginx]# cat nginx.conf
user nginx;
worker_processes auto;
error_log /var/log/nginx/error.log;
pid /run/nginx.pid;

include /usr/share/nginx/modules/*.conf;

events {
    worker_connections 1024;
}

http {
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';

    access_log  /var/log/nginx/access.log  main;

    sendfile            on;
    tcp_nopush          on;
    tcp_nodelay         on;
    keepalive_timeout   65;
    types_hash_max_size 4096;

    include             /etc/nginx/mime.types;
    default_type        application/octet-stream;

    include /etc/nginx/conf.d/*.conf;

    server {
        listen       80;
        server_name  test.linuxnc.com;
        root         /code;

        include /etc/nginx/default.d/*.conf;

    }

}
[root@web01 nginx]# echo web01... > /code/index.html
[root@web01 nginx]# cat /code/index.html
web01...
[root@web01 nginx]# curl 127.0.0.1
web01...



3.配置DNS域名解析

负载均衡产品配置证书

1.web服务器配置静态页即可
2.购买阿里云slb负载均衡,配置80端口转443规则,添加两台服务器,添加证书
3.DNS域名解析添加负载均衡服务器公网IP
4.访问测试
posted @ 2025-03-21 08:53  丁志岩  阅读(35)  评论(0)    收藏  举报