nginx之rewrite用法

rewrite用法

nginx之rewrite用法_第1张图片

【语法】 rewrite regex replacement [flag];
flag=【break|last|redirect|permant】
regex: 是正则表达式;
replacement:是替换值,是新值;
flag: 处理标志,可以省略。

rewrite配置文件

server {
listen       80;
server_name  rewrite.smallboy.com;

location /a.html {
    root html;
}

#访问 http://rewrite.smallboy.com/aa.html
location /aa.html { 
    rewrite ^/ /a.html break;
    root html/static/; 
}
location /cc {
    rewrite ^/ /a.html last;
    root html/static/;
}


#访问http://rewrite.smallboy.com/ab.html
location /ab.html { 
    rewrite ^/ /a.html last;
#   rewrite ^/ /b.html;
    root html/static/; 
}

location /wanglei {
    rewrite ^/ /a.html redirect;
    root html/static/;
}
}

http://rewrite.smallboy.com/aa.html
本来在html/static目录下面查找aa.html,rewrite将原先的路径改为/a.html,在html/static查找a.html。

nginx之rewrite用法_第2张图片

flag参数含义

flag=【break|last|redirect|permant|不写】

redirect/permant

redirect/permant: 如果rewrite 命中,则为页面重定向;

#访问http://rewrite.smallboy.com/wanglei 页面会变http://rewrite.smallboy.com/a.html
nginx之rewrite用法_第3张图片
break/last

break/last是内部重定向:

break标记:停止执行后续命令

last标记:会引发location重新匹配
nginx之rewrite用法_第4张图片
flag无值与有值区别
当flag有值时,rewrite值会中断,last会引发location重匹配;

当flag无值时,rewrite会继续往下走,最后一个rewrite值覆盖前面,在引发location重新匹配。

你可能感兴趣的:(nginx,nginx,linux,正则表达式,运维)