17@https
HTTPS
一、.HTTPS基本概述
为什么需要使用HTTPS,因为HTTP不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。
那么我们在实现https时,需要了解ssl协议,但我们现在使用的更多的是TLS加密协议。
那么TLS是怎么保证明文消息被加密的呢?在OSI七层模型中,应用层是http协议,那么在应用层协议之下,我们的表示层,是ssl协议所发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,是应用层http协议没有感知的情况下做到了数据的安全加密
二、HTTPS证书下发流程

我们首先需要申请证书,先去登记机构进行身份登记,我是谁,我是干嘛的,我想做什么,然后登记机构再通过CSR发给CA,CA中心通过后会生成一堆公钥和私钥,公钥会在CA证书链中保存,公钥和私钥证书我们拿到后,会将其部署在WEB服务器上
1.当浏览器访问我们的https站点时,他会去请求我们的证书
2.Nginx这样的web服务器会将我们的公钥证书发给浏览器
3.浏览器会去验证我们的证书是否合法有效
4.CA机构会将过期的证书放置在CRL服务器,CRL服务的验证效率是非常差的,所以CA有推出了OCSP响应程序,OCSP响应程序可以查询指定的一个证书是否过去,所以浏览器可以直接查询OSCP响应程序,但OSCP响应程序性能还不是很高
5.Nginx会有一个OCSP的开关,当我们开启后,Nginx会主动上OCSP上查询,这样大量的客户端直接从Nginx获取证书是否有效

三、模拟网站劫持
1、配置劫持的网站
#配置一个我的网站
[root@web01 conf.d]# vim jc.linux.com.conf
server {
listen 80;
server_name jc.linux.com;
root /code/jc;
index index.html;
charset utf8;
}
#配置自己需要访问的页面
#配置一个我的网站
[root@web01 conf.d]# vim jc.linux.com.conf
server {
listen 80;
server_name jc.linux.com;
root /hzl/jc;
index index.html;
charset utf8;
}
#配置一个我得页面
[root@web01 conf.d]# vim /hzl/jc/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>我是一个标签</title>
</head>
<body>
<article>
<header>
<h1>我是linhaoda</h1>
<p>创建时间:<time pubdate="pubdate">2020-03-04</time></p>
</header>
<p>
<b>Aticle</b>很怕被别人劫持,好他*的紧张...
</p>
<footer>
<p><small>最终解释权归我所有!</small></p>
</footer>
</article>
</body>
</html>
#配置本地host域名解析
#检查配置文件重启nginx
[root@web01 conf.d]# nginx -t
[root@web01 conf.d]# systemctl restart nginx
2、配置劫持网站的服务器
[root@lb01 conf.d]# vim jc.linux.com.conf
server {
listen 80;
server_name jc.linux.com;
location / {
proxy_pass http://10.0.0.7;
proxy_set_header HOST $http_host;
sub_filter '我是一个标签' '性感荷官在线发牌';
sub_filter '<b>Aticle</b>很怕被别人劫持,好他*的紧张...' '<img src="https://www.mumusir.com/download/pic/xingganheguan.gif">';
sub_filter '最终解释权归我所有' '彪悍的人生不需要解释';
}
}
四、证书类型介绍
| 对比 | 域名型 DV | 企业型 OV | 增强型 EV |
|---|---|---|---|
| 绿色地址栏 | 小锁标记+https | 小锁标记 | 小锁标记+企业名称+https |
| 一般用途 | 个人站点和应用; 简单的https加密需求 | 电子商务站点和应用; 中小型企业站点 | 大型金融平台; 大型企业和政府机构站点 |
| 审核内容 | 域名所有权验证 | 全面的企业身份验证; 域名所有权验证 | 最高等级的企业身份验证; 域名所有权验证 |
| 颁发时长 | 10分钟-24小时 | 3-5个工作日 | 5-7个工作日 |
| 单次申请年限 | 1年 | 1-2年 | 1-2年 |
| 赔付保障金 | —— | 125-175万美金 | 150-175万美金 |
1、证书购买选择
1.保护一个域名 www.mumusir.com
2.保护多个域名 www. test. cdn. image. class.
3.保护通配符域名 *.mumusir.com
2、HTTPS证书注意事项
1.https不支持续费,证书到期需要重新申请并进行替换
2.https不支持三级域名解析,如 test.m.haoda.com
3.https显示绿色,说明整个网站的url都是https的
4.https显示黄色,因为网站代码中包含http的不安全链接
5.https显示红色,那么证书是假的或者证书过期。
五、单台机器HTTPS证书的配置
1、检查ssl证书的可用模块
[root@web01 ~]# nginx -V
........
--with-http_ssl_module
....
2、创建证书存放的目录(可以默认位置创建)
[root@web01 ~]# mkdir /etc/nginx/ssl_key
[root@web01 ~]# cd /etc/nginx/ssl_key/
3、证书生成
#秘钥生成
openssl genrsa -idea -out server.key 2048
#证书生成
openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
# req --> 用于创建新的证书
# new --> 表示创建的是新证书
# x509 --> 表示定义证书的格式为标准格式
# key --> 表示调用的私钥文件信息
# out --> 表示输出证书文件信息
# days --> 表示证书的有效期
[root@web01 ssl_key]# openssl genrsa -idea -out server.key 2048
Generating RSA private key, 2048 bit long modulus
..................................................................................................................................+++
...............................................................................+++
e is 65537 (0x10001)
Enter pass phrase for server.key:
Verifying - Enter pass phrase for server.key:
[root@web01 ssl_key]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
Generating a 2048 bit 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) [XX]:China
string is too long, it needs to be less than 2 bytes long
Country Name (2 letter code) [XX]:zg
State or Province Name (full name) []:riben
Locality Name (eg, city) [Default City]:bali
Organization Name (eg, company) [Default Company Ltd]:oldboy
Organizational Unit Name (eg, section) []:oldboy
Common Name (eg, your name or your server's hostname) []:maliao
Email Address []:123@qq.com
You have new mail in /var/spool/mail/root
[root@web01 ssl_key]#
[root@web01 ssl_key]# ll
total 8
-rw-r--r-- 1 root root 1375 Mar 5 15:15 server.crt #证书文件
-rw-r--r-- 1 root root 1704 Mar 5 15:15 server.key #秘钥文件
4、证书语法配置
#启动ssl功能
Syntax: ssl on | off; #语法
Default: ssl off;
Context: http, server #使用环境
#证书文件
Syntax: ssl_certificate file;
Default: —
Context: http, server
#私钥文件
Syntax: ssl_certificate_key file;
Default: —
Context: http, server
5、nginx证书配置
#配置nginx
[root@web01 ssl_key]# vim /etc/nginx/conf.d/s.linux.com.conf
server {
listen 443 ssl; #默认ssl使用端口
server_name s.linux.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
root /hzl/https;
index index.html;
}
}
server {
listen 80;
server_name s.linux.com;
rewrite (.*) https://$server_name$1 redirect;
#return 302 https://$server_name$request_uri;
}
#配置站点
[root@web01 ssl_key]# echo "web01-https" > /hzl/https/index.html
#检查配置文件并重启nginx
[root@web01 ssl_key]# mginx -t
[root@web01 ssl_key]# systemctl restart nginx
[root@web01 ssl_key]# nginx -s reload
6、配置本地host文件,测试
六、Nginx集群实现HTTPS

1、环境准备
| 主机 | 外网IP | 内网IP | 身份 |
|---|---|---|---|
| lb01 | 172.168.15.4 | 172.16.1.4 | 负载均衡 |
| web01 | 172.168.15.7 | 172.16.1.7 | web服务器 |
| web02 | 172.168.15.8 | 172.16.1.8 | web服务器 |
2.配置web服务器(两台)
#创建配置文件
[root@web01 conf.d]# vim linux.com.conf
server {
listen 80;
server_name s.linux.com;
location / {
root /hzl/https;
index index.html;
}
}
[root@web01 conf.d]# systemctl restart nginx
3、创建配置文件(推送)
#同步配置文件
[root@web01 conf.d]# scp linux.com.conf 172.16.1.8:/etc/nginx/conf.d/
#配置站点目录文件
[root@web01 conf.d]# mkdir /hzl/https #web01创建站点目录
[root@web01 conf.d]# echo "web01-https" > /hzl/https/index.html
[root@web02 conf.d]# mkdir /code/https
[root@web02 conf.d]# echo "https2222" > /hzl/https/index.html
[root@web01 conf.d]# chown -R www.www /hzl/https/
[root@web02 conf.d]# chown -R www.www /hzl/https/
4、上传证书文件(推送)
[root@web01 conf.d]# scp -r /etc/nginx/ssl_key 172.16.1.4:/etc/nginx/
5、配置负载均衡机器nginx
[root@lb01 conf.d]# vim linux.com.conf
upstream webserver {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 443 ssl;
server_name linux.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://webserver;
proxy_set_header host $http_host;
}
}
server {
listen 80;
server_name linux.com;
return 302 https://$server_name$request_uri;
}
七、全站项目集群实现https
1、配置web端博客nginx配置文件
[root@web01 conf.d]# vim blog.linux.com.conf
server {
listen 80;
server_name blog.linux.com;
location / {
root /code/wordpress;
index index.php;
}
location ~* \.php$ {
root /code/wordpress;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# scp blog.linux.com.conf 172.16.1.8:/etc/nginx/conf.d/
2、配置web端知乎的配置文件
[root@web01 conf.d]# vim zh.linux.com.conf
server {
listen 80;
server_name zh.linux.com;
location / {
root /hzl/wecenter;
index index.php;
}
location ~* \.php$ {
root /hzl/wecenter;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
#推送配置文件
[root@web01 conf.d]# scp zh.linux.com.conf 172.16.1.8:/etc/nginx/conf.d/
3、配置负载均衡
[root@lb01 conf.d]# vim proxy_https.conf
upstream web {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 443 ssl;
server_name blog.linux.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://web;
include proxy_params;
}
}
server {
listen 80;
server_name blog.linux.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name zh.linux.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://web;
include proxy_params;
}
}
server {
listen 80;
server_name zh.linux.com;
return 302 https://$server_name$request_uri;
}
#配置文件检查并重启nginx
[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
#负载均衡lb配置
[root@lb01 conf.d]# vim proxy_https.conf
upstream web {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 443 ssl;
server_name blog.linux.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://web;
include proxy_params;
}
}
server {
listen 80;
server_name blog.linux.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name zh.linux.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://web;
include proxy_params;
}
}
server {
listen 80;
server_name zh.linux.com;
return 302 https://$server_name$request_uri;
}
#配置文件检查并重启nginx
[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
4、配置hosts访问测试
#页面格式混乱,代理到php的时候开启HTTPS模式
server {
... ...
location ~* \.php$ {
root /code/wecenter;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#开启https模式
fastcgi_param HTTPS on;
include fastcgi_params;
}
}
5.配置web端phpmyadmin
[root@web01 conf.d]# vim phpmyadmin.conf
server {
listen 80;
server_name php.linux.com;
location / {
root /code/phpmyadmin;
index index.php;
}
location ~ \.php$ {
root /code/phpmyadmin;
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
}
[root@web01 conf.d]# scp phpmyadmin.conf 172.16.1.8:/etc/nginx/conf.d/
6.配置负载均衡phpmyadmin
[root@lb01 conf.d]# vim phpmyadmin_proxy.conf
upstream phpmyadmin {
server 10.0.0.7;
server 10.0.0.8;
}
server {
listen 443 ssl;
server_name php.linux.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://phpmyadmin;
include proxy_params;
}
}
server {
listen 80;
server_name php.linux.com;
return 302 https://$server_name$request_uri;
}
[root@lb01 conf.d]# systemctl restart ngin
本文来自博客园,作者:ଲ小何才露煎煎饺,转载请注明原文链接:https://www.cnblogs.com/zeny/p/15121563.html

小锁标记+https
小锁标记
小锁标记+企业名称+https
浙公网安备 33010602011771号