kylin-Nginx的rewrite跳转模块
Nginx的rewrite跳转模块
一、使用场景
1、地址跳转,用户访问dezyan.cn这个URL时,将其定向至一个新的域名ding.cn
2、协议跳转,用户通过http协议请求网站时,将其重新跳转至https协议方式
3、伪静态,将动态页面显示为静态页面方式的一种技术,便于搜索引擎的录入,同时减少动态URL地址对外暴露过多的参数,提升更高的安全性。
4、搜索引擎,SEO优化依赖于url路径,好记的url便于支持搜索引擎录入
二、配置方法
- 语法格式
rewrite [正则表达式] [页面位置] [flag标记]
- flag标记
last #本条规则匹配完成后,停止匹配,不再匹配后面的规则
break #本条规则匹配完成后,停止匹配,不再匹配后面的规则
redirect #返回302临时重定向,地址栏会显示跳转后的地址
permanent #返回301永久重定向,地址栏会显示跳转后的地址
- 应用场景
server,location,if
-
示例
- rewrite功能展示
[root@web01 conf.d]# vim bl.conf server{ listen 80; server_name bl.test.com; root /code/bl; location /{ rewrite /1.html /2.html; rewrite /2.html /3.html; } location /2.html{ rewrite /2.html /a.html; } location /3.html{ rewrite /3.html /b.html; } } [root@web01 bl]# echo 1111111 > 1.html [root@web01 bl]# echo 2222222 > 2.html [root@web01 bl]# echo 3333333 > 3.html [root@web01 bl]# echo aaaaaaa > a.html [root@web01 bl]# echo bbbbbbb > b.html 此时: 浏览器测试访问 http://bl.test.com/1.html 返回 bbbbbbb http://bl.test.com/2.html 返回 aaaaaaa http://bl.test.com/3.html 返回 bbbbbbb- break和last的区别展示
server{ listen 80; server_name bl.test.com; root /code/bl; location /{ #rewrite /1.html /2.html last; #rewrite /1.html /2.html break; rewrite /2.html /3.html; } location /2.html{ rewrite /2.html /a.html; } location /3.html{ rewrite /3.html /b.html; } } 当启用last行时,浏览器访问http://bl.test.com/1.html会返回a.html的内容 当启用break行时,浏览器访问http://bl.test.com/1.html会返回2.hmtl的内容 也就是说: last标签表示处理完当前rewrite规则后,会继续向后匹配对应的location继续处理请求; break标签表示处理完当前rewrite规则后,不再进行任何处理请求的操作;- redirect和permanent的区别展示(实现跳转https)
[root@web01 conf.d]# vim rq.conf server { listen 80; server_name rp.test.com; root /code/rp; location /test { rewrite ^(.*)$ https://www.baidu.com redirect; #return 301 https://www.baidu.com; #rewrite ^(.*)$ https://www.baidu.com permanent; #return 302 https://www.baidu.com; } } 如此,无论启用哪行,用户访问rp.test.com/test时都会被跳转到百度页 当启用redirect行时,用户每次访问rp.test.com/test都会询问服务器才跳转到百度页; 当启用permanent行时,用户第一次访问网站,浏览器会记录跳转的地址;第二次就不会再询问服务器,直接通过浏览器缓存跳转;
三、案例
在写rewrite规则之前,我们需要开启rewrite日志对规则的匹配进行调试。
[root@web01 code]# vim /etc/nginx/nginx.conf
/var/log/nginx/error.log notice;
http{
……
rewrite_log on;
……
}
规则匹配跳转
- 用户访问 www.test.com/……abc跳转到www.test.com/ccc/bbb/2.html界面
[root@web01 conf.d]# cat re.conf
server {
listen 80;
server_name www.test.com;
root /code;
location / {
root /code;
index index.html;
}
location ~ abc$ { # 匹配以abc结尾的url
rewrite (.*) /ccc/bbb/2.html redirect;
#return 302 /ccc/bbb/2.html;
}
}
- 用户访问 www.test.com/abc跳转到www.test.com/ccc/bbb/2.html界面
[root@web01 conf.d]# cat re.conf
server {
listen 80;
server_name www.test.com;
root /code;
location / {
root /code;
index index.html;
}
location ~ ^/abc$ { # 必须为abc
rewrite (.*) /ccc/bbb/2.html redirect;
#return 302 /ccc/bbb/2.html;
}
}
资源匹配后向引用跳转
- 资源跳转使用后向引用的方式(当用户访问www.test.com/2024/任意内容时跳转至www.test.com/2018/任意内容)
[root@web01 conf.d]# cat re.conf
server {
listen 80;
server_name www.test.com;
root /code;
location / {
root /code;
index index.html;
}
location /2024 {
rewrite ^/2024/(.*)$ /2018/$1 redirect;
}
}
[root@web01 conf.d]# mkdir /code/2018/11/22/ -p
[root@web01 conf.d]# echo 2018的广告策划.. > /code/2018/11/22/3.html
[root@web01 conf.d]# cat /code/2018/11/22/3.html
2018的广告策划..
#多个后向引用
#例:用户访问www.test.com/2024/10-16.html时跳转至www.test.com/2018/10/16/index.html
[root@web01 conf.d]# vim re.conf
server {
listen 80;
server_name www.test.com;
root /code;
location / {
root /code;
index index.html;
}
location /2024 {
rewrite ^/2024/(.*)-(.*).html$ /2018/$1/$2/index.html redirect;
}
}
判断语句跳转
- 当客户端为10.0.0.1时候,在请求行添加参数
[root@web01 conf.d]# cat re.conf
server {
listen 80;
server_name test.dezyan.com;
# $args为Nginx内置变量请求行的参数
set $args "&showoffline=1";
location / {
root /code;
index index.html;
}
if ($remote_addr = 10.0.0.1 ){ # 判断如果客户端是10.0.0.1 才执行下面的rewrite
rewrite (.*) http://test.dezyan.com$1;
}
}
- 跳转维护页面,指定IP正常访问
[root@web01 conf.d]# cat re.conf
server {
listen 80;
server_name test.dezyan.com;
root /code;
index index.html;
location / {
set $ip 0; # 设置变量为0
if ($remote_addr = "10.0.0.1"){
set $ip 1; # 如果来源IP为0.1则设置为1
}
if ($ip = 0){ # 判断如果变量为0 则跳转维护页面
rewrite ^(.*)$ /wh.html break;
}
}
}
[root@web01 conf.d]# echo "目前网站正在维护中...." > /code/wh.html
将 http 请求跳转到 https
#Nginx跳转配置
server {
listen 80;
server_name www.dezyan.com;
rewrite ^(.*) https://$server_name$1 redirect;
#return 302 https://$server_name$request_uri;
}
server {
listen 443;
server_name www.dezyan.com;
ssl on;
}
错误页跳转
server {
listen 80;
root /code;
location /test {
rewrite (.*) https://www.dezyan10.com redirect;
}
error_page 403 404 500 501 502 @error_test;
location @error_test {
rewrite ^(.*)$ /404.html break;
}
}
配置DNS劫持
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为密钥使用之前约定好的解密算法获取网页内容。
本文来自博客园,作者:丁志岩,转载请注明原文链接:https://www.cnblogs.com/dezyan/p/18784458

浙公网安备 33010602011771号