Plugin Config

Description

Plugin Configs are used to extract commonly used Plugin configurations and can be bound directly to a Route.

While configuring the same plugin, only one copy of the configuration is valid. The order of precedence is always Consumer > Consumer Group > Route > Plugin Config > Service.

Example

The example below illustrates how to create a Plugin Config and bind it to a Route:

  1. curl http://127.0.0.1:9180/apisix/admin/plugin_configs/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  3. {
  4. "desc": "blah",
  5. "plugins": {
  6. "limit-count": {
  7. "count": 2,
  8. "time_window": 60,
  9. "rejected_code": 503
  10. }
  11. }
  12. }'
  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY:edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  3. {
  4. "uris": ["/index.html"],
  5. "plugin_config_id": 1,
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:1980": 1
  10. }
  11. }
  12. }'

When APISIX can’t find the Plugin Config with the id, the requests reaching this Route are terminated with a status code of 503.

Plugin Config - 图1note

If a Route already has the plugins field configured, the plugins in the Plugin Config will effectively be merged to it.

The same plugin in the Plugin Config will not override the ones configured directly in the Route. For more information, see Plugin.

For example, if you configure a Plugin Config as shown below:

  1. curl http://127.0.0.1:9180/apisix/admin/plugin_configs/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  3. {
  4. "desc": "I am plugin_config 1",
  5. "plugins": {
  6. "ip-restriction": {
  7. "whitelist": [
  8. "127.0.0.0/24",
  9. "113.74.26.106"
  10. ]
  11. },
  12. "limit-count": {
  13. "count": 2,
  14. "time_window": 60,
  15. "rejected_code": 503
  16. }
  17. }
  18. }'

to a Route as shown below,

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  3. {
  4. "uris": ["/index.html"],
  5. "plugin_config_id": 1,
  6. "upstream": {
  7. "type": "roundrobin",
  8. "nodes": {
  9. "127.0.0.1:1980": 1
  10. }
  11. }
  12. "plugins": {
  13. "proxy-rewrite": {
  14. "uri": "/test/add",
  15. "host": "apisix.iresty.com"
  16. },
  17. "limit-count": {
  18. "count": 20,
  19. "time_window": 60,
  20. "rejected_code": 503,
  21. "key": "remote_addr"
  22. }
  23. }
  24. }'

the effective configuration will be as the one shown below:

  1. curl http://127.0.0.1:9180/apisix/admin/routes/1 \
  2. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  3. {
  4. "uris": ["/index.html"],
  5. "upstream": {
  6. "type": "roundrobin",
  7. "nodes": {
  8. "127.0.0.1:1980": 1
  9. }
  10. }
  11. "plugins": {
  12. "ip-restriction": {
  13. "whitelist": [
  14. "127.0.0.0/24",
  15. "113.74.26.106"
  16. ]
  17. },
  18. "proxy-rewrite": {
  19. "uri": "/test/add",
  20. "host": "apisix.iresty.com"
  21. },
  22. "limit-count": {
  23. "count": 20,
  24. "time_window": 60,
  25. "rejected_code": 503
  26. }
  27. }
  28. }'