How can global variables be set in nginx?

Global variables can be set in nginx using the “set” directive. Here is an example of how to set global variables.

http {
    # 设置全局变量
    set $my_variable "hello world";

    server {
        location / {
            # 使用全局变量
            echo $my_variable;
        }
    }
}

In the example above, we set $my_variable to “hello world” using the set directive. Next, in the location block, we use the echo directive to output the value of this global variable.

bannerAds