limit-count

    You can enable the Plugin on a Route as shown below:

    You can also configure the key_type to var_combination as shown:

    1. curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "uri": "/index.html",
    4. "plugins": {
    5. "limit-count": {
    6. "count": 2,
    7. "time_window": 60,
    8. "rejected_code": 503,
    9. "key_type": "var_combination",
    10. "key": "$consumer_name $remote_addr"
    11. }
    12. },
    13. "upstream": {
    14. "type": "roundrobin",
    15. "nodes": {
    16. "127.0.0.1:9001": 1
    17. }
    18. }
    19. }'

    You can also create a group to share the same counter across multiple Routes:

    1. curl -i http://127.0.0.1:9080/apisix/admin/services/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "plugins": {
    4. "limit-count": {
    5. "count": 1,
    6. "time_window": 60,
    7. "rejected_code": 503,
    8. "group": "services_1#1640140620"
    9. }
    10. },
    11. "upstream": {
    12. "type": "roundrobin",
    13. "nodes": {
    14. "127.0.0.1:1980": 1
    15. }
    16. }
    17. }'
    1. curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "service_id": "1",
    4. "uri": "/hello"
    5. }'
    1. curl -i http://127.0.0.1:9080/apisix/admin/routes/2 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
    2. {
    3. "service_id": "1",
    4. "uri": "/hello2"
    5. }'

      You can also share the same limit counter for all your requests by setting the key_type to constant:

      1. curl -i http://127.0.0.1:9080/apisix/admin/services/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
      2. {
      3. "plugins": {
      4. "limit-count": {
      5. "count": 1,
      6. "time_window": 60,
      7. "rejected_code": 503,
      8. "key": "remote_addr",
      9. "key_type": "constant",
      10. "group": "services_1#1640140621"
      11. }
      12. },
      13. "upstream": {
      14. "type": "roundrobin",
      15. "nodes": {
      16. "127.0.0.1:1980": 1
      17. }
      18. }
      19. }'

      Now every request will share the same count limitation regardless of their remote address.

      For cluster-level traffic limiting, you can use a Redis server. The counter will be shared between different APISIX nodes to achieve traffic limiting.

      1. curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
      2. {
      3. "uri": "/index.html",
      4. "plugins": {
      5. "limit-count": {
      6. "count": 2,
      7. "time_window": 60,
      8. "rejected_code": 503,
      9. "key": "remote_addr",
      10. "policy": "redis",
      11. "redis_host": "127.0.0.1",
      12. "redis_port": 6379,
      13. "redis_password": "password",
      14. "redis_database": 1,
      15. "redis_timeout": 1001
      16. }
      17. },
      18. "upstream": {
      19. "type": "roundrobin",
      20. "nodes": {
      21. "127.0.0.1:1980": 1
      22. }
      23. }

      Similarly you can also configure the redis-cluster policy:

      1. curl -i http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
      2. {
      3. "uri": "/index.html",
      4. "plugins": {
      5. "limit-count": {
      6. "count": 2,
      7. "time_window": 60,
      8. "rejected_code": 503,
      9. "key": "remote_addr",
      10. "redis_cluster_nodes": [
      11. "127.0.0.1:5000",
      12. "127.0.0.1:5001"
      13. ],
      14. "redis_password": "password",
      15. "redis_cluster_name": "redis-cluster-1"
      16. }
      17. },
      18. "upstream": {
      19. "type": "roundrobin",
      20. "nodes": {
      21. "127.0.0.1:1980": 1
      22. }
      23. }
      24. }'

      The above configuration limits to 2 requests in 60 seconds. The first two requests will work and the response headers will contain the headers X-RateLimit-Limit and X-RateLimit-Remaining:

      1. HTTP/1.1 200 OK
      2. Content-Type: text/html
      3. Content-Length: 13175
      4. Connection: keep-alive
      5. X-RateLimit-Limit: 2
      6. X-RateLimit-Remaining: 0
      7. Server: APISIX web server

      When you visit for a third time in the 60 seconds, you will receive a response with 503 code:

      1. HTTP/1.1 503 Service Temporarily Unavailable
      2. Content-Type: text/html
      3. Content-Length: 194
      4. Connection: keep-alive
      5. Server: APISIX web server
      6. <html>
      7. <head><title>503 Service Temporarily Unavailable</title></head>
      8. <body>
      9. <center><h1>503 Service Temporarily Unavailable</h1></center>
      10. <hr><center>openresty</center>
      11. </body>
      12. </html>
      1. HTTP/1.1 503 Service Temporarily Unavailable
      2. Content-Type: text/html
      3. Content-Length: 194
      4. Connection: keep-alive
      5. Server: APISIX web server
      6. {"error_msg":"Requests are too frequent, please try again later."}

      To disable the limit-count Plugin, you can delete the corresponding JSON configuration from the Plugin configuration. APISIX will automatically reload and you do not have to restart for this to take effect.

      1. curl http://127.0.0.1:9080/apisix/admin/routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
      2. {
      3. "methods": ["GET"],
      4. "uri": "/index.html",
      5. "upstream": {
      6. "type": "roundrobin",
      7. "nodes": {
      8. "127.0.0.1:1980": 1
      9. }
      10. }