Rate Limit
Rate limiting is a way to control incoming traffic to a network. For example, with Rate Limit you can specify that a user may only send a certain number of requests per minute, and if the number of requests exceeds that, an error is returned. The goals of implementing rate limiting are:
- Better management of traffic flow
- Increased security by preventing attacks such as DDoS, brute-force attacks, or any other malicious attack at the application layer
Arvancloud Rate Limiting Settings
With Arvancloud rate limiting, you can limit the number of requests per IP, per second, hour, or day, to a path or to your whole domain.
Since Arvancloud rate limiting lets you define different rules for different paths, you can have a different policy and limit for each path.
Rate Limiting Settings in the Panel
To apply a new limit rule, go to CDN > Rate Limit in the Arvancloud panel and click Add Rule.
In this section, you can set the following parameters to apply the limit:
- Limit path
- Description
- Number of requests
- Time period
- Allowed methods
- Allowed IP list
Note that the path must be entered based on the Glob pattern.
In the limit section, there are two fields: Number of Requests and Time Period. The number entered in Number of Requests is the maximum number of requests sent from one IP within the time period you specify. If an IP sends more requests than this within that time period, it is limited and receives a 429 error.
The Time Period field specifies the length of time during which the specified Number of Requests can be sent from one IP. If more requests are sent from the same IP within this period, the user is limited and receives a 429 error.
After specifying these two values, you can set that if an IP sends more requests than allowed, its requests are blocked with status code 429 for a specific length of time.
Next are the lists of allowed methods and IPs, which the limit defined in this rule does not apply to. In these two sections, you can specify which HTTP methods and IPs this limit is not applied to.
These fields and the description field are optional and can be left empty.
Finally, after entering the information and selecting Save, the limit is applied.
Setting the Rate Limit Module's Behavior
In the Rate Limit module, you can specify how Arvancloud should respond to requests when the request rate for a specific path exceeds the defined limit. In this case, subsequent requests can be blocked or faced with a DDoS challenge. If you choose the DDoS challenge, you can also set the protection level and the challenge's validity period.
Creating a rule with the Challenge action via API:
curl --location --request POST 'https://napi.arvancloud.ir/cdn/4.0/domains/example.com/rate-limit/rules' \
--header 'authority: napi.arvancloud.ir' \
--header 'accept: application/json, text/plain, */*' \
--header 'authorization: API KEY 1 2 3 4' \
--header 'cache-control: no-cache' \
--header 'content-type: application/json' \
--header 'origin: https://panel.arvancloud.ir' \
--header 'pragma: no-cache' \
--data-raw '{"is_enabled":true,"url_pattern":"example.com/**","description":"","exclude_sources":[],"rate":1,"burst":4000000,"block_duration":0,"time_duration":10,"allowed_methods":[],"action":"challenge","action_details":{"mode":1,"ttl":120,"https_only":false}}'
- When creating a rule with the Challenge action via API, the
modeparameter can be 1, 2, or 3: 1 is the Cookie challenge, 2 the JS challenge, and 3 the Captcha challenge. - The TTL parameter is how long the DDoS challenge is valid; until it expires, the user is not verified again.
Rate Limiting Settings via API
All Rate Limit settings that can be defined in the Arvancloud panel are also available through the API for this feature, as shown in the following table:
| Use | API |
|---|---|
| Get the domain's Rate-Limit settings | Get Rate-Limit settings |
| Change the domain's Rate-Limit settings | Update domain's Rate-Limit configuration |
| Define and add a new Rate-Limit rule | Store new Rate-Limit rule |
| Get the details of a Rate-Limit rule | Get Rate-Limit's rule information |
| Change the settings of a Rate-Limit rule | Update the Rate-Limit's rule |
| Delete a Rate-Limit rule | Delete Rate-Limit's rule |
| Change the priority of a Rate-Limit rule | Change priority of Rate-Limit's rules |
Example: Limiting Access to a Path to 20 Requests per Day
Suppose your website has a contact form. To prevent DDoS or brute-force attacks, you can set a limit appropriate to the number of requests received on the API that submits this form. For example, limit access to www.example.com/api/contact/form to 20 requests per day, and if an IP sends more than that, block it for 24 hours. Also, this limit should not apply to the GET method or to the source IP 1.2.3.4. You can apply this rule through the panel and the API as follows.
curl --location --request POST 'https://napi.arvancloud.ir/cdn/4.0/domains/example.com/rate-limit/rules' \
--header 'Content-Type: application/json' \
--header 'Accept: application/json' \
--header 'Authorization: Bearer <Bearer Token>' \
--data-raw '{
"url_pattern": "example.com/api/contact/form",
"rate": 20,
"time_duration": 86400,
"is_enabled": true,
"description": "test",
"exclude_sources": [
"1.2.3.4/32"
],
"burst": 4000000,
"block_duration": 86400,
"allowed_methods": [
"GET"
]
}'
Prioritizing Limits
When counting a user's requests and matching them against the existing rules, priorities are processed in order, starting with priority 1. That is, if a user hits the limit of rule 1, the other rules with lower priority are no longer checked for that user. By prioritizing different paths, you can set different policies for your site. For example, put less sensitive paths at the top of the list and more sensitive paths at the bottom of the rules list.
Suppose you want to limit users' access to the following two paths:
| Time | Number of Requests | Path |
|---|---|---|
| 60 s | 5 | example.com/api/login/** |
| 60 s | 10 | example.com/api/** |
In this case, it is better to set path 1 with a higher-priority rule (priority number 1) and path 2 with a lower priority (priority number 2).