如果你想要复用一组通用的插件配置,你可以把它们提取成一个 Plugin config,并绑定到对应的路由上。

    举个例子,你可以这么做:

    1. # 创建 Plugin config
    2. $ curl http://127.0.0.1:9080/apisix/admin/plugin_configs/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
    3. {
    4. "desc": "吾乃插件配置1",
    5. "plugins": {
    6. "limit-count": {
    7. "count": 2,
    8. "time_window": 60,
    9. "rejected_code": 503
    10. }
    11. }
    12. }'
    13. # 绑定到路由上
    14. $ curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
    15. {
    16. "uris": ["/index.html"],
    17. "plugin_config_id": 1,
    18. "upstream": {
    19. "type": "roundrobin",
    20. "nodes": {
    21. "39.97.63.215:80": 1
    22. }
    23. }
    24. }'

    如果找不到对应的 Plugin config,该路由上的请求会报 503 错误。

    如果这个路由已经配置了 plugins,那么 Plugin config 里面的插件配置会合并进去。 相同的插件会覆盖掉 plugins 原有的插件。

    举个例子:

    1. {
    2. "desc": "吾乃插件配置1",
    3. "plugins": {
    4. "ip-restriction": {
    5. "whitelist": [
    6. "127.0.0.0/24",
    7. "113.74.26.106"
    8. ]
    9. },
    10. "limit-count": {
    11. "count": 2,
    12. "time_window": 60,
    13. "rejected_code": 503
    14. }
    15. }
    16. }

    加上

    1. {
    2. "uris": ["/index.html"],
    3. "plugin_config_id": 1,
    4. "upstream": {
    5. "type": "roundrobin",
    6. "nodes": {
    7. "39.97.63.215:80": 1
    8. }
    9. }
    10. "plugins": {
    11. "proxy-rewrite": {
    12. "uri": "/test/add",
    13. "scheme": "https",
    14. "host": "apisix.iresty.com"
    15. },
    16. "limit-count": {
    17. "count": 20,
    18. "time_window": 60,
    19. "rejected_code": 503,
    20. "key": "remote_addr"
    21. }
    22. }
    23. }

    等于

    1. {
    2. "uris": ["/index.html"],
    3. "upstream": {
    4. "type": "roundrobin",
    5. "nodes": {
    6. "39.97.63.215:80": 1
    7. }
    8. }
    9. "plugins": {
    10. "ip-restriction": {
    11. "whitelist": [
    12. "127.0.0.0/24",
    13. "113.74.26.106"
    14. ]
    15. },
    16. "proxy-rewrite": {
    17. "uri": "/test/add",
    18. "scheme": "https",
    19. "host": "apisix.iresty.com"
    20. },
    21. "limit-count": {
    22. "count": 2,
    23. "time_window": 60,
    24. "rejected_code": 503
    25. }
    26. }
    27. }