一 Nginx Rewrite
1.1 Rewrite跳转场景
● URL看起来更规范、合理
● 企业会将动态URL地址伪装成静态地址提供服务
● 网址换新域名后,让旧的访问跳转到新的域名上
● 服务端某些业务调整
1.2 Rewrite跳转实现
1.3 Rewrite实际场景
● Nginx跳转需求的实现方式
● 使用 rewrite进行匹配跳转
● 使用if匹配全局变量后跳转
● 使用 location匹配再跳转
● rewrite放在 server{},if{}, location{} 段中
● 对域名或参数字符串
● 使用if全局变量匹配
● 使用 proxy_pass反向代理
1.4 Nginx正则表达式
1.5 Rewrite命令
1.5.1 Rewrite命令语法
rewrite [flag]
1.5.2 flag标记说明
last:相当于Apache的[L]标记,表示完成rewrite
break:本条规则匹配完成即终止,不再匹配后面的任何规则
redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址,爬虫不会更新url
permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址,爬虫更新url
二 Nginx Rewrite基本操作
2.1 location分类
● location = patt {} [精准匹配]
● ocation patt {} [一般匹配 ]
● location ~ patt {} [正则匹配]
2.2 location优先级
● 相同类型的表达式,字符串长的会优先匹配
● 按优先级排列
● = 类型
● ^~ 类型表达式
● 正则表达式 (和*)类型
● 常规字符串匹配类型,按前缀匹配
● 通用匹配(/),如果没有其他匹配,任何请求都会匹配到
2.3 比较rewrite和location
● 相同点
● 都能实现跳转
● 不同点
● rewrite是在同一域名内更改获取资源的路径
● location是对一类路径做控制访问或反向代理,还可以 proxy_pass到其他机器
● write会写在 location里,执行顺序
● 执行 server块里面的 rewrite指令
● 执行 location匹配
● 执行选定的 location中的 rewrite指令
2.4 Location优先级的示例
location = / { '//精确匹配 /,主机名后面不能带任何字符串'
[configuraion A ]
}
location / { '//所有的地址都以/开头,这条规则将匹配到所有请求,但正则和最长字符串会优先匹配'
[configuraion B ]
}
location /documents/ { '//匹配任何以/documents/开头的地址,当后面的正则表达式没有匹配到时,才起作用'
[configuraion C ]
}
location ~ /documents/abc { '//匹配任何以/documents/abc开头的地址,当后面的正则表达式没有匹配到时,才会起作用'
[configuraion D ]
}
location ^~ /images/ { '//以/images/开头的地址,匹配符合后,停止往下匹配'
[configuraion E ]
}
location ~*\.(gif|jpg|gpeg)$ { '//匹配所有以 gif, jpg或jpeg结尾的请求, Images/下的图片会被 [configuration E]处理,因为^~的优先级更高'
[configuraion F ]
}
location /images/abc { '//最长字符匹配到 /images/abc,优先级最低'
[configuraion G ]
}
location ~ /images/abc { '//以/ Images/abc开头的,优先级次之'
[configuraion H ]
}
location /images/abc/1.html { '//如果和正则 ~ images/abc/1.htm相比,正则优先级更高'
[configuraion I ]
}
2.5 location优先级规则
● 匹配某个具体文件
● ( location = 完整路径)>( location ^~ 完整路径)>( location ~* 完整路径)>( location ~ 完整路径)>( location完整路径)>( location /)
● 用目录做匹配访问某个文件
● ( location = 目录)>( location ^~ 目录)>( location ~ 目录)>
( location ~* 目录)>( location 目录)>( location /)
三 实验
3.1 基于域名的跳转
[root@localhost ~]# vi /usr/local/nginx/conf/nginx.conf
location / {
# root html;
# index index.html index.htm;
if ($host = 'www.51xit.top')
{
rewrite ^/(.*)$ http://www.52xit.top/$1 permanent;
}
}
浏览器输入:http://www.51xit.top,结果跳转到www.52xit.top
3.2 基于客户端IP访问跳转
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log
/usr/local/nginx/logs/www.51xit.top.access.log;
#改日志存储路径 去掉main
set $rewrite true;
if ($remote_addr = "192.168.100.1"){
set $rewrite false;
}
if ($rewrite = true){
rewrite (.*) /wh.html;
}
location = /wh.html {
root /usr/local/nginx/html;
}
location / {
root html;
index index.html index.htm;
}
[root@www ~]# killall -s HUP nginx
[root@www ~]# echo "网页维护中" >/usr/local/nginx/html/wh.html
浏览器测试,输入www.51xit.top,正常访问;
登录客户机20.0.0.11,浏览器输入www.51xit.top,提示"网页维护中"
3.3 基于旧域名跳转到新域名后面加目录
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log
/usr/local/nginx/logs/www.51xit.top.access.log;
localtion /post {
rewrite (.+) http://www.52xit.top/bbs$1 permanent;
}
[root@www ~]# killall -s HUP nginx
浏览器测试,输入www.51xit.top/post,显示www.52xit.top/bbs
3.4 基于参数匹配的跳转
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log
/usr/local/nginx/logs/www.51xit.top.access.log;
if ($request_uri ~ ^/100-(100|200)-(\d+).html$){
rewrite (.*) http://www.51xit.top permanent;
}
[root@www ~]# killall -s HUP nginx
浏览器输入http://www.51xit.top/100-100-100.html ,页面显示http://www.51xit.top
浏览器输入http://www.51xit.top/100-200-10.html ,页面显示http://www.51xit.top
浏览器输入http://www.51xit.top/100-100-100dddd.html ,页面不能跳转
3.5 基于目录下所有php结尾的文件跳转
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log /usr/local/ng
inx/logs/www.51xit.top.access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.51xit.top permanent;
}
[root@www ~]# killall -s HUP nginx
浏览器输入http://www.51xit.top/upload/1.php ,页面显示http://www.51xit.top
浏览器输入http://www.51xit.top/upload/bbs/1.php ,页面显示http://www.51xit.top
浏览器输入http://www.51xit.top/upload/index.html ,页面不跳转
3.6 基于最普通一条url请求的跳转
[root@www ~]# vim /usr/local/nginx/conf/nginx.conf
...
server {
listen 80;
server_name www.51xit.top;
charset utf-8;
access_log
/usr/local/nginx/logs/www.51xit.top.access.log;
location ~* ^/1/test.html {
rewrite (.+) http://www.51xit.top permanent;
}
[root@www ~]# killall -s HUP nginx
浏览器输入http://www.51xit.top/1/test.html ,页面显示http://www.51xit.top