rewirte使用详解
rewirte
rewrite功能
-
使用nginx提供的全局变量或自己设置的变量,结合正则表达式和标记位实现URL重写以及重定向。
-
rewrite只能放在server{},location{},if{}中,并且默认只能对域名后边的除去传递的参数外的字符串起作用,
rewrite跳转实现:
- Nginx:通过ngx_http_rewrite_module 模块支持URL重写、支持if条件判断,但不支持else
- 跳转:从一个location跳转到另一个location,循环最多可以执行10次,超过后nginx将返回500错误
- PCRE支持:perl兼容正则表达式的语法规则匹配
- 重写模块 set 指令:创建新的变量并设其值
rewirte 执行顺序如下:
(1)执行 server 块里面的 rewirte 指令。
(2)执行 location 匹配
(3)执行选定的 location 中的 rewrite 指令。
语法
rewrite <regex><replacement> [flag];
regex:表示正则匹配规则。
replacement:表示跳转后的内容。
fiag:表示 rewirte 支持的 flag 标记。
flag标记说明
last: 本条规则匹配完成后,继续向下匹配新的 location URL规则,一般用在server 和 if 中。
break:本条规则匹配完成即终止,不再匹配后面的任何规则,一般使用在 location 中。
redirect:返回302临时重定向,浏览器地址会显示跳转后的URL地址。
permanent:返回301永久重定向,浏览器地址栏会显示跳转后的URL地址。
rewrite使用介绍
(1)基于域名的跳转
- 现在公司旧域名www.muzi.com有业务需求变更,需要使用新域名www.hua.com代替,但是旧域名不能废除,需要跳转
到新域名上,而且后面的参数保持不变。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.muzi.com #域名修改
charset utf-8;
access_log /var/log/nginx/www.muzi.com-accsee.log; #日志修改
location / {
#添加域名重定向
if ($host = 'www.muzi.com'){ #$host为rewrite全局变量。代表请求主机头字段或主机名
rewrite ^/(.*)http://www.hua.com/$1 permanent; #$1为正则匹配的内容,即域名后边的字符串
}
root html;
index index.html index.htm;
}
}
echo "192.168.199.40 www.muzi.com www.hua.com" >> /etc/hosts
systemctl restart nginx
浏览器输入模拟访问 http://www.muzi.com.test/1.html(虽然这个请求内容时不存在的)
会跳转到www.hua.com/test/1.html,查看元素可以看到返回301.实现了永久重定向跳转,而且域名后的参数也正常跳转。
(2)域名客户端 IP 跳转访问
- 今天公司业务新版本上线,要求所有IP 范围任何内容都显示一个固定维护页面,只有公司IP:192.168.199.10访问正常
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name www.muzi.com #域名修改
charset utf-8;
access_log /var/log/nginx/ww
w.muzi.com-accsee.log; #日志修改
#设置是否合法的IP标记
set $rewrite true; #设置变量$rewirte,变量值为boole值true
#判断是否为合法IP
if ($remote_addr = "192.168.199.10)"{ #当客户端IP为192.168.199.10时,将变量值设为false,不进行重写
set $rewirte false;
}
#除了合法IP,其他都是非法IP,进行重写跳转维护页面
if ($rewrite = true) { #当变量值为true时,进行重写
rewrite (.+) /weihu.html; #重写在访问IP后边插入/weihu.html,例如192.168.199.10/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/
echo "<h1>We are maintaining now!</h1>" > /var/www/html/weihu.html
systemctl restart nginx
只有 IP 为 192.168.199.10 能正常访问,其他地址都是维护页面
(3)基于旧域名跳转到新域名后面加目录
- 现在访问的是http://bbs.kgc.com,现在需要将这个域名下面的访问都跳转到http://www.kgc.com/bbs
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server_name bbs.kgc.com; #域名修改
charset utf-8;
access_log /var/log/nginx/ www.kgc.com-access.log;
#添加
location /post {
rewrite (.+) http://www.kgc.com/bbs$1 permanent; #这里的$1为位置变量,代表/post
}
location / {
root html;
index index.html index.html;
}
}
mkdir -p /usr/local/nginx/html/bbs/post
echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
mkdir -p /usr/local/nginx/html/bbs/post
echo "this is 1.html" >> /usr/local/nginx/html/bbs/post/1.html
echo "192.168.80.10 bbs.kgc.com" >> /etc/hosts
systemctl restart nginx
使用浏览器访问http://bbs.kgc.com/post/1.html 跳转到http://www.kgc.com/bbs/post/1.html
(4)基于参数匹配的跳转
- 现在访问http://www.kgc.com/100-(100|200)-100.html跳转到http//www.kgc.com页面。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server name www.kgc.com; #域名修改
charset utf-8;
access_ log / var/ log/ nginx/www.kgc.com-access.log;
if ($request_uri~ ^/100-(100|200)-(\d+)\.html$) {
rewrite (. +) http://www.kgc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
使用浏览器访问http://www.kgc.com/100-200-100.html 或http://www.kgc.com/100-100-100.html跳转到
http://www.kgc.com页面。
(5)基于目录下所有php结尾的文件跳转
- 要求访问http://www.kgc.com/upload/123.php 跳转到首页。
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server name www.kgc.com; #域名修改
charset utf-8;
access_log /var/log/nginx/www.kgc.com-access.log;
location ~* /upload/.*\.php$ {
rewrite (.+) http://www.kgc.com permanent;
}
location / {
root html;
index index.html index.htm;
}
}
systemctl restart nginx
浏览器访问http://www.kgc.com/upload/123.php 跳转到 http://www.kgc.com 页面。
(6)基于最普通一条url请求的跳转
- 要求访问一个具体的页面如http://www.kgc.com/abc/123.html 跳转到首页
vim /usr/local/nginx/conf/nginx.conf
server {
listen 80;
server name www.kgc.com; #域名修改
charset utf-8;
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.html;
}
}
systemctl restart nginx
浏览器访问http://www.kgc.com/abc/123.html 跳转到 http://www.kgc.com 页面。