动态Add_header

1. 功能描述

该功能主要将add_header指令动态化,可以通过动态api接口进行配置。

2. 依赖模块

Add_header功能依赖模块:

load_module modules/njt_http_dyn_header_module.so;

3. 指令说明

Syntax: add_header key value [always];
Default:
Context: http, server, location, if in location

key header名,可配置常量或者变量。

value 变量值,可包含变量。

always always为true时忽略返回码。 默认当返回码为200, 201 (1.3.10), 204, 206, 301, 302, 303, 304, 307 (1.1.16, 1.0.13), or 308 才显示需要添加的header。

  1. 配置说明

helper broker modules/njt_helper_broker_module.so conf/mqtt.conf;
helper ctrl modules/njt_helper_ctrl_module.so conf/ctrl.conf;
 
load_module modules/njt_http_dyn_header_module.so;

user  root root;
worker_processes  2;

cluster_name helper;
node_name node1;



error_log  logs/error.log info;
pid        logs/njet.pid;


events {
    worker_connections  1024;
}


http {
  
    dyn_kv_conf conf/iot-work.conf;
    include       mime.types;
    default_type  application/octet-stream;
   
    default_type text/html;
 
    access_log  logs/access.log;

    variables_hash_max_size  2048;
  
    sendfile        on;

    keepalive_timeout  65;

    upstream backend1 {

        zone backend1 128k;

        server 127.0.0.1:5889;

    }
   

    server {
  
        listen 5555;
        server_name localhost;
  
         set $upstream backend2;
         set $upstream_name backend1;  
 
         location / {

              proxy_pass http://backend1;
         }


        location /add_header {
         
                   proxy_pass http://backend1?test=$host/;

         }
 
        location ($uri = /test_header) {
          
                  proxy_pass http://backend1;
        }
   
    }

     }

    server {

        listen 5889;

        return 200 "8001 $request_uri";

     }

}

4. 调用样例

4.1 API说明

查询接口:

GET http://IP+port/api/v1/config/http_dyn_header

修改接口:

PUT http://IP+port/api/v1/config/http_dyn_header

4.2 增加一个header

使用curl向ctrl端口8081发送

curl -X PUT http://127.0.0.1:8081/api/v1/config/http_dyn_header -d '{
  "servers": [
    {
      "listens": [
        "0.0.0.0:5555"
      ],
      "serverNames": [
        "localhost"
      ],
      "locations": [
        {
          "location": "/add_header",
          "headers": [
             {
                "key": "test",
                "value": "abcdef!",
                "always": false
             }
          ]
        }
      ]
    }
  ]
}'

返回

{"code":0,"msg":"success."}

再通过动态header接口查看

curl -X GET http://127.0.0.1:8081/api/v1/config/http_dyn_header

返回

{
  "servers": [
    {
      "listens": [
        "0.0.0.0:5555"
      ],
      "serverNames": [
        "localhost"
      ],
      "locations": [
        {
          "location": "/"
        },
        {
          "location": "/add_header",
          "headers": [
            {
              "key": "test",
              "value": "abcdef!",
              "always": false
            }
          ]
        },
        {
          "location": "($uri = /test_header)"
        }
      ]
    }
  ]
}

4.3 移除通过add_header指令添加的header,对其余header无影响

使用curl向ctrl端口8081发送

curl -X PUT http://127.0.0.1:8081/api/v1/config/http_dyn_header -d '{
  "servers": [
    {
      "listens": [
        "0.0.0.0:5555"
      ],
      "serverNames": [
        "localhost"
      ],
      "locations": [
        {
          "location": "/add_header",
          "headers": [
          ]
        }
      ]
    }
  ]
}'

返回

{"code":0,"msg":"success."}

再通过动态header接口查看

curl -X GET http://127.0.0.1:8081/api/v1/config/http_dyn_header

返回

{
  "servers": [
    {
      "listens": [
        "0.0.0.0:5555"
      ],
      "serverNames": [
        "localhost"
      ],
      "locations": [
        {
          "location": "/"
        },
        {
          "location": "/add_header",
          "headers": [
          ]
        },
        {
          "location": "($uri = /test_header)"
        }
      ]
    }
  ]
}

4.4 查询当前header

使用curl向ctrl端口8081发送

curl -X GET http://127.0.0.1:8081/api/v1/config/http_dyn_header

返回

{
  "servers": [
    {
      "listens": [
        "0.0.0.0:5555"
      ],
      "serverNames": [
        "localhost"
      ],
      "locations": [
        {
          "location": "/"
        },
        {
          "location": "/add_header",
          "headers": [
            {
              "key": "test",
              "value": "abcdef!",
              "always": false
            }
          ]
        },
        {
          "location": "($uri = /test_header)"
        }
      ]
    }
  ]
}