ApiSix常用api接入配置方法

配置基于apisix 3.9.1版本

場景1:Get Api增加 uri 參數(shù),配置示例

#curl "http://192.168.11.159:8080/v2/open-api/muti-icp-info?dm=52pojie.cn----->apisix補齊這個參數(shù)(&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后插入請求頭,其他參數(shù)不改變,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內(nèi)容,body增加zone_key_id = "1111111111111111ae"參數(shù)

#注意事項,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
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容