目录
[3、rewrite 指令](#3、rewrite 指令)
[3.1 正则表达式](#3.1 正则表达式)
[2.基于客户端 IP 访问跳转](#2.基于客户端 IP 访问跳转)
[5.基于目录下所有 php 结尾的文件跳转](#5.基于目录下所有 php 结尾的文件跳转)
[6.基于最普通一条 url 请求的跳转](#6.基于最普通一条 url 请求的跳转)
一、rewrite概述
1、rewrite功能
访问重写 rewrite 是 Nginx HTTP 请求处理过程中的一个重要功能,它是以模块的形式存在于代码中的,其功能是对用户请求的 URI 进行 PCRE 正则重写,然后返回 30× 重定向跳转或按条件执行相关配置。
Nginx服务器利用 ngx_http_rewrite_module 模块解析和处理rewrite请求,此功能依靠 PCRE(perl compatible regular expression),因此编译之前要安装PCRE库,rewrite是nginx服务器的重要功能之一,用于实现URL的重写,URL的重写是非常有用的功能,比如它可以在我们改变网站结构之后,不需要客户端修改原来的书签,也无需其他网站修改我们的链接,就可以设置为自动访问,另外还可以在一定程度上提高网站的安全性。
2、跳转场景
Rewrite 跳转场景主要包括以下几种
- 可以调整用户浏览的 URL,看起来更规范,合乎开发及产品人员的需求
- 为了让搜索引擎搜录网站内容及用户体验更好,企业会将动态 URL 地址伪装成静态地址提供服务
- 网址换新域名后,让旧的访问跳转到新的域名上。例如,访问京东的 360buy.com会跳转到 jd.com
- 根据特殊变量、目录、客户端的信息进行 URL 调整等。
二、标准配置指令
1、rewrite日志记录指令
名称
rewrite 日志记录指令
指令
rewrite_log
作用域
http, server, location
默认值
off
指令值选项
on 或 off
指令说明
当指令值为 on 时,rewrite 的执行结果会以 notice 级别记录到 Nginx 的 error 日志文件中
配置样例如下:
http {
rewrite_log off;
}
2、未初始化变量告警日志记录指令
名称
未初始化变量告警日志记录指令
指令
uninitialized_variable_warn
作用域
http, server, location
默认值
on
指令值选项
on 或 off
指令说明
指令值为 on 时,会将未初始化的变量告警记录到日志中
配置样例如下:
http {
uninitialized_variable_warn off;
}
3、rewrite 指令
通过正则表达式的匹配来改变URI,可以同时存在一个或多个指令,按照顺序依次对URI进行匹配,rewrite主要是针对用户请求的URL或者是URI做具体处理
名称
rewrite 指令
指令
rewrite
作用域
server, location,if
默认值
on
指令值选项
on 或 off
指令说明
对用户的 URI 用正则表达式的方式进行重写,并跳转到新的 URI
配置样例如下:
http {
rewrite ^/users/(.*)$ /show?user=$1 last;
}
rewrite 访问重写是通过 rewrite 指令实现的,rewrite 指令的语法格式如下:
rewrite regex replacement [flag];
注意:
-
regex 是 PCRE 语法格式的正则表达式。
-
replacement 是重写 URI 的改写规则。当改写规则以"http://""https://"或"$scheme"开头时,Nginx 重写该语句后将停止执行后续任务,并将改写后的 URI 跳转返回客户端。
-
flag 是执行该条重写指令后的操作控制符。操作控制符有如下 4 种:
- last:执行完当前重写规则跳转到新的 URI 后继续执行后续操作;
- break:执行完当前重写规则跳转到新的 URI 后不再执行后续操作。不影响用户浏览器 URI 显示;
- redirect:返回响应状态码 302 的临时重定向,返回内容是重定向 URI 的内容,但浏览器网址仍为请求时的 URI;
- permanent:返回响应状态码 301 的永久重定向,返回内容是重定向 URI 的内容,浏览器网址变为重定向的 URI。
rewrite将用户请求的URI基于regex所描述的模式进行检查,匹配到时将其替换为表达式指定的新的URI。如果在同一级配置块中存在多个rewrite规则,那么会自下而下逐个检查;被某条件规则替换完成后,会重新一轮的替换检查,隐含有循环机制,但不超过10次;如果超过,提示500响应码,[flag]所表示的标志位用于控制此循环机制如果替换后的URL是以http://或https://开头,则替换结果会直接以重定向返回给客户端, 即永久重定向 301。
3.1 正则表达式
符号
功能描述
.
匹配除换行符以外的任意字符
w
匹配字母或数字或下划线或汉字
s
匹配任意的空白符
d
匹配数字 [0-9]
匹配单词的开始或结束
^
匹配字付串的开始
$
匹配字符串的结束
*
匹配重复零次或更多次
匹配重复一次或更多次
匹配重复零次或一次
(n)
匹配重复n次
{n,}
匹配重复n次或更多次
{n,m}
匹配重复n到m次
*
匹配重复任意次,但尽可能少重复
匹配重复1次或更多次,但尽可能少重复
匹配重复0次或1次,但尽可能少重复
{n,m}
匹配重复n到m次,但尽可能少重复
{n,}
匹配重复n次以上,但尽可能少重复
W
匹配任意不是字母,数字,下划线,汉字的字符
S
匹配任意不是空白符的字符
D
匹配任意非数字的字符
B
匹配不是单词开头或结束的位置
[^x]
匹配除了x以外的任意字符
[^kgc]
匹配除了kgc 这几个字母以外的任意字符
三、rewrite模块使用实例
1.基于域名的跳转
现在公司旧域名www.jiu.com有业务需求变更,需要使用新域名www.xin.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。
第一步:修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.jiu.com;
#域名修改
charset utf-8;
access_log /var/log/nginx/www.jiu.com-access.log;
#日志修改
location / {
#添加域名重定向
if ($host = 'www.jiu.com'){
#$host为rewrite全局变量,代表请求主机头字段或主机名
rewrite ^/(.*)$ http://www.xin.com/$1 permanent;
#$1为正则匹配的内容,即域名后边的字符串
}
root html;
index index.html index.htm;
}
}

第二步:创建所需目录与文件
#创建日志文件夹,检查语法
mkdir -p /var/log/nginx
nginx -t
#创建网页test目录与文件1.html
mkdir -p /usr/local/nginx/html/test
vim /usr/local/nginx/html/test/1.html
------------------------------------------
<h1 font color=red>
Here is the content of test
<img src="1.jpg"/>
</h1
-------------------------------------------
#上传1.jpg图片文件
cd /usr/local/nginx/html/test
rz -E


第三步:添加域名与IP地址映射关系

第四步:网页验证
#打开浏览器输入
www.jiu.com/test/1.html
会发现重定向到www.xin.com/test/1.html

2.基于客户端 IP 访问跳转
公司业务新版本上线,要求所有 IP 访问任何内容都显示一个固定维护页面,只有公司 IP :192.168.79.210访问正常。
第一步:修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
--------------------------------------------------------------
server {
listen 80;
server_name www.weihu.com;
#域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log; #日志修改
#设置是否合法的IP标记
set $rewrite true; #设置变量$rewrite,变量值为boole值true
#判断是否为合法IP
if ($remote_addr = "192.168.80.111"){ #当客户端IP为192.168.80.111时,将变量值设为false,不进行重写
set $rewrite false;
}
#除了合法IP,其它都是非法IP,进行重写跳转维护页面
if ($rewrite = true){ #当变量值为true时,进行重写
rewrite (.+) /weihu.html; #重写在访问IP后边插入/weihu.html,例如192.168.80.11/weihu.html
}
location = /weihu.html {
root /var/www/html; #网页返回/var/www/html/weihu.html的内容
}
location / {
root html;
index index.html index.htm;
}
}

第二步:设置维护界面并重启服务
mkdir -p /var/www/html/
vim /var/www/html/weihu.html
-----------------------------------
<h1>
Sorry!
We are busy now!
See you tomorrow!
</h1>
-----------------------------------
systemctl restart nginx

第三步:网页验证
#本机网页浏览器输入
www.weihu.com
访问应正常
#在其他设备浏览器访问测试首先修改映射文件
echo "192.168.80.111 www.weihu.com" >>/etc/hosts
#浏览器测试
www.weihu.com


3.基于旧域名跳转到新域名后面加目录
现在访问的是 http://mail.jiu.com/post,现在需要将这个域名下面的访问都跳转到http://www.jiu.com/
第一步:修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
-------------------------------------
server {
listen 80;
server_name www.jiu.com; #域名修改
charset utf-8;
access_log /var/log/nginx/pc03-access.log;
#添加
location /post {
rewrite (.+) http://www.jiu.com/mail$1 permanent; #这里的$1为位置变量,代表/post
}
location / {
root html;
index index.html index.htm;
}
}

第二步:添加临时域名和IP的映射关系
vim /etc/hosts
--------------
192.168.80.111 www.jiu.com mail.jiu.com
第三步:创建准备的网页文件
[root@localhost conf.d]#mkdir -p /apps/nginx/html/post/
[root@localhost conf.d]#echo "this is 1.html" >> /apps/nginx/html/post/1.html
[root@localhost conf.d]#systemctl restart nginx
第四步:浏览器验证
输入mail.jiu.com/post/1.html
显示为www.jiu.com/post/1.html

4.基于参数匹配的跳转
访问http://www.jiu.com/100-(100\|200)-100.html 跳转到http://www.jiu.com页面。
第一步:修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
--------------------------------------
server {
listen 80;
server_name www.pc04.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.pc04.com-access.log;
if ($request_uri ~ ^/100-(100|200)-(d+).html$) {
rewrite (.*) http://www.pc04.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
第二步:检查语法并重启服务
nginx -t
systemctl restart nginx
第三步:网页验证
#在浏览器输入
www.pc04.com/100-200-100.html

#在浏览器输入错误范围
www.jiu.com/100-500-100.html

5.基于目录下所有 php 结尾的文件跳转
要求访问 http://www.kgc.com/upload/123.php 跳转到首页。
第一步:修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
------------------------------------
server {
listen 80;
server_name www.pc05.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.pc05.com-access.log;
location ~* /upload/.*.php$ {
rewrite (.+) http://www.pc05.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
第二步:检查语法并重启服务
nginx -t
systemctl restart nginx
第三步:网页验证
#在浏览器输入
www.pc05.com/upload/123.php
6.基于最普通一条 url 请求的跳转
要求访问一个具体的页面如 http://www.kgc.com/abc/123.html 跳转到首页
第一步:修改主配置文件
vim /usr/local/nginx/conf/nginx.conf
----------------------------------------
server {
listen 80;
server_name www.pc06.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.pc06.com-access.log main;
location ~* ^/abc/123.html {
rewrite (.+) http://www.pc06.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}

第二步:检查配置文件并且重启服务
nginx -t
systemctl restart nginx
第三步:浏览器中访问测试
#在浏览器输入
www.pc06.com/abc/123.html
