目录

名字

ip-restriction 可以通过以下方式限制对服务或路线的访问,将 IP 地址列入白名单或黑名单。 单个 IP 地址,多个 IP 地址 或 CIDR 范围,可以使用类似 10.10.10.0/24 的 CIDR 表示法。

属性

参数名类型可选项默认值有效值描述
whitelistarray[string]可选加入白名单的 IP 地址或 CIDR 范围
blacklistarray[string]可选加入黑名单的 IP 地址或 CIDR 范围

只能单独启用白名单或黑名单,两个不能一起使用。

如何启用

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

curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
    "uri": "/index.html",
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "127.0.0.1:1980": 1
        }
    },
    "plugins": {
        "ip-restriction": {
            "whitelist": [
                "127.0.0.1",
                "113.74.26.106/24"
            ]
        }
    }
}'

测试插件

通过 127.0.0.1 访问:

$ curl http://127.0.0.1:9080/index.html -i
HTTP/1.1 200 OK
...

通过 127.0.0.2 访问:

$ curl http://127.0.0.1:9080/index.html -i --interface 127.0.0.2
HTTP/1.1 403 Forbidden
...
{"message":"Your IP address is not allowed"}

禁用插件

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

$ curl http://127.0.0.1:2379/v2/keys/apisix/routes/1  -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d value='
{
    "uri": "/index.html",
    "plugins": {},
    "upstream": {
        "type": "roundrobin",
        "nodes": {
            "39.97.63.215:80": 1
        }
    }
}'

现在就已移除 ip-restriction 插件,其它插件的开启和移除也类似。