How to configure global variables in nginx

In order to set a global variable, you need to use the set directive in the configuration file of nginx. Here is an example:

http {
  # 定义全局变量
  set $my_var "Hello, World!";

  server {
    listen 80;
    server_name example.com;

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

In the example above, we defined a global variable named my_var using the set directive within the http block, and set its value to “Hello, World!”. Then, within the location block of the server, we used the echo directive to output the value of this global variable.

You can set global variables anywhere as needed and use them in other parts of the configuration file. This makes it convenient to share the values of variables without having to repeatedly define or pass them.

bannerAds