day38-HTTP
1.每日复盘与今日内容
1.1复盘
- Nginx性能优化
- 压力测试工具
- 系统性能优化
- 代理服务优化
- 静态资源优化🍟🍟🍟🍟🍟
- 文件高效传输
- 静态资源压缩🍟🍟🍟🍟🍟
- 防止资源盗链🍟🍟🍟🍟🍟
- 跨域访问
- CPU亲和
1.2今日内容
- HTTP证书、加密流程🍟🍟🍟🍟🍟
- 模拟网站篡改
- 部署自签证书、部署真实的证书🍟🍟🍟🍟🍟
- wordpress实现集群HTTPS
- https优化参数
2.HTTP证书
- HTTPS安全证书基本概述
为什么需要使用HTTPS,因为HTTP不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。
那么我们在实现https时,需要了解ssl协议,但我们现在使用的更多的是TLS加密协议。
那么TLS是怎么保证明文消息被加密的呢?在OSI七层模型中,应用层是http协议,那么在应用层协议之下,我们的表示层,是ssl协议所发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,是应用层http协议没有感知的情况下做到了数据的安全加密
3.HTTP加密流程
- 流程
1、浏览器发起往服务器的443端口发起请求,请求携带了浏览器支持的加密算法和哈希算法。
2、服务器收到请求,选择浏览器支持的加密算法和哈希算法。
3、服务器下将数字证书返回给浏览器,这里的数字证书可以是向某个可靠机构申请的,也可以是自制的。
4、浏览器进入数字证书认证环节,这一部分是浏览器内置的TLS完成的:
4.1 首先浏览器会从内置的证书列表中索引,找到服务器下发证书对应的机构,如果没有找到,此时就会提示用户该证书是不是由权威机构颁发,是不可信任的。如果查到了对应的机构,则取出该机构颁发的公钥。
4.2 用机构的证书公钥解密得到证书的内容和证书签名,内容包括网站的网址、网站的公钥、证书的有效期等。浏览器会先验证证书签名的合法性(验证过程类似上面Bob和Susan的通信)。签名通过后,浏览器验证证书记录的网址是否和当前网址是一致的,不一致会提示用户。如果网址一致会检查证书有效期,证书过期了也会提示用户。这些都通过认证时,浏览器就可以安全使用证书中的网站公钥了。
4.3 浏览器生成一个随机数R,并使用网站公钥对R进行加密。
5、浏览器将加密的R传送给服务器。
6、服务器用自己的私钥解密得到R。
7、服务器以R为密钥使用了对称加密算法加密网页内容并传输给浏览器。
8、浏览器以R为密钥使用之前约定好的解密算法获取网页内容。
4.模拟网站篡改
#配置WEB01正常访问的网站
[root@web01 conf.d]# cat test.conf
server {listen 80;server_name www.lzy.com;root /code/test;index index.html;charset utf-8;}
[root@web01 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@web01 conf.d]# systemctl start nginx
[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>windows的hosts解析
10.0.0.7 www.lzy.com
#WEB02配置为劫持的服务器
[root@web02 conf.d]# cat jc.conf
upstream jiechi {server 10.0.0.7:80;
}server {listen 80;server_name www.lzy.com;location / {proxy_pass http://jiechi;proxy_set_header Host $http_host;sub_filter '<h1>我是妹妹' '<h1>我是哥哥';sub_filter '<small>版权所有' ' <small>开源';}
}
[root@web02 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@web02 conf.d]# systemctl restart nginxwindows的hosts劫持
10.0.0.8 www.lzy.com
5.证书类型介绍
对比 | 域名型DV | 企业型OV | 增强型EV |
绿色地址栏 | 小锁标记+https | 小锁标记+https | 小锁标记+企业名称+https |
一般用途 | 个人站点和应用; 简单的https加密需求 | 电子商务站点和应用; 中小型企业站点 | 大型金融平台; 大型企业和政府机构站点 |
审核内容 | 域名所有权验证 | 全面的企业身份验证; 域名所有权验证 | 最高等级的企业身份验证; 域名所有权验证 |
颁发时长 | 几分钟 | 3-5个工作日 | 5-7个工作日 |
单次申请年限 | 3个月 | 1-2年(1年一换) | 1-2年 |
赔付保障金 | —— | 125-175万美金 | 150-175万美金 |
- 保护域名的方式
支持单个域名: www.oldboy.com
支持多个域名: www.oldboy.com test.oldboy.com news.oldboy.com www.test.com
支持通配符域名: *.oldboy.com
一个证书对应一个域名安全
一个证书对应多个域名安全
- 部署证书
1.充当CA机构给自己颁发假证
#生成假证流程--了解
#第一步: 创建存放证书的路径
[root@lb01 ~]# mkdir -p /etc/nginx/ssl_key
[root@lb01 ~]# cd /etc/nginx/ssl_key
[root@lb01 ssl_key]#
[root@web01 ssl_key]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus (2 primes)
......................................+++++
.............................................+++++
e is 65537 (0x010001)
Enter pass phrase for server.key: # 输入密码 1234
Verifying - Enter pass phrase for server.key: # 输入1234#第二步: 生成自签证书内容自定义填写
[root@web01 ssl_key]# openssl req -days 36500 -x509 \
> -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Generating a RSA private key
.............................................................................................................................................+++++
...........+++++
writing new private key to 'server.key'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [AU]:CN
State or Province Name (full name) [Some-State]:BJ
Locality Name (eg, city) []:SH
Organization Name (eg, company) [Internet Widgits Pty Ltd]:oldboy
Organizational Unit Name (eg, section) []:oldboy
Common Name (e.g. server FQDN or YOUR name) []:oldboy
Email Address []:oldboy123@qq.com[root@web01 ssl_key]# ll
total 8
-rw-r--r-- 1 root root 1395 Aug 20 10:14 server.crt
-rw------- 1 root root 1704 Aug 20 10:13 server.key#第三步: 配置证书
[root@web01 conf.d]# cat wp.conf
server {listen 443 ssl;server_name www.wp.com;root /code/wordpress;ssl_certificate ssl_key/server.crt;ssl_certificate_key ssl_key/server.key;location / {index index.php index.html;}location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_keep_conn on; # 开启长连接fastcgi_connect_timeout 60s; # 超时时间include fastcgi_params;}
}[root@web01 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@web01 conf.d]# systemctl restart nginx#配置80跳转443
[root@web01 conf.d]# cat wp.conf
server {listen 443 ssl;server_name www.wp.com;root /code/wordpress;ssl_certificate ssl_key/server.crt;ssl_certificate_key ssl_key/server.key;location / {index index.php index.html;}location ~ \.php$ {fastcgi_pass 127.0.0.1:9000;fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;fastcgi_keep_conn on; # 开启长连接fastcgi_connect_timeout 60s; # 超时时间include fastcgi_params;}
}#配置将用户访问http请求强制跳转https
server {listen 80;server_name www.wp.com;return 302 https://$server_name$request_uri;
}
2.真实的域名+真实的HTTPS证书
#第一步申请和下载真实证书 前提必须有一个备案好的域名
[root@web01 ssl_key]# ll
total 16
-rw-r--r-- 1 root root 4146 Aug 20 10:32 20022642_test.linuxnc.com_nginx.zip[root@web01 ssl_key]# unzip 20022642_test.linuxnc.com_nginx.zip
#将之前的做备份
[root@web01 ssl_key]# mv server.crt server.crt.bak
[root@web01 ssl_key]# mv server.key server.key.bak#将下载的公钥和私钥进行改名(和配置文件一致)
[root@web01 ssl_key]# mv test.linuxnc.com.pem server.crt
[root@web01 ssl_key]# mv test.linuxnc.com.key server.key
[root@web01 ssl_key]# ll
total 24
-rw-r--r-- 1 root root 4146 Aug 20 10:32 20022642_test.linuxnc.com_nginx.zip
-rw-r--r-- 1 root root 3842 Aug 20 10:32 server.crt
-rw-r--r-- 1 root root 1395 Aug 20 10:14 server.crt.bak
-rw-r--r-- 1 root root 1675 Aug 20 10:32 server.key
-rw------- 1 root root 1704 Aug 20 10:13 server.key.bak#第二步: 配置证书
[root@web01 conf.d]# cat test.conf
server {listen 443 ssl;server_name test.linuxnc.com;root /code/test;ssl_certificate ssl_key/server.crt;ssl_certificate_key ssl_key/server.key;location / {index index.php index.html;}}#配置将用户访问http请求强制跳转https
server {listen 80;server_name test.linuxnc.com;return 302 https://$server_name$request_uri;
}
[root@web01 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@web01 conf.d]# systemctl restart nginx
5.wordpress实现集群HTTPS
#第一步: 将证书部署到负载均衡、使用自签证书
[root@lb01 nginx]# scp -r 10.0.0.7:/etc/nginx/ssl_key .Authorized users only. All activities may be monitored and reported.
root@10.0.0.7's password:
total 24
-rw-r--r-- 1 root root 4146 Aug 20 10:41 20022642_test.linuxnc.com_nginx.zip
-rw-r--r-- 1 root root 3842 Aug 20 10:41 server.crt
-rw-r--r-- 1 root root 1395 Aug 20 10:41 server.crt.bak
-rw-r--r-- 1 root root 1675 Aug 20 10:41 server.key
-rw------- 1 root root 1704 Aug 20 10:41 server.key.bak
[root@lb01 nginx]# cd ssl_key/
[root@lb01 ssl_key]# rm -rf server.crt server.key
[root@lb01 ssl_key]# rm -rf 20022642_test.linuxnc.com_nginx.zip
[root@lb01 ssl_key]# ll
total 8
-rw-r--r-- 1 root root 1395 Aug 20 10:41 server.crt.bak
-rw------- 1 root root 1704 Aug 20 10:41 server.key.bak
[root@lb01 ssl_key]# mv server.crt.bak server.crt
[root@lb01 ssl_key]# mv server.key.bak server.key
[root@lb01 ssl_key]# ll
total 8
-rw-r--r-- 1 root root 1395 Aug 20 10:41 server.crt
-rw------- 1 root root 1704 Aug 20 10:41 server.key第二步: 配置负载均衡
[root@lb01 conf.d]# cat wp.conf
upstream web {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;include lv_env;location / {proxy_pass http://web;}}#配置将用户访问http请求强制跳转https
server {listen 80;server_name www.wp.com;return 302 https://$server_name$request_uri;
}携带内容:
[root@lb01 conf.d]# cat ../lv_env proxy_set_header Host $http_host;proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;proxy_http_version 1.1;proxy_connect_timeout 30;proxy_send_timeout 60;proxy_read_timeout 60;proxy_buffering on;proxy_buffer_size 32k;proxy_buffers 4 128k;[root@lb01 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@lb01 conf.d]# systemctl restart nginx#最后配置fstcgi支持https的请求
[root@web01 conf.d]# vim 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;fastcgi_keep_conn on; # 开启长连接fastcgi_connect_timeout 60s; # 超时时间include fastcgi_params;fastcgi_param HTTPS on; # 添加此参数}
}[root@web01 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@web01 conf.d]# systemctl restart nginx#web02
[root@web02 conf.d]# vim 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; # 添加此参数 }
}[root@web02 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@web02 conf.d]# systemctl restart nginxwindows的hsots解析
10.0.0.5 www.wp.com
6.HTTPS优化参数
ssl_session_cache shared:SSL:10m; #在建立完ssl握手后如果断开连接,在session_timeout时间内再次连接,是不需要再次获取公钥建立握手的,可以复用之前的连接ssl_session_timeout 1440m; #ssl连接断开后的超时时间ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4; #配置加密协议ssl_protocols TLSv1.2 TLSv1.3; #使用TLS版本协议ssl_prefer_server_ciphers on; #nginx决定使用哪些协议与浏览器通信[root@lb01 conf.d]# cat wp.conf
upstream web {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 会话缓存,提高性能ssl_session_cache shared:SSL:1m;# 设置 SSL 会话超时时间ssl_session_timeout 5m;# 自定义设置使用的TLS协议的类型以及加密套件(以下为配置示例,请您自行评估是否需要配置)ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;# 指定允许的 TLS 协议版本,TLS协议版本越高,HTTPS通信的安全性越高,但是相较于低版本TLS协议,高版本TLS协议对浏览器的兼容性较差ssl_protocols TLSv1.2 TLSv1.3;# 优先使用服务端指定的加密套件ssl_prefer_server_ciphers on;include lv_env;location / {proxy_pass http://web;}}#配置将用户访问http请求强制跳转https
server {listen 80;server_name www.wp.com;return 302 https://$server_name$request_uri;
}
7.今日总结
- HTTP证书、加密流程🍟🍟🍟🍟🍟
- 模拟网站篡改
- 部署自签证书、部署真实的证书🍟🍟🍟🍟🍟
- wordpress实现集群HTTPS
- https优化参数