nginx-5 Rewrite

Rewrite

所属模块:ngx_http_rewrite_module

依赖:PCRE

作用:重定向、url重写(伪静态)、动静分离、、逻辑判断

变量

https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/variable.md

指令

 

rewrite重定向

规则:https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/rewrite_ruler.md

我在/data/wwwroot/www.1.com下有四个文件:1.html、2.html、3.html、index.html

server{
        listen 80;        
        server_name www.1.com;            #域名
        index 1.html;                    #设置默认页面就是/data/wwwroot/www.1.com目录下的1.html文件,如果不设置index属性那默认访问/data/wwwroot/www.1.com目录下的index.html文件
        root /data/wwwroot/www.1.com;    #默认网页所在
        rewrite_log on;                  #开启rewrite执行日志,这个需要把erro_log的级别设置为notice
        rewrite /1.html /2.html;
        rewrite /2.html /3.html;
}

请求:curl -x127.0.0.1:80 www.1.com/1.html

上面这个配置就是将请求到1.html的请求重定向到2.html,然后再把请求重定向到3.html

查看日志 less /var/log/nginx/error.log:

19/05/12 20:59:37 [notice] 10375#10375: *13 "/1.html" does not match "/", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/ HTTP/1.1", host: "www.1.com"
2019/05/12 20:59:37 [notice] 10375#10375: *13 "/2.html" does not match "/", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/ HTTP/1.1", host: "www.1.com"
2019/05/12 20:59:37 [notice] 10375#10375: *13 "/1.html" does not match "/index.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/ HTTP/1.1", host: "www.1.com"
2019/05/12 20:59:37 [notice] 10375#10375: *13 "/2.html" does not match "/index.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/ HTTP/1.1", host: "www.1.com"
2019/05/12 20:59:54 [notice] 10376#10376: *14 "/1.html" matches "/1.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/05/12 20:59:54 [notice] 10376#10376: *14 rewritten data: "/2.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/05/12 20:59:54 [notice] 10376#10376: *14 "/2.html" matches "/2.html", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
2019/05/12 20:59:54 [notice] 10376#10376: *14 rewritten data: "/3.html", args: "", client: 127.0.0.1, server: www.1.com, request: "GET HTTP://www.1.com/1.html HTTP/1.1", host: "www.1.com"
(END)

用户请求了1.html,首先先去匹配index,html,不匹配,然后匹配到1.html,然后重定向到2.html,后来又匹配到2.html然后重定向到3.html,所以最终结果是显示的3.html页面的内容

break结束

server{
        listen 80;        
        server_name www.1.com;            #域名
        index 1.html;                    #设置默认页面就是/data/wwwroot/www.1.com目录下的1.html文件,如果不设置index属性那默认访问/data/wwwroot/www.1.com目录下的index.html文件
        root /data/wwwroot/www.1.com;    #默认网页所在
        rewrite_log on;                  #开启rewrite执行日志,这个需要把erro_log的级别设置为notice
        rewrite /1.html /2.html break;
        rewrite /2.html /3.html;
}

这里就只会重定向到2.html,应为在第一个rewrite的时候就退出执行了,所以下面那个跳转到3.html的指令就没执行

last

功能大体上和break一样,区别在于,如果它们在location语句中不一样,在location外面就是一样的

请求:curl -x127.0.0.1:80 www.1.com/1.html

这个配置的执行日志

首先,匹配上location /,找到了1.html,最终重定向到3.html(一个请求匹配到了location之后,location中的所有语句都会执行一遍),然后又匹配上了location 3.html重定向到b.html(b.html不存在所以报404)

在不加break和last的情况下,程序要依次全部语句都执行一次(匹配的),如果此时加上break,那么这个serer整个模块全部终止,如:

这里没有再去匹配locatin /2.html,因为它直接跳出了server。

如果是last

执行过程:首先匹配上location /,然后重定向到2.html,然后location中后面的语句就不执行了,将会再一次的执行server,依次匹配上了location /和loaction /2.html,最终执行的是location /2.html,因为location /2.html更精准。

return返回

它可以直接返回状态码、

字符串给用户(返回字符串必须指定状态码),

返回json

返回html

返回链接:

这个和编程一样的,跳出当前作用域(做比喻)

 

if语句

参考:https://coding.net/u/aminglinux/p/nginx/git/blob/master/rewrite/if.md

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值