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

文章目录[隐藏]

前言

由于某个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
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy