首页 > 其他分享 >apisix Admin API配置路由、证书

apisix Admin API配置路由、证书

时间:2024-09-27 13:50:39浏览次数:1  
标签:http Admin admin API key test apisix

apisix Admin API配置路由、证书

  这里记录了一些个人常用的基本配置,其他详细配置参考官方文档(https://apisix.incubator.apache.org/zh/docs/apisix/3.9/getting-started/README/)。

配置前准备

  1、安装yq工具

wget https://github.com/mikefarah/yq/releases/download/v4.44.2/yq_linux_amd64.tar.gz
tar zxf yq_linux_amd64.tar.gz
cp yq_linux_amd64 /usr/local/bin/yq

  2、配置admin_key变量

admin_key=$(yq '.deployment.admin.admin_key[0].key' /usr/local/apisix/conf/config.yaml | sed 's/"//g')

  3、安装jq工具

yum -y install jq

 

配置路由

一、配置反向代理

  配置一个uri为/test1/,转发到上游服务器192.168.1.110的80端口。

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -H "X-API-KEY: $admin_key"  -d '
{
  "id": "test",
  "uri": "/test1/*",
  "upstream": {"nodes": {
      "192.168.1.110:80": 1
    }
  }
}'

 

二、配置301跳转。

  这里使用redirect插件配置了一个自动加/的功能。

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -H "X-API-KEY: $admin_key"  -d '
{
  "id": "test301",
  "uri": "/test",
  "plugins": {
    "redirect": {
      "uri": "/test/",
      "ret_code": 301
    }
  }
}'

 

三、查看所有路由信息

curl http://127.0.0.1:9180/apisix/admin/routes  -H "X-API-KEY: $admin_key "  | jq 

 

四、查看所有路由的id信息

  这里的id是唯一信息,如果配置的id重复了,会覆盖之前的配置,就相当于是修改路由操作。

curl http://127.0.0.1:9180/apisix/admin/routes  -H "X-API-KEY: $admin_key " | jq '.list[].value.id'

 

五、负载均衡

  这里只配置了轮训模式,nodes中后面的数值是权重,数值越大,权重越高。

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -H "X-API-KEY: $admin_key"  -d '
{
  "id": "test-load",
  "uri": "/test-load/*",
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "192.168.1.110:80": 1,
      "192.168.1.111.:80": 1
    }
  }
}'

 

六、健康检查

  这里介绍主动健康检查,设置每3秒钟检查一次,成功1次则认为是健康(状态码200,302),失败2次则认为不健康,流量将不被转发到该节点。被动模式参考官网配置。

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -H "X-API-KEY: $admin_key"  -d '
{
  "id": "test-rewrite",
  "uri": "/test-rewrite/*",
  "upstream": {
    "type": "roundrobin",
    "nodes": {
      "192.168.110.18:80": 1,
      "192.168.110.17:80": 1
    },
    "checks": {
      "active": {
        "timeout": 5,
        "http_path": "/",
        "healthy": {
          "interval": 3,
          "successes": 1
        },
        "unhealthy": {
          "interval": 3,
          "http_failures": 2
        }
      }
    }
  }
}'

  使用健康检查后,可以使用 http://127.0.0.1:9090/v1/healthcheck接口对状态进行查看,比如

curl  http://127.0.0.1:9090/v1/healthcheck | jq '.[].nodes '| jq '.[] | select (.status == "unhealthy")' 

 

七、重写

  这里使用proxy-rewrite插件,将开头为/test-rewirte/的url去掉,将后面的所有内容转发给上游服务器。

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -H "X-API-KEY: $admin_key"  -d '
{
  "id": "test-rewrite",
  "uri": "/test-rewrite/*",
  "plugins": {
    "proxy-rewrite": {
      "regex_uri": ["^/test-rewrite/(.*)$", "/$1"]
    }
  },
  "upstream": {
    "nodes": {
      "192.168.1.110:80": 1
    }
  }
}'

 

八、证书

  1、创建ssl对象

curl http://127.0.0.1:9180/apisix/admin/ssls/cy.cqjyck.com -H "X-API-KEY: $admin_key" -X PUT -d '
{
     "cert" : "'"$(cat /home/test/ssl/www.test.com.pem)"'",
     "key": "'"$(cat /home/test/ssl/www.test.com.key)"'",
     "snis": ["www.test.com"]
}'

  2、创建路由

curl -i "http://127.0.0.1:9180/apisix/admin/routes" -X PUT -H "X-API-KEY: $admin_key"  -d '
{
  "id": "www.test.com",
  "hosts": ["www.test.com"],
  "uri": "/test/*",
  "plugins": {
    "proxy-rewrite": {
      "regex_uri": ["^/test/(.*)$", "/$1"]
    }
  },
  "upstream": {
    "nodes": {
      "192.168.1.110:80": 1
    }
  }
}'

  3、本地验证

curl --resolve 'www.test.com:9443:127.0.0.1' https://www.test.com:9443/test/ -k -vvv

 

标签:http,Admin,admin,API,key,test,apisix
From: https://www.cnblogs.com/NanZhiHan/p/18420911

相关文章

  • 【API安全】crAPI靶场全解
    目录BOLAVulnerabilitiesChallenge1-Accessdetailsofanotheruser’svehicleChallenge2-AccessmechanicreportsofotherusersBrokenUserAuthenticationChallenge3-ResetthepasswordofadifferentuserExcessiveDataExposureChallenge4-Fin......
  • AM05 Workshop 2 - Data acquisition from Spotify API
    AM05Workshop2-DataacquisitionfromSpotifyAPIAM05Workshop2-DataacquisitionfromSpotifyAPIOverviewInthisworkshop,youwilllearnhowto:CreateaSpotifyApp:ObtainthenecessarycredentialstoaccesstheSpotifyAPI.RequestanAcces......
  • APP集成人脸识别接口-C#人脸识别API接口
    人脸识别技术是一种基于生物特征的识别技术,它通过捕捉和分析人脸特征来识别或验证个体身份。这项技术主要依赖于计算机视觉、图像处理和人工智能算法的结合,一般由第三方人工智能接口平台来提供,例如:翔云、阿里云等平台。人脸识别技术的应用可以大大提升身份验证的准确性,减......
  • C#车辆登记证识别API接口集成示例-车辆合格证识别免费的API接口
    车辆登记证识别接口是一种OCR技术,基于深度学习算法,可快速、精准、自动识别车辆登记证上的文字信息。目前,车辆合格证识别的应用场景主要集中在与车辆相关的各类业务流程中,特别是汽车行业、物流运输和车辆管理等领域。1.汽车销售与登记新车上牌:在新车销售过程中,车辆合......
  • 什么是API?
    ##什么是API?API是应用程序编程接口(ApplicationProgrammingInterface)的缩写,它定义了软件组件之间如何相互通信。API充当不同软件间的桥梁,允许应用程序使用另一个应用程序的功能或数据。###API的工作原理-**请求**:客户端(比如一个移动应用或者一个网页)向服务器发送请求。-**......
  • API接口开发实现一键智能化自动抓取电商平台热销商品数据支持高并发免费接入示例
    为了实现一键智能化自动抓取电商平台热销商品数据支持高并发免费接入,你可以使用Python编程语言和相关库(如requests、BeautifulSoup等)来开发一个API接口,也可以使用封装好的api接口获取,注册一个api账号获取key和secret。以下是一个简单的示例:#coding:utf-8"""Compatibleforpytho......
  • 电商API数据接口1688alibaba接口item_search_shop-获得店铺的所有商品接入演示
    一、接口功能item_search_shop接口是1688阿里巴巴提供的获取店铺所有商品的API接口,用户可以通过输入店铺ID,获取该店铺的所有商品信息。二、接口调用请求参数:seller_nick=b2b-2200733087881719de&start_price=0&end_price=0&q=&page=1&cid=参数说明:seller_nick:sid或者加密后的_sopi......
  • Web APIs 4:日期对象、时间戳、节点操作、swiper插件
    WebAPIs4(日期对象、节点操作、swiper插件)1.实例化日期对象获得当前时间:constdate=newDate()获得指定时间:constdata=newDate(‘2024-1-108:30:30’)2.日期对象方法方法作用说明getFullTear()获得年份获取四位年份getMonth()获得月份取值为0~11getDate()获取月......
  • OpenCV视频I/O(3)视频采集类VideoCapture之获取当前使用的视频捕获 API 后端的名称函数
    操作系统:ubuntu22.04OpenCV版本:OpenCV4.9IDE:VisualStudioCode编程语言:C++11算法描述getBackendName函数是OpenCV中VideoCapture类的一个方法,用于获取当前使用的视频捕获API后端的名称。这可以帮助开发者了解当前VideoCapture实例正在使用哪个后端来处理视......
  • 玄空飞星 九宫飞星 api接口
    所谓的玄空飞星,其实是一种以理气为主的风水堪舆学说,这种风水学说讲究的是风水随着时间推移的变化,我们研究玄空风水,就是要根据这种变化去改造风水。接口名称:风水-玄空飞星接口平台:https://doc.yuanfenju.com/fengshui/xuankong.html接口地址:https://api.yuanfenju.com/index.php......