1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| server { listen 8080;
location /foo { echo "foo = [$foo]"; }
location /bar { set $foo 32; echo "foo = [$foo]"; } } $ curl 'http://localhost:8080/foo' foo = []
$ curl 'http://localhost:8080/bar' foo = [32]
$ curl 'http://localhost:8080/foo' foo = []
|
Nginx 变量一旦创建,其变量名的可见范围就是整个 Nginx 配置,甚至可以跨越不同虚拟主机的 server
配置块,但是赋值操作在bar中实现,foo中的foo为空
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| server { listen 80; server_name www.example.com;
location /api/ { proxy_pass http://backend_api; }
location /static/ { root /var/www/html; }
location /admin { return 403; } }
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| map $args $foo { default 0; debug 1; }
server { listen 8080;
location /test { set $orig_foo $foo; set $args debug;
echo "original foo: $orig_foo"; echo "foo: $foo"; } }
|
1 2 3 4 5 6 7 8 9
| location /test { set $a 32; echo $a; set $a 50; echo $a; }
|