Rewrite功能
rewrite功能就是,使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。比如:更换域名后需要保持旧的域名能跳转到新的域名上、某网页发生改变需要跳转到新的页面、网站防盗链等等需求。
Rewrite跳转场景
1、URL看起来更规范、合理,合乎开发及产品的人员的需求
2、为了让用户体验更好,企业会将动态URL地址伪装成静态地址提供服务
3、网址换新域名后,让旧的访问跳转到新的域名上
4、服务端某些业务调整 ,例如根据特殊变量,目录,客户端信息进行URL调整
Rewrite实现跳转方式
Nginx:通过ngx_http_rewrite_module模块支持URL重写、支持if条件判断,但不支持else
跳转:从一个iocation跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误
PCRE支持: perl兼容正则表达式的语法规则匹配
重写模块set指令:创建新的变量并设其值
Rewrite实际场景
Nginx跳转需求的实现方式
●使用rewrite进行匹配跳转
●使用if匹配全局变量后跳转
●使用location匹配再跳转
rewrite放在server{}, if{},location{}段中(三个生效位置)
●location只对域名后边的除去传递参数外的字符串起作用
对域名或参数字符串
●使用if全局变量匹配
●使用proxy_pass反向代理
Nginx常用正则表达式元字符
^ 匹配输入字符串的起始位置
$ 匹配输入字符串的的结束位置
* 匹配前面的字符零次或多次
+ 匹配前面的字符一次或多次
? 匹配前面的字符一次或零次
. 匹配除”\n“之外的任何单个字符
\d 匹配纯数字
\s 匹配任意的空白符
\b 匹配单词的开始或结束
{n} 重复n次
{n,} 重复n次或多次
{n,m} 重复n到m次
[c] 匹配单个字符c
[a-z] 匹配a-z小写字母的任意一个
[a-zA-Z] 匹配a-z小写字母或大写字母的任意一个
() 表达式的开始和结束位置
| 或运算符
Rewrite命令语法
rewrite <regex> <replacement> [flag];
(正则) (跳转后的内容) (rewrite支持的flag标记)
标记 说明
last 相当于[L]标记,表示完成rewrite(一般写在server和if中,不终止重写后的url匹配)
break 本条规则匹配完成即终止,不再匹配后面的任何规则(一般使用在location中,终止重写后的url匹配)
redirect 返回302临时重定向,浏览器地址会显示跳转后的URL地址
permanent 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url
last和break最大的不同在于
break是终止当前location的rewrite检测,而且不再进行location匹配
last是终止当前location的rewrite检测,但会继续重试location匹配并处理区块中的rewrite规则
Rewrite执行顺序优先级
1、执行server模块里面的rewrite指令
2、执行选定的location和rewrite指令
3、执行选定的location中if中的rewrite指令
location模块分类
location = patt {} 精确匹配
location patt {} 一般匹配
location ~ patt {} 正则匹配
常用的正则表达式
~ 执行一个正则匹配
~* 执行一个正则匹配,不区分大小写
!~ 执行一个正则匹配,区分大小写不匹配
!~* 执行一个正则匹配,不区分大小写不匹配
^~ 普通字符匹配,使用前缀匹配,如果匹配成功,则不再匹配其他location
= 匹配字符精确匹配,也就是完全匹配
@ 定义一个命名的location,使用内部定向
location优先级
相同类型的表达式,字符串长的会优先匹配
按照优先级排序由高到低
1、= 类型
2、^~ 类型表达式
3、正则表达式 (~和~*)类型
4、常规字符串匹配类型,按前缀匹配
5、通用匹配(/),如果没有其他匹配,任何请求都会匹配到
匹配某个具体文件
(location=完整路径)>(location ^~完整路径)>(location ~*完整路径)>(locaion ~完整路径)>(location 完整路径)>(location /)
用目录做匹配访问某个文件
(location=目录)>(location ^~目录)>(location ~目录)>(locaion ~* 目录)>(location 目录)>(location /)
rewrite与location异同点对比
相同点:都可以实现跳转
不同点:rewrite是在同一域名内更获取资源的路径
location是对一类路径做控制访问或反向代理,还可以proxy_到其他机器
rewrite会写在location里,执行顺序
执行server块里面的rewrite指令
执行location匹配
执行选定得location中的if指令
实际网站使用中至少三个匹配规则定义
1、直接匹配网站根,通过域名访问网站首页比较频繁,食用这个会加速处理,例如官网可以是一个静态首页,也可以直接转发给后端应用服务器
location = / }
root html;
index index.html index.htm;
}
2、处理静态文件请求,这是nginx作为http服务器得强项,有两种配置模式,目录匹配或后后缀匹配,任选其一或搭配使用
location ^~ /static/ {
root /webroot/static/ ;
}
location ~* \. (html | gif | jpg)$ {
root /webroot/res/ ;
}
3、通用规则,比如用来转发带.php .jsp后缀的动态请求到后端应用服务器非静态文件请求就默认是动态请求
upstream tomcat_server
xxx.xxx.xxx.xxx
}
location / {
proxy_pass http://tomcat_server: #跳转地址
}
基于域名的跳转
**示例一**
现在公司旧域名www.kgc.com有业务需求变更,需要使用新域名www.benet.com代替,但是旧域名不能废除,需要跳转到新域名上,而且后面的参数保持不变。
1、添加映射
[root@localhost ~]# vim /etc/resolv.conf
# Generated by NetworkManager
search localdomain
nameserver 114.114.114.114
[root@localhost ~]# vim /etc/hosts
192.168.153.215 www.kgc.com www.benet.com
2、创建日志
[root@localhost conf]# mkdir -p /var/log/nginx/
3、修改主配置文件
[root@localhost conf]# vim nginx.conf
#gzip on;
server {
listen 80;
server_name www.kgc.com; #修改域名
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com-access.log;#修改取消注释和其他
location / {
if ($host = 'www.kgc.com'){ #$host为rewrite全局变量,代表请求主机头字段或主机名
rewrite ^/(.*) http://www.benet.com/$1 permanent; #$1为匹配的位置变量,即域名后边的字符串,同时永久跳转
}
root html;
index index.html index.htm;
[root@localhost ~]# systemctl start nginx
4、测试访问旧域名www.kgc.com
5、测试访问www.kgc.com.html,错误页面上kgc也会变成benet
基于客户端IP跳转访问
**示例二**
今天公司业务新版本上线,要求所有iP访问任何内容都显示一个固定维护页面,只有公司IP 192.168.153.215访问正常。
首先删除示例一配置
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server_name www.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com-access.log;
**set $rewrite true;
if ($remote_addr = "192.168.153.215"){
set $rewrite false;
}
if ($rewrite = true){
rewrite (.+) /weihu.html;
}
location = /weihu.html {
root /var/www/html;
}**
location / {
root html;
index index.html index.htm;
}
修改文件的注释
#设置是否为合法的ip标记
set $rewrite true; #设置变量$rewrite,变量值为布尔值为true
#判断是否为合法ip
if ($remote_addr = "192.168.153.215"){ #当客户端为153.215时,将变量值设为flase,不进行重写
set $rewrite false;
}
#除了合法ip,其他都是非法ip,进行重写跳转到维护页面
if ($rewrite = true){ #当变量值为true时,进行重写
rewrite (.+) /weihu.html; #重写在访问ip后边插入/weihu.html,例如153.215/weihu.html
}
location = /weihu.html {
root /var/www/html; #页面返回/var/www/html/weihu.html的内容
}
[root@localhost ~]# mkdir -p /var/www/html
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# vim weihu.html
<h1> this is weihuzong </h1>
[root@localhost ~]# systemctl start nginx
测试用一台机访问153.215时weihu.html界面,仅ip为153.215的一台机才可以正常访问
基于旧域名跳转到新域名后面加目录
示例三:
当前访问的是:http://bbs.kgc.com/post/1.html现在需要将这个域名下面的访问都跳转到http://www.benet.com/bbs/post/1.html下面
1、创建指定目录,添加映射
[root@localhost ~]# mkdir -p /usr/local/nginx/html/bbs/post
[root@localhost ~]# echo "<h1> this is 1.html </h1>" >> /usr/local/nginx/html/bbs/post/1.html
[root@localhost ~]# echo "192.168.153.215 bbs.kgc.com" >> /etc/hosts
2、修改主配置文件
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
**server_name bbs.kgc.com;** 修改域名
#charset koi8-r;
access_log /var/log/nginx/www.benet.com-access.log; #修改日志名
location /post {
rewrite (.+) http://www.benet.com/bbs$1 permanent; #.+代表1或多个,$1为位置变量,代表/post
}
location / {
root html;
index index.html index.htm;
}
[root@localhost ~]# systemctl start nginx
3、访问bbs.kgc.com/post/1.html转到benet.com/bbs/post/1.html
基于参数匹配(多余)的跳转
示例四:
访问http://www.kgc.com/100-(100|200) -100.html会跳转到http://www.kgc.com的页面
server {
listen 80;
server_name www.kgc.com; #修改域名
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com-access.log; #修改日志名
if ($request_uri ~ ^/100-(100|200)-(\d+)\.html$){ #$request_uri 内置变量,表示uri,\d 纯数字
rewrite (.*) http://www.kgc.com permanent; #设置重写
}
[root@localhost ~]# systemctl start nginx
访问www.kgc.com/100-(100|200) -100.html
基于目录下所有php结尾的文件跳转
示例五:
访问http://www.kgc.com/upload/123.php跳转到首页(场景:注册)
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com-access.log;
location ~* /upload/.*\.php$ { #不区分大小写匹配/upload/目录下的所有以.php为结尾的文件
rewrite (.+) http://www.kgc.com permanent; #.+是一或多次
}
location / {
root html;
index index.html index.htm;
}
[root@localhost ~]# systemctl start nginx
测试访问www.kgc.com/upload/123.php是否跳转到首页
基于最普通的一条url请求的跳转
示例六:
要求访问一个具体的页面,如: http : / /www.kgc.com/abc/123.html,跳转到首页
[root@localhost ~]# vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.kgc.com;
#charset koi8-r;
access_log /var/log/nginx/www.kgc.com-access.log;
location ~* /abc/123.html {
rewrite (.+) http://www.kgc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
[root@localhost ~]# systemctl start nginx
小结:
1、nginx是做静态页面处理,工作在第七层应用层。
2、静态页面的并发量:3~5万
3、nginx通过与php连接,实现动静分离
4、防盗链是nginx中最基础的安全措施之一,其他优化是为了提高用户体验,提高运行效率