To enable hCaptcha in OpenResty Edge, enter your hCaptcha Sitekey and Secret key under Global Config → Captcha Configuration, then either add an “Enable hCaptcha” action in a page rule or set your WAF block action to the hCaptcha challenge, and make a new release. hCaptcha is a third-party service that helps defend against bots, spam, and abuse.

This tutorial walks through both paths with screenshots, and shows how the clearance time controls how often visitors get challenged.

hCaptcha challenge page served by OpenResty Edge

Configure hCaptcha Keys in OpenResty Edge’s Global Config

As always, let’s go to the OpenResty Edge’s Admin web console. This is our sample deployment of the console. Every user has her local deployment.

OpenResty Edge Admin web console

Before we enable hCaptcha for our application, we need to config Sitekey and Secret key of hCaptcha first.

The Applications page in the OpenResty Edge Admin console

Let’s go to the Global Config page.

Going to the Global Config page from the top menu

Go to the Captcha Configuration page.

Opening the Captcha Configuration page in Global Config

The Sitekey and Secret key of hCaptcha need to be obtained from the official website of hCaptcha.

hCaptcha Sitekey and Secret key fields in OpenResty Edge’s captcha configuration

Go to the hCaptcha website and log in or sign up.

Logging in or signing up on the hCaptcha website

Then, go to the sites page to see our Sitekey list. Go to the Sitekey Settings page.

Sitekey list on the hCaptcha sites page

And we can get the sitekey details here.

Sitekey details in the hCaptcha Sitekey Settings page

Then we go to the settings page to get the secret key.

Opening the hCaptcha settings page to get the secret key

We can get the secret key here.

Secret key shown on the hCaptcha settings page

We enter the Sitekey and Secret key we obtained from the hcaptcha website.

Entering the hCaptcha Sitekey and Secret key in OpenResty Edge

Click to save.

Saving the hCaptcha captcha configuration in OpenResty Edge

Now that we’ve configured the Sitekey and Secret key for hCaptcha.

hCaptcha Sitekey and Secret key configured in OpenResty Edge

Enable hCaptcha with a Page Rule Action

Let’s configure hCaptcha for our application.

Going back to the Applications page from the Global Config

We continue with the test-edge.com domain to demonstrate how to config hCaptcha.

Searching for the test-edge.com domain in the application list

Let’s enter this application.

The test-edge.com application found in the search results

We already have an upstream defined in our earlier tutorial.

Going to the Upstreams page of the application

This my_backend upstream has a backend server defined already.

The my_backend upstream with a backend server defined

And we also have a page rule already defined.

Going to the Page Rules page of the application

In the previous tutorial, this page rule has set up a reverse proxy to the upstream we just saw.

Reverse proxy page rule pointing to the my_backend upstream

Let’s add a new page rule to enable the hCaptcha action.

Adding a new page rule to enable hCaptcha

We do not specify a condition for this rule. So it will fire unconditionally.

Page rule without conditions so it fires unconditionally

Let’s add a new action.

Adding a new action to the page rule

Search for the action “Enable hCaptcha”.

Searching for the Enable hCaptcha action in OpenResty Edge

Select the option “Enable hCaptcha”.

Selecting the Enable hCaptcha option

Clearance time means that when the client passes the captcha verification, the OpenResty Edge will allow all requests from the same client in the specified period. hCaptcha records the verification information through cookies.

Clearance time setting of the Enable hCaptcha action

We set the time to 10 seconds.

Setting the hCaptcha clearance time to 10 seconds

We insert this rule as an always top rule to ensure this rule is executed first. This is for our demonstration. You can set any order you want.

The insertion position options for the new page rule

Save this rule.

Creating the page rule with the Always-Top position selected

As always, we need to make a new release to push out our edited page rule.

The pending change notice with the to-be-released link

Click on this button.

Releasing the pending change on the Releases page

Ship it!

Shipping the new release

It is fully synchronized.

Release fully synchronized to all gateway clusters and servers

Now the new page rule has been pushed to all the gateway clusters and servers.

Configuration sync starting across OpenResty Edge gateway clusters

Configuration sync in progress across OpenResty Edge gateway clusters

Configuration sync completed across OpenResty Edge gateway clusters

Our configuration changes do NOT require server reload, restart, or binary upgrade. So it’s very efficient and scalable.

Diagram: the Edge Admin server syncs config to each node’s local key-value storage, and request processing sessions pick up the update without reload

Test the hCaptcha Challenge

Now let’s access test-edge.com. We can see that the page returns the hCaptcha page instead of our original content.

hCaptcha challenge page returned when accessing test-edge.com

Click to verify. We need to follow the instructions to select the correct image.

hCaptcha image selection challenge

If the selection is incorrect, another group of images will continue to come out to select.

Another group of hCaptcha images shown after an incorrect selection

After verification, we can see the expected content of the URI. Remember we just set the clearance time to 10 seconds. We can access the application without captchas within 10 seconds.

The original page content (the OpenResty welcome page) shown after passing the hCaptcha verification

After 10 seconds, we need to verify again.

hCaptcha challenge shown again after the clearance time expires

Set hCaptcha as the WAF Block Action

In addition to setting the hCaptcha in action, we can also set it in the WAF rules. We explain the specific features of the WAF rules in the enabling WAF in OpenResty Edge tutorial. The WAF engine’s performance is far higher than traditional WAFs like ModSecurity — see the OpenResty Edge WAF performance benchmark against ModSecurity.

Going to the Page Rules of the application from the Releases page

Let’s take a brief look at how to set up hCaptcha in WAF rules.

Opening the hCaptcha page rule to edit its WAF section

Let’s enable WAF.

Turning on the WAF section in the page rule

We can set the block action to hCaptcha.

The Block Action setting in the WAF section of the page rule

We selected the “OpenResty Edge gateway server captcha challenge - hCaptcha” option.

Selecting the OpenResty Edge gateway server captcha challenge - hCaptcha option

hCaptcha selected as the WAF block action

As with the action we set before, we need to set a clearance time here. I won’t demonstrate it in detail here. You can go through it in a WAF rules-related video.

Clearance time setting for the hCaptcha WAF block action

By the way, we also support the OpenResty Edge built-in captcha service, which is served directly from the gateway server. You can learn about it in the built-in captcha tutorial.

What is OpenResty Edge

OpenResty Edge is our all-in-one gateway software for microservices and distributed traffic architectures. It combines traffic management, private CDN construction, API gateway, security, and more to help you easily build, manage, and protect modern applications. OpenResty Edge delivers industry-leading performance and scalability to meet the demanding needs of high concurrency, high load scenarios. It supports scheduling containerized application traffic such as K8s and manages massive domains, making it easy to meet the needs of large websites and complex applications.

If you like this tutorial, please subscribe to this blog site and/or our YouTube channel. Thank you!

About The Author

Yichun Zhang (Github handle: agentzh), is the original creator of the OpenResty® open-source project and the CEO of OpenResty Inc..

Yichun is one of the earliest advocates and leaders of “open-source technology”. He worked at many internationally renowned tech companies, such as Cloudflare, Yahoo!. He is a pioneer of “edge computing”, “dynamic tracing” and “machine coding”, with over 22 years of programming and 16 years of open source experience. Yichun is well-known in the open-source space as the project leader of OpenResty®, adopted by more than 40 million global website domains.

OpenResty Inc., the enterprise software start-up founded by Yichun in 2017, has customers from some of the biggest companies in the world. Its flagship product, OpenResty XRay, is a non-invasive profiling and troubleshooting tool that significantly enhances and utilizes dynamic tracing technology. And its OpenResty Edge product is a powerful distributed traffic management and private CDN software product.

As an avid open-source contributor, Yichun has contributed more than a million lines of code to numerous open-source projects, including Linux kernel, Nginx, LuaJIT, GDB, SystemTap, LLVM, Perl, etc. He has also authored more than 60 open-source software libraries.