剔除任意指定参数配置
只需要修改需要剔除的参数key(如:redirectUrl)
#剔除$args中的redirectUrl 参数 server { listen 80; server_name www.mynginx.rewrite; location /search {
#在参数前加入&,并赋值给args_tmp set $args_tmp &${args};
#正则判断进行剔除 if ( $args_tmp ~* ^(.*)(&redirectUrl=[\w%@\.]*)(.*)$){ set $args $1$3; }
#除去参数开头可能带有&字符的情况 if ( $args ~* ^&(.*)$){ set $args $1; }
#最终的$args为剔除 redirectUrl参数后的$args
rewrite ^/search http://www.text.com/text?$args? break;
}
}
测试项:
http://www.mynginx.rewrite/search?redirectUrl=abc.@&abcd=1234&token=xxxxx
http://www.mynginx.rewrite/search?token=xxxxx&redirectUrl=abc.@&abcd=1234
http://www.mynginx.rewrite/search?token=xxxxx&abcd=1234&redirectUrl=abc.@
测试结果分别是:
http://www.text.com/text?abcd=1234&token=xxxxx
http://www.text.com/text?token=xxxxx&abcd=1234
http://www.text.com/text?token=xxxxx&abcd=1234
某一参数转为访问路径
将$args 参数数组中的某一参数转为访问目录,并在$args 中剔除(原理如上)
server { listen 80; server_name www.mynginx.rewrite; location /search { set $args_tmp &${args}; if ( $args_tmp ~* ^(.*)(&redirectUrl=\w*)(.*)$){
set $arg_path $arg_redirectUrl; #获取参数变量redirectUrl的值 set $args $1$3; rewrite ^/search /text/$arg_path; } } location ~* /text { if ( $args ~* ^&(.*)$){#除去参数开头可能带有&字符的情况 set $args $1; } rewrite ^/text/(.*) http://www.text.com/text/$1?$args? break; } }
测试项:
http://www.mynginx.rewrite/search?redirectUrl=abc&abcd=1234&token=xxxxx
http://www.mynginx.rewrite/search?token=xxxxx&redirectUrl=abc&abcd=1234
http://www.mynginx.rewrite/search?token=xxxxx&abcd=1234&redirectUrl=abc
测试结果分别是:
http://www.text.com/text/abc?abcd=1234&token=xxxxx
http://www.text.com/text/abc?token=xxxxx&abcd=1234
http://www.text.com/text/abc?token=xxxxx&abcd=1234
注:测试前请在C:\Windows\System32\drivers\etc\hosts中配置DNS,有疑问请在评论区留言。
知识虽不属于个人,但请尊重每位笔者。
转载请指明出处:https://www.cnblogs.com/dennyLee2025/p/13742497.html