12.9rewrite、HTTPS

一、rewrite
1.使用场景
2.rewrite的flag标签
3.伪静态
二、rewrite补充
1.rewrite匹配优先级
1)优先级
2)配置
2.rewrite的环境变量
1) s e r v e r n a m e 2 ) server_name 2) servername2request_filename/ r e q u e s t u r i : 请 求 变 量 3 ) request_uri:请求变量 3) requesturi:3http_host
3.rewrite开启日志
三、HTTPS介绍
1.概述
2.模拟网站劫持
1)配置一个正经网站
2)配置一个正经页面
3)配置劫持的网站
4)篡改hosts测试
5)开始篡改网站
3.HTTPS证书下发流程
4.证书的类型介绍
5.证书购买选择
6.HTTPS证书注意事项
四、单台HTTPS配置
1.检查nginx
2.创建证书存放目录
3.造假证书
1)生成私钥
2)生成公钥
3)查看生成的证书
4.配置证书语法
5.配置nginx证书
6.配置hosts访问
7.配置http自动跳转https
五、全站HTTPS
1.环境准备
2.配置web服务器
1)配置nginx
2)配置站点
3)测试分别访问
3.配置负载均衡
1)配置证书
2)配置nginx
4.配置hosts访问测试
1.配置web端博客配置文件
2.配置负载均衡
3.配置hosts访问测试
4.解决页面格式错乱问题
5.再次访问测试
6.ssl证书优化参数

一、rewrite

1.使用场景

地址跳转
协议跳转
伪静态,seo

2.rewrite的flag标签

在这里插入图片描述

3.伪静态
[root@web01 ~]# vim /etc/nginx/conf.d/linux.discuz.com.conf
server {
    listen 80;
    server_name linux.discuz.com;
    root /code/discuz/upload;

    location / {
        root /code/discuz/upload;
        index index.php;

        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 ^([^\.]*)/(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 ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

二、rewrite补充

1.rewrite匹配优先级

1)优先级
先执行server块的rewrite指令
其次执行location匹配规则
最后执行location中的rewrite
2)配置

server {
    listen 80;
    server_name linux.youxianxji.com;
    
    rewrite (.*) http://www.baidu.com;
    
    location / {
    	rewrite (.*) http://www.jd.com;
    }
    
    location =/ {
        rewrite (.*) http://www.taobao.com;
    }
}
2.rewrite的环境变量

1)$server_name

$server_name    #当前用户请求的域名

server {
        listen 80;
        server_name linux.test.com;
        rewrite ^(.*)$ https://$server_name$1;
}

2) r e q u e s t f i l e n a m e / request_filename/ requestfilename/request_uri:请求变量

$request_filename 请求的文件路径名(带网站的主目录/code/images/test.jpg)

$request_uri 当前请求的文件路径(不带网站的主目录/images/test.jpg)

#大多数用于http协议转https协议
server {
        listen 80;
        server_name linux.test.com;
        return 302 https://$server_name$request_uri;
}

3)$http_host

#很古董的配置方法
server {
        listen 80;
        server_name www.baidu.com baidu.com www.baidu.cn;
        if ($http_host = baidu.com){
            rewrite (.*) http://www.baidu.com$1;
        }
}

#推荐书写格式
server {
        listen 80;
        server_name baidu.com;
        rewrite (.*) http://www.baidu.com$1;
}
server {
        listen 80;
        server_name www.baidu.com;
        location / {...}
}
3.rewrite开启日志
[root@web01 ~]# vim /etc/nginx/nginx.conf
... ...
error_log  /var/log/nginx/error.log notice;
... ...
http {
    ... ...
    rewrite_log on;
    ... ...
}

三、HTTPS介绍

1.概述

为什么需要使用HTTPS,因为HTTP不安全,

那么我们在实现https时,需要了解ssl协议,但我们现在使用的更多的是TLS加密协议。

那么TLS是怎么保证明文消息被加密的呢?在OSI七层模型中,应用层是http协议,那么在应用层协议之下,我们的表示层,是ssl协议所发挥作用的一层,他通过(握手、交换秘钥、告警、加密)等方式,是应用层http协议没有感知的情况下做到了数据的安全加密
在这里插入图片描述

2.模拟网站劫持

1)配置一个正经网站

[root@web01 ~]# vim /etc/nginx/conf.d/linux.jc.com.conf
server {
    listen 80;
    server_name linux.jc.com;

    location / {
        root /code;
        index index.html;
    }
}

[root@web01 ~]# systemctl restart nginx

2)配置一个正经页面

[root@web01 ~]# vim /code/index.html
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>学生信息注册页面</title>
</head>
<body>
<h3 align="center">学生信息注册</h3>
<form  name="stu"action="">
<table>
  <tr><td>姓名:</td><td><input type="text"name="stuName"/></td></tr>
  <tr><td>性别:</td>
      <td><input type="radio"name="stuSex"checked="checked">男
          <input type="radio"name="stuSex">女
          </td>
          </tr>
   <tr><td>出生日期</td>
       <td><input type="text"name="stuBirthday"></td>
       <td>按格式yyyy-mm-dd</td>
       </tr>
       <tr><td>学校:</td><td><input type="text"name="stuSchool"></td></tr>
       <tr><td>专业:</td>
           <td><select name="stuSelect2">
               <option selected>计算机科学与技术</option>
               <option>网络工程</option>
               <option>物联网工程</option>
               <option>应用数学</option>
               </select>
               </td>
               </tr>
               <tr><td>体育特长:</td>
                   <td colspan="2">
                      <input type="checkbox"name="stuCheck" >篮球
                      <input type="checkbox"name="stuCheck" >足球
                      <input type="checkbox"name="stuCheck" >排球
                      <input type="checkbox"name="stuCheck" >游泳
                   </td> 
               </tr>
               <tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr>
               <tr><td>密码:</td><td><input type="password"name="stuPwd" ></td></tr>
               <tr><td>个人介绍:</td>
                   <td colspan="2"><textarea name="Letter"rows="4"cols="40"></textarea></td>
               </tr>
               <tr>
                 <td><input type="submit"value="提交" ><input type="reset"value="取消" ></td>
                 </tr>
                 </table>
                 </form>
</body>
</html>

[root@web01 ~]# chown -R www.www /code/index.html

3)配置劫持的网站

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.jc.com.conf
server {
    listen 80;
    server_name linux.jc.com;

    location / {
        proxy_pass http://10.0.0.7:80;
        include proxy_params;
    }
}

[root@lb01 ~]# systemctl restart nginx

4)篡改hosts测试

10.0.0.4 linux.jc.com

5)开始篡改网站

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.jc.com.conf 
server {
    listen 80;
    server_name linux.jc.com;

    location / {
        proxy_pass http://10.0.0.7:80;
        include proxy_params;

        sub_filter '<title>学生信息注册页面</title>' '<title>澳门首家线上赌场</title>';
        sub_filter '<h3 align="center">学生信息注册</h3>' '<h3 align="center">VIP用户信息注册</h3>';
        sub_filter '<tr><td>性别:</td>' '<tr><td>爱好:</td>';
        sub_filter '<option selected>计算机科学与技术</option>' '<option selected>按摩</option>';
        sub_filter '<option>网络工程</option>' '<option>抽烟</option>';
        sub_filter '<option>物联网工程</option>' '<option>喝酒</option>';
        sub_filter '<option>应用数学</option>' '<option>烫头</option>';
        sub_filter '<tr><td>上传照片:</td><td colspan="2"><input type="file" ></td></tr>' '<img src="https://blog.driverzeng.com/zenglaoshi/xingganheguan.gif">';
    }
}

[root@lb01 ~]# systemctl restart nginx
3.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获取证书是否有效
6.浏览器再次访问的时候,web服务器会将证书和验证结果一起发给浏览器,浏览器直接与我们建立连接

#加密流程
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.证书的类型介绍

在这里插入图片描述

5.证书购买选择
1.保护单个域名 www.mumusir.com
2.保护五个域名 www images cdn test m
3.通配符域名 *.linux.com
6.HTTPS证书注意事项
1.https证书不支持续费,证书到期需要重新申请并进行替换
2.https不支持三级域名解析,如 test.m.linux.com
3.https显示绿色,说明整个网站的url都是https的
4.https显示黄色,因为网站代码中包含http的不安全链接
5.https显示红色,那么证书是假的或者证书过期。

四、单台HTTPS配置

1.检查nginx
[root@web01 ~]# nginx -V
--with-http_ssl_module
2.创建证书存放目录
[root@web01 ~]# mkdir /etc/nginx/ssl_key
[root@web01 ~]# cd /etc/nginx/ssl_key/
3.造假证书

1)生成私钥

#使用openssl命令充当CA权威机构创建证书(生产不使用此方式生成证书,不被互联网认可的黑户证书)
[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: 123456
Verifying - Enter pass phrase for server.key: 123456

[root@web01 ssl_key]# ll
total 4
-rw-r--r--. 1 root root 1739 Dec  9 11:27 server.key

2)生成公钥

#生成自签证书(公钥),同时去掉私钥的密码
[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]:CN
State or Province Name (full name) []:meiguo
Locality Name (eg, city) [Default City]:riben
Organization Name (eg, company) [Default Company Ltd]:heishoudang
Organizational Unit Name (eg, section) []:oldboy
Common Name (eg, your name or your server's hostname) []:oldboy
Email Address []:123@qq.com

# req  --> 用于创建新的证书
# new  --> 表示创建的是新证书    
# x509 --> 表示定义证书的格式为标准格式
# key  --> 表示调用的私钥文件信息
# out  --> 表示输出证书文件信息
# days --> 表示证书的有效期
# sha256 --> 加密方式

3)查看生成的证书

[root@web01 ssl_key]# ll
total 8
-rw-r--r--. 1 root root 1395 Dec  9 11:31 server.crt
-rw-r--r--. 1 root root 1708 Dec  9 11:31 server.key
4.配置证书语法
#1.开启证书
Syntax:	ssl on | off;
Default:	ssl off;
Context:	http, server

#2.指定证书文件
Syntax:	ssl_certificate file;
Default:	—
Context:	http, server

#3.指定私钥文件
Syntax:	ssl_certificate_key file;
Default:	—
Context:	http, server
5.配置nginx证书
[root@web01 ~]# vim /etc/nginx/conf.d/linux.ssl.com.conf 
server {
    listen 443 ssl;
    server_name linux.ssl.com;
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
        root /code;
        index index.html;
    }
}

[root@web01 ~]# 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 ~]# systemctl restart nginx
6.配置hosts访问
10.0.0.7 linux.ssl.com

访问:https://linux.ssl.com/
7.配置http自动跳转https
[root@web01 ~]# vim /etc/nginx/conf.d/linux.ssl.com.conf 
server {
    listen 443 ssl;
    server_name linux.ssl.com;
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
        root /code;
        index index.html;
    }
}
server {
    listen 80;
    server_name linux.ssl.com;

    rewrite (.*) https://$server_name$1;
    #return 302 https://$server_name$request_uri;
}

[root@web01 ~]# 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 ~]# systemctl restart nginx

五、全站HTTPS

1.环境准备

在这里插入图片描述

2.配置web服务器

1)配置nginx

[root@web01 ~]# vim /etc/nginx/conf.d/linux.https.com.conf
server {
    listen 80;
    server_name linux.https.com;

    location / {
        root /code/https;
        index index.html;
    }
}
[root@web01 ~]# systemctl restart nginx

[root@web02 ~]# vim /etc/nginx/conf.d/linux.https.com.conf
server {
    listen 80;
    server_name linux.https.com;

    location / {
        root /code/https;
        index index.html;
    }
}
[root@web01 ~]# systemctl restart nginx

2)配置站点

[root@web01 ~]# mkdir /code/https
[root@web01 ~]# echo "web0111111111111111111" > /code/https/index.html
[root@web01 ~]# chown -R www.www /code/https/

[root@web02 ~]# mkdir /code/https
[root@web02 ~]# echo "web022222222222222222" > /code/https/index.html
[root@web02 ~]# chown -R www.www /code/https/

3)测试分别访问

10.0.0.8 linux.https.com
10.0.0.7 linux.https.com
3.配置负载均衡

1)配置证书

[root@web01 ~]# scp -r /etc/nginx/ssl_key 172.16.1.4:/etc/nginx/

2)配置nginx

[root@lb01 ~]# vim /etc/nginx/conf.d/linux.https.com.conf
upstream web_https {
    server 172.16.1.7:80;
    server 172.16.1.8:80;
}
server {
    listen 80;
    server_name linux.https.com;

    rewrite (.*) https://$server_name$1;
}
server {
    listen 443 ssl;
    server_name linux.https.com;
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
        proxy_pass http://web_https;
        include proxy_params;
    }
}

[root@lb01 ~]# 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 ~]# systemctl restart nginx
4.配置hosts访问测试
10.0.0.4 linux.https.com

六、项目全站HTTPS

1.配置web端博客配置文件
[root@web01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf 
server {
    listen 80;
    server_name linux.wp.com;
    
    location / {
        root /code/wordpress;
        index index.php;
    }   
    
    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        include fastcgi_params;
    }
}
2.配置负载均衡
[root@lb01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf 
upstream blog {
    server 172.16.1.7;
    server 172.16.1.8;
}

server {
    listen 80;
    server_name linux.wp.com;

    rewrite (.*) https://$server_name$1;
}

server {
    listen 443 ssl;
    server_name linux.wp.com;
    ssl_certificate /etc/nginx/ssl_key/server.crt;
    ssl_certificate_key /etc/nginx/ssl_key/server.key;

    location / {
        proxy_pass http://blog;
        include proxy_params;
    }
}

[root@lb01 ~]# 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 ~]# systemctl restart nginx
3.配置hosts访问测试
10.0.0.4 linux.wp.com

#页面格式乱码

4.解决页面格式错乱问题
#问题:因为负载均衡请求web端是http请求,web端请求php也是http格式,php返回的内容就是http格式的内容,我们浏览器请求的是https,所以格式显示错乱,我们需要配置让php返回的格式是https格式

[root@web01 ~]# vim /etc/nginx/conf.d/linux.wp.com.conf 
server {
    listen 80;
    server_name linux.wp.com;

    location / {
        root /code/wordpress;
        index index.php;
    }

    location ~* \.php$ {
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_param SCRIPT_FILENAME /code/wordpress/$fastcgi_script_name;
        #开启https模式
        fastcgi_param HTTPS on;
        include fastcgi_params;
    }
}

[root@web01 ~]# 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 ~]# systemctl restart nginx
5.再次访问测试
6.ssl证书优化参数
server {
    listen 443 default_server;
    server_name blog.driverzeng.com driverzeng.com;
    ssl on;
    root /var/www/wordpress;
    index index.php index.html index.htm;
    ssl_certificate   ssl/215089466160853.pem;
    ssl_certificate_key  ssl/215089466160853.key;
    
    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 TLSv1.1 TLSv1.2;  #使用TLS版本协议
    ssl_prefer_server_ciphers on;  #nginx决定使用哪些协议与浏览器通信
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值