在将 request_uri 传递给 proxy_pass 之前替换它的一部分 [英] replace part of request_uri before passing it to proxy_pass

查看:54
本文介绍了在将 request_uri 传递给 proxy_pass 之前替换它的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用重写替换特定部分或 request_uri,但由于某种原因它不起作用

i'm trying to replace specific part or request_uri using rewrite, but it won't work for some reason

示例网址: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;
    }

}

所以这里的目标是替换 'src1=' 和 '&src2' 之间的确切字符串,以便可以将更改后的字符串传递给 proxy_pass

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

推荐答案

locationrewrite 指令使用 规范化 URI 不包含查询字符串(从 ? 开始的任何内容).

要操作查询字符串,您需要查看 $request_uri$args 变量,或使用 $arg_ 的各个参数code> 变量族(例如 $arg_src1).

The location and rewrite directives use a normalised URI which does not include the query string (anything from the ? onwards).

最简单的解决方案可能是在向上游传递新值之前使用 map 指令来操作 $request_uri.

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).

例如:

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.

这篇关于在将 request_uri 传递给 proxy_pass 之前替换它的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆