为什么需要使用HTTPS,因为HTTP不安全,当我们使用http网站时,会遭到劫持和篡改,如果采用https协议,那么数据在传输过程中是加密的,所以黑客无法窃取或者篡改数据报文信息,同时也避免网站传输时信息泄露。
那么我们在实现https时,需要了解ssl协议,但我们现在使用的更多的是TLS加密协议。
那么TLS是怎么保证明文消息被加密的呢?在OSI七层模型中,应用层是http协议,那么在应用层协议之下,我们的表示层,是ssl协议所发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,是应用层http协议没有感知的情况下做到了数据的安全加密
我们首先需要申请证书,先去登记机构进行身份登记,我是谁,我是干嘛的,我想做什么,然后登记机构再通过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获取证书是否有效
#配置一个我的网站
[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 /code/jc/index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>LOVE YOU</title>
<script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
<link rel="stylesheet" href="http://apps.bdimg.com/libs/bootstrap/3.3.4/css/bootstrap.min.css" >
<script src="http://apps.bdimg.com/libs/bootstrap/3.3.4/js/bootstrap.min.js" ></script>
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
</head>
<body>
<div class="container">
<div class="row col-md-3"></div>
<div class="row col-md-6" >
<div class="row question">
<div class="col-md-6 col-xs-12">
<p style="line-height: 50px;font-size: 10px;">“小萍萍,我观察你很久了”</p>
<p style="line-height: 50px;font-size: 20px;">做我女朋友好不好?</p>
</div>
<div class="col-md-6 col-xs-12">
<img src="http://ozef40uqu.bkt.clouddn.com/1.png" alt="" style="height: 200px;">
</div>
</div>
<div class="row question" style="margin-top: 20px;">
<div class="col-md-6 col-xs-6" style="text-align: center;">
<button type="button" class="btn btn-success" style="width: 80%" id="no">好</button>
</div>
<div class="col-md-6 col-xs-6" style="text-align: center;">
<button type="button" class="btn btn-danger" style="width: 80%" id="ok">不好</button>
</div>
</div>
<div class="col-md-12 col-xs-12 hide" id="success">
<img src="http://ozef40uqu.bkt.clouddn.com/666.jpg" alt="" style="width: 100%;">
</div>
</div>
<div class="row col-md-3"></div>
</div>
<script>
var i=1;
var ok=false;
$(document).ready(function(){
$("#no").click(function(){
alert("真的吗?你答应了?");
alert("给我发消息吧,爱你");
$(".question").addClass('hide');
$("#success").removeClass('hide');
ok=true;
});
$("#ok").click(function(){
switch(i){
case 1:
alert("工资上交");
break;
case 2:
alert("家务全包");
break;
case 3:
alert("房产证写你的名字");
break;
case 4:
alert("保大");
break;
case 5:
alert("我妈会游泳");
break;
case 6:
alert("不跟你吵架,会撒娇 会卖萌");
break;
default:
alert("答应我吧");
}
i++;
});
});
</script>
</body>
</html>
[root@web01 conf.d]#chown -R www.www /code/index.html
[root@web01 conf.d]# systemctl restart nginx
#配置本地hosts
192.168.15.7 jc.linux.com
[root@lb01 conf.d]# vim jc.linux.com.conf
server {
listen 80;
server_name jc.linux.com;
location / {
proxy_pass http://192.168.15.7:80;
include proxy_params;
}
}
[root@lb01 conf.d]# systemctl restart nginx
1、篡改hosts测试
192.168.15.7 jc.linux.com
[root@lb01 conf.d]# cat linux12.jc.com.conf
server {
listen 80;
server_name linux12.jc.com;
location / {
proxy_pass http://10.10.0.7:80;
include proxy_params;
sub_filter 'LOVE YOU ' <title>霉霉</title> ;
}
}
## nginx -t检查并重启
[root@web01 ~]# systemctl restart nginx
#配置hosts
192.168.15.7 jc.linux.com
对比 | 域名型 DV | 企业型 OV | 增强型 EV |
---|---|---|---|
绿色地址栏 | 小锁标记+https | 小锁标记+https | 小锁标记+企业名称+https |
一般用途 | 个人站点和应用; 简单的https加密需求 | 电子商务站点和应用; 中小型企业站点 | 大型金融平台; 大型企业和政府机构站点 |
审核内容 | 域名所有权验证 | 全面的企业身份验证; 域名所有权验证 | 最高等级的企业身份验证; 域名所有权验证 |
颁发时长 | 10分钟-24小时 | 3-5个工作日 | 5-7个工作日 |
单次申请年限 | 1年 | 1-2年 | 1-2年 |
赔付保障金 | —— | 125-175万美金 | 150-175万美金 |
1.保护一个域名 www.mumusir.com
2.保护多个域名 www. test. cdn. image. class.
3.保护通配符域名 *.mumusir.com
1.https不支持续费,证书到期需要重新申请并进行替换
2.https不支持三级域名解析,如 test.m.haoda.com
3.https显示绿色,说明整个网站的url都是https的
https显示黄色,因为网站代码中包含http的不安全链接
https显示红色,那么证书是假的或者证书过期。
[root@web01 ~]# nginx -V
--with-http_ssl_module
[root@web01 ~]# mkdir /etc/nginx/ssl_key
[root@web01 ~]# cd /etc/nginx/ssl_key/
[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 []:[email protected]
You have new mail in /var/spool/mail/root
[root@web01 ssl_key]#
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]# 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
#启动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
#配置nginx
[root@web01 ssl_key]# vim /etc/nginx/conf.d/s.linux.com.conf
server {
listen 443 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 /code/https;
index index.html;
}
}
#重启nginx
[root@web01 ssl_key]# systemctl restart nginx
#配置站点
[root@web01 ssl_key]# echo "test https" > /code/https/index.html
10.0.0.7 discuz.linux.com jc.linux.com s.linux.com
主机 | 外网IP | 内网IP | 身份 |
---|---|---|---|
lb01 | 172.168.15.5 | 172.16.1.5 | 负载均衡 |
web01 | 172.168.15.7 | 172.16.1.7 | web服务器 |
web02 | 172.168.15.8 | 172.16.1.8 | web服务器 |
[root@web01 conf.d]# vim s.linux.com.conf
server {
listen 80;
server_name s.linux.com;
location / {
root /code/https;
index index.html;
}
}
[root@web01 conf.d]# systemctl restart nginx
#同步配置文件
[root@web01 conf.d]# scp s.linux.com.conf 172.16.1.8:/etc/nginx/conf.d/
#配置站点目录文件
[root@web01 conf.d]# mkdir /code/https
[root@web01 conf.d]# echo "https1111" > /code/https/index.html
[root@web02 conf.d]# mkdir /code/https
[root@web02 conf.d]# echo "https2222" > /code/https/index.html
[root@web01 conf.d]# chown -R www.www /code/https/
[root@web02 conf.d]# chown -R www.www /code/https/
[root@web01 conf.d]# systemctl restart nginx
#加入hosts 访问
[root@web01 conf.d]# scp -r /etc/nginx/ssl_key 172.16.1.4:/etc/nginx/
[root@lb01 conf.d]# vim s.linux.com.conf
upstream webserver {
server 172.16.1.7:80;
server 172.16.1.8:80;
}
server {
listen 443 ssl;
server_name s.linux.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;
location / {
proxy_pass http://webserver;
}
}
server {
listen 80;
server_name s.linux.com;
return 302 https://$server_name$request_uri;
}
[root@web01 nginx]# mount -t nfs 172.16.1.31:/conf /etc/nginx/conf.d/
[root@web01 web]# mount -t nfs 172.16.1.31:/web /web/
#查看挂载
[root@web01 web]# df -h
Filesystem Size Used Avail Use% Mounted on
172.16.1.31:/conf 99G 2.7G 97G 3% /etc/nginx/conf.d
172.16.1.31:/web 99G 2.7G 97G 3% /web
[root@web01 conf.d]# cat discuz.conf
server {
listen 80;
server_name luntan123.com;
root /web/DiscuzX/upload;
location / {
index index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
#fastcgi_param HTTPS ON;
include fastcgi_params;
}
}
[root@web01 web]# ll
total 16616
drwxrwxrwx 6 www www 95 Apr 23 14:08 DiscuzX
drwxr-xr-x 13 www www 4096 May 6 20:28 phpMyAdmin-5.1.0-all-languages
-rw-rw-r-- 1 www www 17005138 May 6 23:02 web.tar.gz
drwxr-xr-x 5 www www 4096 May 6 23:05 wordpres
#注:如果安装DiscuzX出现不可写,授权777站点目录
#db01增加一个discuz的数据库
#创建用户并登录
[root@db01 ~]# mysqladmin -uroot password '***'
[root@db01 ~]# mysql -uroot -p***
#创建数据库
MariaDB [(none)]> create database discuz;
Query OK, 1 row affected (0.00 sec)
# 创建用户给予web以及其它网站使用
MariaDB [mysql]> grant all privileges on *.* to baimo@'%' identified by 'baimo';
Query OK, 0 rows affected (0.01 sec)
MariaDB [mysql]> flush privileges;#刷新
Query OK, 0 rows affected (0.00 sec)
#按照流程安装
[root@web01 conf.d]# cat discuz.conf
server {
listen 80;
server_name luntan123.com;
root /web/DiscuzX/upload;
rewrite ^([^\.]*)/topic-(.+)\.html$ $1/portal.php?mod=topic&topic=$2 last;
rewrite ^([^\.]*)/article-([0-9]+)-([0-9]+)\.html$ $1/portal.php?mod=view&aid=$2&page=$3 last;
rewrite ^([^\.]*)/forum-(\w+)-([0-9]+)\.html$ $1/forum.php?mod=forumdisplay&fid=$2&page=$3 last;
rewrite ^([^\.]*)/thread-([0-9]+)-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=viewthread&tid=$2&extra=page%3D$4&page=$3 last;
rewrite ^([^\.]*)/group-([0-9]+)-([0-9]+)\.html$ $1/forum.php?mod=group&fid=$2&page=$3 last;
rewrite ^([^\.]*)/space-(username|uid)-(.+)\.html$ $1/home.php?mod=space&$2=$3 last;
rewrite ^([^\.]*)/blog-([0-9]+)-([0-9]+)\.html$ $1/home.php?mod=space&uid=$2&do=blog&id=$3 last;
rewrite ^([^\.]*)/archiver/(fid|tid)-([0-9]+)\.html$ $1/archiver/index.php?action=$2&value=$3 last;
rewrite ^([^\.]*)/([a-z]+[a-z0-9_]*)-([a-z0-9_\-]+)\.html$ $1/plugin.php?id=$2:$3 last;
if (!-e $request_filename) {
return 404;
}
location / {
index index.php;
}
location ~* \.php$ {
fastcgi_pass 127.0.0.1:9000;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
fastcgi_param HTTPS ON; #http返回的给https乱码开启
include fastcgi_params;
}
}
需求分析:
1、有证书ssl
2、负载均衡
3、用户访问(.*)的时候自动跳转到https;
[root@lb01 nginx]# mkdir ssl_key
[root@lb01 nginx]# cd ssl_key/
[root@lb01 ssl_key]# openssl genrsa -idea -out server.key 2048
[root@lb01 ssl_key]# openssl req -days 36500 -x509 -sha256 -nodes -newkey rsa:2048 -keyout server.key -out server.crt
[root@lb01 ssl_key]# ll
-rw-r--r-- 1 root root 1220 May 9 14:57 server.crt
-rw-r--r-- 1 root root 1704 May 9 14:57 server.key
[root@lb01 conf.d]# cat discuz.conf
upstream web {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
} #代理转发的链接池
server {
listen 80;
server_name luntan123.com;
rewrite (.*) https://$server_name$request_uri;
} #监听80端口和域名,任何请求过来都转发给 https://luntan123.com
server {
listen 443 ssl; #http默认端口
server_name luntan123.com;
ssl_certificate /etc/nginx/ssl_key/server.crt;
ssl_certificate_key /etc/nginx/ssl_key/server.key;#证书
location / { #接收到请求后负载均衡转发
proxy_pass http://web;#转发给链接池的ip
proxy_set_header host $http_host;#携带域名一起转发
}
}
[root@lb01 conf.d]# cat /etc/nginx/conf.d/discuz.conf
upstream web {
server 172.16.1.7:80;
server 172.16.1.8:80;
server 172.16.1.9:80;
}
server {
listen 80;
server_name luntan123.com;
return 302 https://$server_name$request_uri;
}
server {
listen 443 ssl;
server_name luntan123.com;
ssl_certificate /ssl_key/server.crt;
ssl_certificate_key /ssl_key/server.key;
location / {
proxy_pass http://web;
include proxy_params;
}
}
#访问成功
==============================================================================================================
问题:因为负载均衡请求web端是http请求,web端请求php也是http格式,php返回的内容就是http格式的内容,我们浏览器请求的是https,所以格式显示错乱,我们需要配置让php返回的格式是https格式
#页面格式混乱,代理到php的时候开启HTTPS模式
[root@web01 ~]# vim /etc/nginx/conf.d/blog.linux.com.conf
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;
}
}
1.购买云主机
2.解析域名
3.申请域名对应的https证书
4.将https证书部署到服务器