高性能微服务网关APISIX - 常用插件(2)

proxy-rewrite

简介

proxy-rewrite 是上游代理信息重写插件,支持对 schemeurihost 等信息的重写。

属性

NameTypeRequirementDefaultValidDescription
schemestring可选“http”[“http”, “https”]不推荐使用。应该在 Upstream 的 scheme 字段设置上游的 scheme。
uristring可选转发到上游的新 uri 地址。
methodstring可选[“GET”, “POST”, “PUT”, “HEAD”, “DELETE”, “OPTIONS”,“MKCOL”, “COPY”, “MOVE”, “PROPFIND”, “PROPFIND”,“LOCK”, “UNLOCK”, “PATCH”, “TRACE”]将route的请求方法代理为该请求方法。
regex_uriarray[string]可选转发到上游的新 uri 地址, 使用正则表达式匹配来自客户端的uri,当匹配成功后使用模板替换转发到上游的uri, 未匹配成功时将客户端请求的uri转发至上游。当uriregex_uri同时存在时,uri优先被使用。例如:["^/iresty/(.)/(.)/(.*)","/$1-$2-$3"] 第一个元素代表匹配来自客户端请求的uri正则表达式,第二个元素代表匹配成功后转发到上游的uri模板。
hoststring可选转发到上游的新 host 地址,例如:iresty.com
headersobject可选转发到上游的新headers,可以设置多个。头信息如果存在将重写,不存在则添加。想要删除某个 header 的话,把对应的值设置为空字符串即可。支持使用 Nginx 的变量,需要以 $ 开头,如 client_addr: $remote_addr :表示请求头 client_addr 为客户端IP。

如何启用

下面是一个示例,在指定的 route 上开启了 proxy-rewrite 插件:

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "methods": ["GET"],
    "uri": "/test/index.html",
    "plugins": {
        "proxy-rewrite": {
            "uri": "/test/home.html",
            "scheme": "http",
            "host": "iresty.com",
            "headers": {
                "X-Api-Version": "v1",
                "X-Api-Engine": "apisix",
                "X-Api-useless": ""
            }
        }
    },
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:80": 1
        }
    }
}'

测试插件

基于上述配置进行测试:

curl -X GET http://127.0.0.1:9080/test/index.html

发送请求,查看上游服务access.log,如果输出信息与配置一致:

127.0.0.1 - [26/Sep/2019:10:52:20 +0800] iresty.com GET /test/home.html HTTP/1.1 200 38 - curl/7.29.0 - 0.000 199 107

即表示 proxy-rewrite 插件生效了。

禁用插件

当你想去掉 proxy-rewrite 插件的时候,很简单,在插件的配置中把对应的 json 配置删除即可,无须重启服务,即刻生效:

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "methods": ["GET"],
    "uri": "/test/index.html",
    "plugins": {},
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:80": 1
        }
    }
}'

现在就已经移除了 proxy-rewrite 插件了。其他插件的开启和移除也是同样的方法。

proxy-cache

代理缓存插件,该插件提供缓存后端响应数据的能力,它可以和其他插件一起使用。该插件支持基于磁盘的缓存,未来也会支持基于内存的缓存。目前可以根据响应码、请求 Method 来指定需要缓存的数据,另外也可以通过 no_cache 和 cache_bypass 配置更复杂的缓存策略。

基于磁盘的缓存需要注意:

  1. 不能动态配置缓存的过期时间,只能通过后端服务响应头 Expires 或 Cache-Control 来设置过期时间,如果后端响应头中没有 Expires 或 Cache-Control,那么 APISIX 将默认只缓存10秒钟
  2. 如果后端服务不可用, APISIX 将返回502或504,那么502或504将被缓存10秒钟

参数

名称类型必选项默认值有效值描述
cache_strategystring可选disk[“disk”,“memory”]缓存策略,指定缓存数据存储在磁盘还是内存中
cache_zonestring可选disk_cache_one指定使用哪个缓存区域,不同的缓存区域可以配置不同的路径,在 conf/config.yaml 文件中可以预定义使用的缓存区域。当不使用默认值时,指定的缓存区域与 conf/config.yaml 文件中预定义的缓存区域不一致,缓存无效。
cache_keyarray[string]可选[“ h o s t " , " host", " host","request_uri”]缓存key,可以使用变量。例如:[“ h o s t " , " host", " host","uri”, “-cache-id”]
cache_bypassarray[string]可选是否跳过缓存检索,即不在缓存中查找数据,可以使用变量,需要注意当此参数的值不为空或非’0’时将会跳过缓存的检索。例如:["$arg_bypass"]
cache_methodarray[string]可选[“GET”, “HEAD”][“GET”, “POST”, “HEAD”]根据请求method决定是否需要缓存
cache_http_statusarray[integer]可选[200, 301, 404][200, 599]根据响应码决定是否需要缓存
hide_cache_headersboolean可选false是否将 Expires 和 Cache-Control 响应头返回给客户端
cache_controlboolean可选false是否遵守 HTTP 协议规范中的 Cache-Control 的行为
no_cachearray[string]可选是否缓存数据,可以使用变量,需要注意当此参数的值不为空或非’0’时将不会缓存数据
cache_ttlinteger可选300 秒当选项 cache_control 未开启或开启以后服务端没有返回缓存控制头时,提供的默认缓存时间

注:变量以$开头,也可以使用变量和字符串的结合,但是需要以数组的形式分开写,最终变量被解析后会和字符串拼接在一起。

conf/config.yaml 文件中的配置示例:

proxy_cache:                       # 代理缓存配置
    cache_ttl: 10s                 # 如果上游未指定缓存时间,则为默认缓存时间
    zones:                         # 缓存的参数
    - name: disk_cache_one         # 缓存名称(缓存区域),管理员可以通过admin api中的 cache_zone 字段指定要使用的缓存区域
      memory_size: 50m             # 共享内存的大小,用于存储缓存索引
      disk_size: 1G                # 磁盘大小,用于存储缓存数据
      disk_path: "/tmp/disk_cache_one" # 存储缓存数据的路径
      cache_levels: "1:2"          # 缓存的层次结构级别

示例

启用插件

示例一:cache_zone 参数默认为 disk_cache_one

1、为特定路由启用 proxy-cache 插件:

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "plugins": {
        "proxy-cache": {
            "cache_key":  ["$uri", "-cache-id"],
            "cache_bypass": ["$arg_bypass"],
            "cache_method": ["GET"],
            "cache_http_status": [200],
            "hide_cache_headers": true,
            "no_cache": ["$arg_test"]
        }
    },
    "upstream": {
        "nodes": {
            "127.0.0.1:1999": 1
        },
        "type": "roundrobin"
    },
    "uri": "/hello"
}'

测试:

$ curl http://127.0.0.1:9080/hello -i
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 6
Connection: keep-alive
Server: APISIX web server
Date: Tue, 03 Mar 2020 10:45:36 GMT
Last-Modified: Tue, 03 Mar 2020 10:36:38 GMT
Apisix-Cache-Status: MISS

hello

http status 返回200并且响应头中包含 Apisix-Cache-Status,表示该插件已启用。

2、验证数据是否被缓存,再次请求上边的地址:

$ curl http://127.0.0.1:9080/hello -i
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 6
Connection: keep-alive
Server: APISIX web server
Date: Tue, 03 Mar 2020 11:14:46 GMT
Last-Modified: Thu, 20 Feb 2020 14:21:41 GMT
Apisix-Cache-Status: HIT

hello

响应头 Apisix-Cache-Status 值变为了 HIT,说明数据已经被缓存

示例二:自定义 cache_zone 参数为 disk_cache_two

1、在 conf/config.yaml 文件中的指定缓存区域等信息:

proxy_cache:
    cache_ttl: 10s
    zones:
    - name: disk_cache_two
      memory_size: 50m
      disk_size: 1G
      disk_path: "/tmp/disk_cache_one"
      cache_levels: "1:2"

2、为特定路由启用 proxy-cache 插件:

$ curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "plugins": {
        "proxy-cache": {
            "cache_zone": "disk_cache_two",
            "cache_key":  ["$uri", "-cache-id"],
            "cache_bypass": ["$arg_bypass"],
            "cache_method": ["GET"],
            "cache_http_status": [200],
            "hide_cache_headers": true,
            "no_cache": ["$arg_test"]
        }
    },
    "upstream": {
        "nodes": {
            "127.0.0.1:1999": 1
        },
        "type": "roundrobin"
    },
    "uri": "/hello"
}'

测试:

$ curl http://127.0.0.1:9080/hello -i
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 6
Connection: keep-alive
Server: APISIX web server
Date: Tue, 03 Mar 2020 10:45:36 GMT
Last-Modified: Tue, 03 Mar 2020 10:36:38 GMT
Apisix-Cache-Status: MISS

hello

http status 返回200并且响应头中包含 Apisix-Cache-Status,表示该插件已启用。

3、验证数据是否被缓存,再次请求上面的地址:

$ curl http://127.0.0.1:9080/hello -i
HTTP/1.1 200 OK
Content-Type: application/octet-stream
Content-Length: 6
Connection: keep-alive
Server: APISIX web server
Date: Tue, 03 Mar 2020 11:14:46 GMT
Last-Modified: Thu, 20 Feb 2020 14:21:41 GMT
Apisix-Cache-Status: HIT

hello

响应头 Apisix-Cache-Status 值变为了 HIT,说明数据已经被缓存

示例3:指定 cache_zone 为 invalid_disk_cacheconf/config.yaml 文件中指定的缓存区域 disk_cache_one 不一致。

$ curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "plugins": {
        "proxy-cache": {
            "cache_zone": "invalid_disk_cache",
            "cache_key":  ["$uri", "-cache-id"],
            "cache_bypass": ["$arg_bypass"],
            "cache_method": ["GET"],
            "cache_http_status": [200],
            "hide_cache_headers": true,
            "no_cache": ["$arg_test"]
        }
    },
    "upstream": {
        "nodes": {
            "127.0.0.1:1999": 1
        },
        "type": "roundrobin"
    },
    "uri": "/hello"
}'
{"error_msg":"failed to check the configuration of plugin proxy-cache err: cache_zone invalid_disk_cache not found"}

响应错误信息,表示插件配置无效。

清除缓存数据

如何清理缓存的数据,只需要指定请求的 method 为 PURGE。

测试:

$ curl -i http://127.0.0.1:9080/hello -X PURGE
HTTP/1.1 200 OK
Date: Tue, 03 Mar 2020 11:17:35 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX web server

响应码为200即表示删除成功,如果缓存的数据未找到将返回404。

再次请求,缓存数据未找到返回404:

$ curl -i http://127.0.0.1:9080/hello -X PURGE
HTTP/1.1 404 Not Found
Date: Wed, 18 Nov 2020 05:46:34 GMT
Content-Type: text/plain
Transfer-Encoding: chunked
Connection: keep-alive
Server: APISIX web server

<html>
<head><title>404 Not Found</title></head>
<body>
<center><h1>404 Not Found</h1></center>
<hr><center>openresty</center>
</body>
</html>
禁用插件

移除插件配置中相应的 JSON 配置可立即禁用该插件,无需重启服务:

curl http://127.0.0.1:9080/apisix/admin/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "uri": "/hello",
    "plugins": {},
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:1999": 1
        }
    }
}'

这时该插件已被禁用。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值