ApiSix常用api接入配置方法

配置基于apisix 3.9.1版本

场景1:Get Api增加 uri 参数,配置示例

#curl "http://192.168.11.159:8080/v2/open-api/muti-icp-info?dm=52pojie.cn----->apisix补齐这个参数(&uc=w&key=h)"
#应用场景,uc和key分别表示账号和密码,如果源站安全措施较弱,可使用apisix代理加固,避免暴露源站账密
{
  "uri": "/v2/*",
  "name": "xuanji",
  "methods": [
    "GET",
    "POST",
    "HEAD",
    "OPTIONS"
  ],
  "plugins": {
    "serverless-pre-function": {
      "functions": [
        "return function(conf, ctx) local args = ngx.req.get_uri_args(); args.uc = 'w'; args.key = 'h'; ngx.req.set_uri_args(args) end"
      ],
      "phase": "rewrite"
    }
  },
  "upstream_id": "583645207988274031",
  "status": 1
}

场景2:GET Api 实时计算token后插入请求头,其他参数不改变,ngx.req.set_header("Token", token); ngx.req.set_header("Timespan", timespan);

{
  "uri": "/EnfoVerify/*",
  "name": "cc",
  "methods": [
    "GET",
    "POST"
  ],
  "plugins": {
    "hmac-auth": {},
    "proxy-rewrite": {
      "host": "api.qica.com",
      "method": "GET",
      "scheme": "https",
      "uri": "/EnfoVerify/GetInfo?key=44"
    },
    "serverless-pre-function": {
      "disable": false,
      "functions": [
        "return function(conf, ctx) local core = require(\"apisix.core\"); local appKey = \"44\"; local secretKey = \"64251833\"; local timespan = tostring(ngx.time()); local token_src = appKey .. timespan .. secretKey; local md5 = require(\"resty.md5\"); local digest = md5.new(); digest:update(token_src); local function to_hex(str) return (string.gsub(str, \".\", function(c) return string.format(\"%02x\", string.byte(c)) end)) end; local token = string.upper(to_hex(digest:final())); ngx.req.set_header(\"Token\", token); ngx.req.set_header(\"Timespan\", timespan); core.log.warn(\"[GET] Added headers: Token=\", token, \", Timespan=\", timespan) end"
      ],
      "phase": "rewrite"
    }
  },
  "upstream_id": "585782187589108591",
  "status": 1
}

场景3:Post Api修改body内容,body增加zone_key_id = "1111111111111111ae"参数

#注意事项,apisix会自动增加client ip到X-Forwarded-Host,如果源站校验,需要在proxy-rewrite-->headers-->X-Forwarded-Host值,类似的请求头还有其他的,可以按类似方法处理
{
  "uri": "/zero/api/*",
  "name": "0one",
  "plugins": {
    "hmac-auth": {},
    "proxy-rewrite": {
      "headers": {
        "set": {
          "Host": "0.one",
          "X-Forwarded-Host": "0.one"
        }
      },
      "host": "0.one",
      "method": "POST",
      "scheme": "https",
      "uri": "/api/data/"
    },
    "serverless-pre-function": {
      "disable": false,
      "functions": [
        "return function(conf, ctx) local core = require(\"apisix.core\"); local headers = ngx.req.get_headers(); local function safe_get(h, key); return h[key] or h[string.lower(key)]; end ;local ct = headers[\"Content-Type\"]; if not ct or not string.find(ct, \"application/json\", 1, true) then core.log.info(\"skip non-json request, Content-Type: \", ct or \"nil\"); return end; local body = core.request.get_body(); if not body or body == \"\" then body = \"{}\" end; local json, err = core.json.decode(body); if not json then core.log.warn(\"invalid json, using empty object. error: \", err); json = {} end; json.zone_key_id = \"1111111111111111ae\"; local new_body, encode_err = core.json.encode(json); if not new_body then core.log.error(\"failed to encode json: \", encode_err); return end; ngx.req.set_body_data(new_body); ngx.req.clear_header(\"Content-Length\");ngx.req.set_header(\"Content-Length\", string.len(new_body));end"
      ],
      "phase": "rewrite"
    }
  },
  "upstream_id": "585782187589108591",
  "status": 1
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容