问题
i'm trying to replace specific part or request_uri using rewrite, but it won't work for some reason
example url: http://example.com:3000/?soft=55191&src1=changethis&src2=HOME&type=0&id=7700458
server {
server_name example.com;
listen 3000;
location / {
resolver 8.8.8.8;
rewrite ^(?<=&src1=)(.*)(?=&src2)$ changewiththis$1 break;
proxy_pass http://example2.com;
}
}
so the Goal here is to replace the exact string between 'src1=' and '&src2' so it can be passed to proxy_pass with the changed string
回答1:
The location
and rewrite
directives use a normalised URI which does not include the query string (anything from the ?
onwards).
To manipulate the query string, you will need to look at the $request_uri
or $args
variables, or the individual parameters using the $arg_
family of variables (e.g. $arg_src1
).
The simplest solution may be to use a map
directive to manipulate $request_uri
before passing the new value upstream.
For example:
map $request_uri $changethis {
default $request_uri;
~(?<prefix>.*[?](|.*&)src1)=[^&]*(?<suffix>.*)$ $prefix=newvalue$suffix;
}
server {
...
location / {
resolver ...;
proxy_pass http://example.com$changethis;
}
}
See this document for details.
来源:https://stackoverflow.com/questions/57281041/replace-part-of-request-uri-before-passing-it-to-proxy-pass