location /test { set $orig_args $args; set $args "a=3&b=4"; echo "original args: $orig_args"; echo "args: $args"; }
这里我们把原始的 URL 参数串先保存在 $orig_args
变量中,然后通过改写 $args 变量来修改当前的 URL 参数串,最后我们用 echo 指令分别输出 $orig_args
和 $args 变量的值。接下来我们这样来测试这个 /test
接口:
$ curl 'http://localhost:8080/test' original args: args: a=3&b=4 $ curl 'http://localhost:8080/test?a=0&b=1&c=2' original args: a=0&b=1&c=2 args: a=3&b=4
在第一次测试中,我们没有设置任何 URL 参数串,所以输出 $orig_args
变量的值时便得到空。而在第一次和第二次测试中,无论我们是否提供 URL 参数串,参数串都会在location /test
中被强行改写成 a=3&b=4
.
location /test { set $orig_a $arg_a; set $args "a=5"; echo "original a: $orig_a"; echo "a: $arg_a"; }
这里我们先把内建变量 $arg_a
的值,即原始请求的 URL 参数 a
的值,保存在用户变量 $orig_a
中,然后通过对内建变量 $args 进行赋值,把当前请求的参数串改写为 a=5
,最后再用 echo 指令分别输出 $orig_a
和 $arg_a
变量的值。因为对内建变量 $args 的修改会直接导致当前请求的 URL 参数串发生变化,因此内建变量 $arg_XXX 自然也会随之变化。测试的结果证实了这一点:
$ curl 'http://localhost:8080/test?a=3' original a: 3 a: 5
我们看到,因为原始请求的 URL 参数串是 a=3
, 所以 $arg_a
最初的值为 3
, 但随后通过改写$args 变量,将 URL 参数串又强行修改为a=5
, 所以最终 $arg_a
的值又自动变为了 5
.
$args
变量影响标准的 HTTP 代理模块 ngx_proxy 的例子:
server { listen 8080; location /test { set $args "foo=1&bar=2"; proxy_pass http://127.0.0.1:8081/args; } } server { listen 8081; location /args { echo "args: $args"; } }
这里我们在 http
配置块中定义了两个虚拟主机。第一个虚拟主机监听 8080 端口,其 /test
接口自己通过改写$args 变量,将当前请求的 URL 参数串无条件地修改为foo=1&bar=2
. 然后 /test
接口再通过 ngx_proxy 模块的 proxy_pass 指令配置了一个反向代理,指向本机的 8081 端口上的 HTTP 服务 /args
. 默认情况下,ngx_proxy 模块在转发 HTTP 请求到远方 HTTP 服务的时候,会自动把当前请求的 URL 参数串也转发到远方。
location /args
中利用echo 指令输出当前请求的 URL 参数串,以检查/test
接口通过 ngx_proxy 模块实际转发过来的 URL 请求参数串。
/test
接口:
$ curl 'http://localhost:8080/test?blah=7' args: foo=1&bar=2
我们看到,虽然请求自己提供了 URL 参数串 blah=7
,但在 location /test
中,参数串被强行改写成了foo=1&bar=2
. 接着经由 proxy_pass 指令将我们被改写掉的参数串转发给了第二个虚拟主机上配置的 /args
接口,然后再把 /args
接口的 URL 参数串输出。事实证明,我们对$args 变量的赋值操作,也成功影响到了ngx_proxy 模块的行为。
#include <string> using namespace std; class Person { public: const string get_name() { return m_name; } void set_name(const string name) { m_name = name; } private: string m_name; };
在这个名叫 Person
的 C++ 类中,我们提供了 get_name
和 set_name
这两个公共方法,以作为私有成员变量m_name
的“存取器”。
Cookie
请求头中的相关定义的。