Nginx 允许在 location 块中使用 set 指令定义变量,并在 location 中引用这些变量。这使得您可以轻松地将常量或动态值传递到不同的 location 配置中。

例如:

http {
    # 定义常量
    set $my_var 'hello';
    set $another_var 'world';

    server {
        listen 80;

        location /test {
            # 引用常量
            add_header X-My-Header $my_var;
            add_header X-Another-Header $another_var;
        }
    }
}

在上面的示例中,我们定义了两个常量 $my_var$another_var,并分别赋值为 'hello' 和 'world'。然后,在 /testlocation 中,我们使用 add_header 指令引用了这些常量。当访问 /test 时,会在响应头中添加 X-My-Header: helloX-Another-Header: world

通过这种方式,您可以在 Nginx 的 location 块中使用常量或变量,使您的配置更加灵活和可维护。

Nginx Location 中引用常量:使用 set 指令定义变量

原文地址: https://www.cveoy.top/t/topic/mVy8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录