• Welcome to LiuJason's Blog!

Nginx中rewrite改写url并替换其中的字符

Linux笔记 Jason 3 years ago (2021-01-24) 634 Views 0 Comments
文章目录[隐藏]

前言

由于某个API的特殊需求,在url中会出现两次?,例如:

https://api.cloudraft.cn/?https://api.cloudflare.com/client/v4/zones/03bfeae5bfad300000000563011154cb/dns_records?&per_page=10&page=1

这种时候如果要在nginx中改写会自动把这个问号给去掉,变成:

https://api.cloudraft.cn/?https://api.cloudflare.com/client/v4/zones/03bfeae5bfad300000000563011154cb/dns_records&per_page=10&page=1

这种时候用就需要用nginx改写成转义后的符号了...

解决方案

#REWRITE-START
        rewrite ^/client/v4/(.*)?(.*)$ https://api.cloudraft.cn/?https://api.cloudflare.com/client/v4/$1%3F$2 redirect;
#REWRITE-END

讲解

  1. rewrite声明重写动作;
  2. ^/client/v4/匹配路径前面的内容;
  3. (.*)通配符,代表字符串变量1;
  4. ?匹配问号;
  5. (.*)$通配符结尾,代表字符串变量2;
  6. https://api.cloudraft.cn/?https://api.cloudflare.com/client/v4/替换2中的内容;
  7. $1替换3中的字符串;
  8. %3F?的转义符;
  9. $2替换5中的字符串;
  10. redirect;声明302重定向。

This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/1152.html
Like (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址