Nginx rewrite 详解

1. rewrite 基本介绍

rewrite是实现URL重写的关键指令,根据regex (正则表达式)部分内容,重定向到replacement,结尾是flag标记。

基本语法:

rewrite <regex> <replacement> [flag];关键字 正则 替代内容 flag标记

  • regex: 正则表达式语句进行规则匹配

  • replacement: 将正则匹配的内容替换成replacement

  • flag: last | break | redirect | permanent

  • last : 本条规则匹配完成后,继续向下匹配新的location URI规则

  • break: 本条规则匹配完成即终止,不再匹配后面的任何规则

  • redirect : 回302临时重定向,浏览器地址会显示跳转后的URL地址 (防爬虫)

  • permanent : 返回301永久重定向,浏览器地址栏会显示跳转后的URL地址

rewrite 使用位置

  • server : 在server中针对所有的请求

  • location : 在 location 中则针对 单个匹配路径的

  • If

2. server 中使用 rewrite

直接在server中使用 rewrite , 它会被先执行 优先于 location 中的

2.1 rewrite 外部站点
rewrite 到外部站点 是指 replacement 部分 是一个完整的 带 http/https 的 外部路径 ,它的特点是 浏览器会再次请求这个新的站点 所以浏览器上的地址一定会发生变化 不受 flag 参数影响

全部拦截 ^/(.*)$

下面的配置是 所有的请求都转发了 https://www.askajohnny.com

...server { listen 80; server_name www.testfront.com; #由于是外部站点带http/s 的 所以不受flag 影响 break last .. 都会进行跳转并且变更浏览器url rewrite ^/(.*)$ https://www.askajohnny.com break; location / { root html; index index.html; }}...

部分匹配

server { listen 80; server_name www.testfront.com; #只有当后缀是 数字.html 的时候才会转发到 https://www.askajohnny.com rewrite ^/([0-9]+).html$ https://www.askajohnny.com break; #其他的请求会走到这个location中 location / { root html; index index.html; }}

经过测试可以发现 直接跳转过去 并且 浏览器中的地址也直接变成了 https://www.askajohnny.com , 待会我们再详细讨论 什么时候会变化这个地址

比如在我的 博客网站中 我把http 的请求全部rewrite 转发到 https 上了, 因为我的QQ登录当时填写的回调是 http,又因为QQ互联的审核太麻烦 太慢 所以干脆就这样配置

2.2 rewrite 到内部站
rewrite 到内部站点是指 replacement 不带http/https 而是内部的另外一个路径 , 相当于访问隐藏起来的这个 内部路径, 只有这种内部站点跳转的时候 浏览器才有可能不变地址 要看 rewite flag 参数了 last 和 break 都不会变的, 只有 redirect 和 permanent

server { listen 80; server_name www.testfront.com; #只有当后缀是 数字.html 的时候才会转发到 https://www.askajohnny.com rewrite ^/([0-9]+).html$ /my.gif break; #其他的请求会走到这个location中 location / { root html; index index.html; } # 上面的rewrite 会被路由到这里 并且浏览器是不会感知到的 location /my.gif { root /www/static/; }}

经过测试 当访问 www.testfront.com/222.html 的时候

  • flag = last 浏览器不会变化 隐藏了 后端 /my.gif 地址

  • flag = break 浏览器不会变化 隐藏了 后端 /my.gif 地址

  • flag = redirect 和 permanent 浏览器变化了URL 变更状态码 302和 301

3. location 中使用rewrite

location 中也可以使用 rewrite 意思是只有匹配到 这个location 后才经过 rewrite 的正则通过后 再跳转
和上面一样 也分为 rewirte 的 replacement 是否包含http和https 外部站点

希望是如果 访问的后缀 是 数字.html 则 返回 my.gif 图 ,其他的都代理到 http://www.testbackend.com

server { listen 80; server_name www.testfront.com; #rewrite ^/([0-9]+).html$ /my.gif last; location /my.gif { root /www/static/;} location / { rewrite ^/([0-9]+).html$ /my.gif break; proxy_pass http://www.testbackend.com; } }

经过测试 只有访问www.testfront.com/数字.html 的时候 才能获取到 my.gif 文件

4. 使用场景模拟

4.1 基于域名跳转

比如现在你所在的公司 网站域名是 www.testfront.com 现在需要使用新的域名 www.newtestfront.com 替代, 但是旧的域名不能作废, 需要让旧的域名跳转到新的域名上 , 并且保持后面参数不变

模拟原本配置

server { listen 80; server_name www.testfront.com; location / { proxy_pass http://www.testbackend.com; } }

新配置,使用rewrite 操作 当访问老的域名 www.testfront.com 跳转到 新的 www.newtestfront.com

server { listen 80; server_name www.testfront.com ...; location / { # $host 是可以拿到访问的主机名 if ( $host = 'www.testfront.com' ) { rewrite ^/(.*)$ http://www.newtestfront.com/$1 permanent; } proxy_pass http://www.testbackend.com; } location ^~ /static/ { root /www/static; } } server { listen 80; server_name www.newtestfront.com; location / { # 这里可以改成 新域名的 新后端代理的服务, 依据实际情况 proxy_pass http://www.testbackend.com; } error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } }

4.2 基于客户端 IP 访问跳转

今天公司业务新版本上线,要求所有 IP 访问任何内容都显示一个固定维护页面,只有公司IP:192.168.200.100访问正常。

server { listen 80; server_name www.testfront.com ...; # 先设置 rewrite变量为true set $rewrite true; # 当客户端ip 是172.16.225.1 的时候 才不 rewrite if ( $remote_addr = "172.16.225.1" ) { set $rewrite = false; } if ( $rewirte = true) { # 将rewrite到 维护界面 weihu.html rewrite (.+) /weihu.html; } location /weihu.html { # 如果要使用 echo 则需要加载 echo-nginx-module 才行 echo "remote_addr: $remote_addr"; root /www/weihu/; } location / { # 如果要使用 echo 则需要加载 echo-nginx-module 才行 echo "remote_addr: $remote_addr"; # 如果是 remote_addr 是特定的ip 则直接正常访问后台 proxy_pass http://www.testbackend.com; } }

此时如果是 172.16.225.1 访问就可以到 后端, 如果是其他的客户端ip 访问就只能到 weihu.html 页面

注意 下面是 echo-nginx-module 模块需要单独下载加载 如果你需要调试的话

git clone https://gitee.com/yaowenqiang/echo-nginx-module.git

总结

本篇主要介绍了 nginx 中 rewrite 的基本概念 以及基本的使用方式 ,rewrite 可以出现在 server , location , if 中

并且介绍了 什么时候才会变化浏览器URL , 以及介绍了2个模拟场景

  • rewirte 内部站点

当rewrite 到内部站点的时候 会根据 flag 参数 last break不变 redirect permanent 变化

  • rewrite 外部站点 带http/https 等

当rewrite 外部站点 不管flag 参数 浏览器URL都会进行变化 相当于浏览器进行了 二次请求了

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

凤舞飘伶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值