Today I’d demonstrate another feature in OpenResty Edge: to limit the request rate by some keys.
Sometimes the clients send requests too fast, like under a denial of service attack. In such cases, we should limit the request rate to protect both the gateway servers and origin servers. Otherwise the servers may get overloaded.
Let’s go to a web console of OpenResty Edge. This is our sample deployment of the console. Every user would have their own deployment.
We can still use our continuing sample application, test-edge.com.
Enter the application.
We already have a page rule defined.
This page rule sets up a reverse proxy to a pre-defined upstream. And there’s no request rate limiting defined yet.
Now let’s edit the existing page rule to add rate limiting.
Add a new action.
You can search for the action you want to add here
Search for “Limit request rate”.
First we need to specify the keys for rate limiting.
We can see that there are a lot of possible key types. Like the client IP address, the URI, the URI argument, cookie, and many more.
Choose the default key type, client IP address. Then the limit is for the scope of each unique client IP address.
The “Shape at” rate is a soft limit. When a client tries to send requests faster than this rate, the gateway server will delay those excessive requests to match this rate. So the faster the client sends requests, the longer delay the gateway will add to the requests.
Here we specify a rate of 50 requests per second.
Because we specified the key type to be client IP addresses, the limit will be applied to individual client IP addresses.
The “Reject at” rate is a hard limit. When the client sends the requests so fast that exceeds this hard limit, we can take more aggresive actions like blocking them immediately without waiting.
Specify a rate of 100 requests per second here.
We can choose from different interception actions.
Like closing the current connection right away, returning an error page, and returning a captcha page to block bots.
Select the default “Error Page” action, which will return an error page via HTTP.
We use the default HTTP status code, 503, to mean service unavailable.
Save our changes to to this page rule.
We need to make a new release to push out our new changes, as always.
Click on this button.
Our new release is now synchronized to all our gateway servers.
Now the new page rule has been pushed to all the gateway clusters and servers.
Our configuration changes do NOT require server reload, restart, or binary upgrade. So it’s very efficient and scalable.
Next, we’ll verify the effect of the new rate limits.
On the terminal, we can send a lot of requests very fast through the open-source utility named
wrk -c 50 -d 1s http://test-edge.com/
Here we use a concurrency level of 50 first. Note the
Run the command. The actual request rate is about 50 requests per second. This is our “Shape at” rate.
Now we increase the concurrency level to make
wrk send requests much faster.
wrk -c 128 -d 1s http://test-edge.com/
Note the 128 concurrency level.
Run it! Note that there are a lot of rejected requests with erroneous responses.
The actual request rate is high this time, just because the server rejects those excessive requests very fast.
In addition to limiting the rate of requests, OpenResty Edge can also limit the rate of SSL or TLS handshakes of HTTPS requests.
On this page, we can configure to limit the rate of SSL handshakes.
Let’s turn on the switch to see the configuration parameters.
These parameters are the same as the request rate limiting feature.
Yichun Zhang is the creator of the OpenResty® open source project. He is also the founder and CEO of the OpenResty Inc. company. He contributed a dozen open source Nginx 3rd-party modules, quite some Nginx and LuaJIT core patches, and designed products like OpenResty Edge, OpenResty XRay, and OpenResty Showman.
We provide the Chinese translation for this article on blog.openresty.com.cn. We welcome interested readers to contribute translations in other natural languages as long as the full article is translated without any omissions. We thank them in advance.
We always welcome talented and enthusiastic engineers to join our team at OpenResty Inc.
to explore various open source software’s internals and build powerful analyzers and
visualizers for real world applications built atop the open source software. If you are
interested, please send your resume to
email@example.com . Thank you!