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. ```shell
  2. curl http://127.0.0.1:9180/apisix/admin/plugin_configs/1 \
  3. -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  4. {
  5. "desc": "blah",
  6. "plugins": {
  7. "limit-count": {
  8. "count": 2,
  9. "time_window": 60,
  10. "rejected_code": 503
  11. }
  12. }
  13. }'
  14. ```
  15. ```shell
  16. curl http://127.0.0.1:9180/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -i -d '
  17. {
  18. "uris": ["/index.html"],
  19. "plugin_config_id": 1,
  20. "upstream": {
  21. "type": "roundrobin",
  22. "nodes": {
  23. "127.0.0.1:1980": 1
  24. }
  25. }
  26. }'
  27. ```

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. }'