nginx的rewrite跳转实现
- rewrite跳转应用场景
- rewrite跳转的实现
- rewrite的主要作用
- rewrite中命令执行顺序
- rewrite的使用语法
- flag标记类型
- location的分类
- location常用匹配规则
- location匹配优先级规则
- 设置nginx基于域名跳转
- 设置nginx基于客户端ip访问跳转
- 跳转后转到新域名后并加目录
- 基于多余参数匹配跳转
- 基于目录下的所有php结尾的文件跳转
- 基于最普通的一条URL请求跳转
rewrite跳转应用场景
- 为了使用户浏览的URL更规范,更契合开发和产品人员的需求
- 为了使用户有更好的体验,企业会将动态URL伪装成静态URL
- 使旧的域名直接跳转到新的域名
- 方便服务端业务的调整
rewrite跳转的实现
- nginx通过一个ngx_http_rewrite_module模块来支持URL的重写,支持if条件判断,但不支持else,类似于单分支语句
- 从一个location跳转到另一个location,循环最多可以执行10次,超过10次将返回500错误
- pcre支持perl兼容正则表达式语法的规则匹配
- 可以自己设置变量或者使用全局变量
rewrite的主要作用
- 使用nginx提供的全局变量或者自己设置的变量,来结合正则表达式和标记位实现URL的重写和重定向
rewrite中命令执行顺序
①先执行server模块里的rewrite命令
②然后执行location中的rewrite命令
③再执行选定的location中的if中的rewrite命令
rewrite的使用语法
rewrite [正则匹配规则] [跳转后的内容] [支持的flag标记]
flag标记类型
①last:本条规则匹配完成后,继续向下匹配新的location URL规则,一般用在server和if中
②break:本条规则匹配完成后,不再匹配后面任何规则,一般用在location中
③redirect:临时重定向,浏览器地址会显示跳转后的URL地址
④permanent:永久重定向,浏览器地址会显示跳转后的URL地址
location的分类
一般location分为三大类
- 精准匹配:location = pass {…} 精确匹配字符串
- 正则匹配:location ~ pass {…} 按照正则表达式方式匹配
- 一般匹配:location pass {…} 只要包含pass字符都行
location常用匹配规则
= :进行普通字符的精准匹配
^~:按正则表达式方式使用前缀匹配,匹配成功就不再匹配其他location
~:按正则表达式方式区分大小写的匹配
~*:按正则表达式方式不区分大小写的匹配
!~:按正则表达式方式区分大小写的匹配然后取反
!~*:按正则表达式方式不区分大小写的匹配然后取反
@:定义一个location,使用在内部定向时
location匹配优先级规则
- 匹配具体文件
(location = 路径) > (location ^~ 路径) >(location ~* 路径) >(location ~ 路径) >(location 路径) > (location /) - 匹配目录
(location = 路径) > (location ^~ 路径) >(location ~ 路径) >(location ~* 路径) >(location 路径) > (location /)
设置nginx基于域名跳转
- 在匹配location根这里添加一个跳转
修改nginx配置文件
server {
listen 80;
server_name www.test.com; //修改域名
#charset koi8-r;
access_log /var/log/nginx/www.test.com-access.log; //开启日志
location / {
if ($host = 'www.test.com'){ //判断输出的是否是test.com
rewrite ^/(.*)$ http://www.ceshi.com/$1 permanent; //是就跳转到ceshi.com 类型是永久跳转,$1匹配的位置变量,就是域名后面的字符串
}
root html;
index index.html index.htm;
}
设置nginx基于客户端ip访问跳转
- 当我们需要维护一个网页时,可以使其他用户访问网页直接跳转到维护页面,而本地访问是可以访问到页面的
- 修改nginx配置文件
server {
listen 80;
server_name www.test.com;
#charset koi8-r;
access_log /var/log/nginx/www.test.com-access.log;
set $rewrite true; //设置变量的布尔值
if ($remote_addr = "12.0.0.8"){
set $rewrite false; //判断ip地址,对了就打上false标签
}
if ($rewrite = true) { //当变量值为true时跳转下面的页面
rewrite (.+) /weihu.html;
}
location = /weihu.html {
root /var/www/html; //页面返回此文件内容
}
location / {
root html;
index index.html index.htm;
}
- 然后创建weihu.html,写入提示信息并重启服务
[root@server ~]# cd /var/www/html/
[root@server html]# ls
weihu.html
[root@server html]# cat weihu.html
<h1>正在维护中。。。</h1>
[root@server html]#
[root@server html]# systemctl stop nginx.service
[root@server html]# systemctl start nginx.service
自身的浏览器,清缓存
其他机器上的浏览器
跳转后转到新域名后并加目录
- 修改nginx配置文件
server {
listen 80;
server_name bbs.123.com; //修改域名
#charset koi8-r;
access_log /var/log/nginx/www.ceshi.com-access.log;
location /post {
rewrite ^(.+) http://www.ceshi.com/bbs$1 permanent;
} //匹配到post跳转,$1代表/post
location / {
root html;
index index.html index.htm;
}
- 然后重启服务
[root@server html]# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
[root@server html]# systemctl stop nginx.service
[root@server html]# systemctl start nginx.service
- 写一个bbs的页面并写入hello
[root@server post]# pwd
/usr/local/nginx/html/bbs/post
[root@server post]# ls
1.html
[root@server post]# cat 1.html
<h1>hello</h1>
清缓存,访问网页用bbs.123.com/post/1.html
基于多余参数匹配跳转
- 修改nginx配置文件
server {
listen 80;
server_name www.test.com;
#charset koi8-r;
access_log /var/log/nginx/www.test.com-access.log;
if ($request_uri ~ ^/100-(10|20|30)-(\d+)\.html$){ //设置正则匹配,在com/后面跟100-10或20或30-随机数字
rewrite (.*) http://www.test.com permanent; //匹配到上面的就会跳转到www.test.com
}
location / {
root html;
index index.html index.htm;
}
- 重启访问页面,清缓存
基于目录下的所有php结尾的文件跳转
- 配置nginx文件
server {
listen 80;
server_name www.test.com;
#charset koi8-r;
access_log /var/log/nginx/www.test.com-access.log;
location ~* /123/.*\.php$ { //正则匹配test文件下的以php结尾的文件
rewrite (.+) http://www.test.com permanent; //匹配到就跳转到www.test.com
}
location / {
root html;
index index.html index.htm;
}
- 重启访问页面,清缓存
基于最普通的一条URL请求跳转
- 修改nginx配置
server {
listen 80;
server_name www.test.com;
#charset koi8-r;
access_log /var/log/nginx/www.test.com-access.log;
location ~* /123/123.html { //正则匹配123下面的123.html
rewrite (.+) http://www.test.com permanent; //匹配到就跳转到www.test.com
}
location / {
root html;
index index.html index.htm;
}
- 重启然后访问页面,清缓存
小结:rewrite跳转需要注意匹配location的优先顺序,目录是区分大小写优先匹配,文件是不区分大小写优先匹配