To configure HTTP basic auth in OpenResty Edge, create a Basic Auth user group — either scoped to one application or global across all of them — in the Admin console, add users to it, then turn it on for a route with the “Enable Basic Authentication” page rule action. Authentication runs directly on the gateway servers, and releasing the configuration pushes it to every cluster with no server reload, restart, or binary upgrade.

This tutorial walks through both the per-application and global setups step by step, then tests them from a browser and with curl. It continues our OpenResty Edge product series.

HTTP basic auth in OpenResty Edge

The HTTP basic auth is handled directly by the gateway servers.

Diagram showing HTTP basic auth handled directly by the OpenResty Edge gateway servers

Configure the HTTP basic auth credentials for applications

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

The OpenResty Edge Admin web console dashboard

Let’s configure the HTTP basic auth credentials for our gateway application.

We still use our continuing sample application for the test-edge.com domain.

The sample gateway application for the test-edge.com domain in OpenResty Edge

Enter this application.

Entering the test-edge.com application in the OpenResty Edge console

Add a basic auth user group

Go to the HTTP Basic Auth Credentials page.

The HTTP Basic Auth Credentials page for the application

We support adding one or more basic auth user groups. Each user group can have one or more users.

Add a new Basic Auth user group.

Adding a new Basic Auth user group

Enter the group name “tutorial-group”. This user group is merely for this tutorial.

Entering the Basic Auth user group name tutorial-group

Save it.

Saving the new Basic Auth user group

Add a new Basic Auth user to this group.

Adding a new Basic Auth user to the tutorial-group group

Click this button to add a new user.

Clicking the button to add a new Basic Auth user

Type in the user name “Sam”.

Typing the Basic Auth user name Sam

Enter the password.

Entering the Basic Auth user password

Save it.

Saving the new Basic Auth user

Now we have a new user in the “tutorial-group” user group.

The new user Sam listed in the tutorial-group Basic Auth user group

By the way, we can go to the Basic tab to edit the user group settings. Here we don’t bother.

The Basic tab for editing Basic Auth user group settings

Close this page.

Closing the Basic Auth user group page

Enable basic auth with a page rule

Go to the Page Rules page to actually enable basic authentication.

The Page Rules page in the OpenResty Edge application

This existing page rule already sets up a reverse proxy to a pre-defined upstream. We covered this in an earlier tutorial.

An existing page rule setting up a reverse proxy to a predefined upstream

Let’s add a new page rule.

Adding a new page rule to enable basic auth

Add a new action.

Adding a new action to the page rule

Type “basic auth” to search.

Searching for the basic auth page rule action

Select “Enable Basic Authentication” option.

Selecting the Enable Basic Authentication page rule action

Select the Basic Auth user group we just created.

Selecting the Basic Auth user group for the page rule

Select “app-tutorial-group”.

Selecting the app-tutorial-group user group

The extra “app-” prefix indicates that the user group is only in the scope of the current application. And global basic auth user group names will get the extra prefix “global-”. I will get to the global HTTP basic auth settings later.

We set this rule as an “Always Top” rule to ensure this rule is always executed first. You can choose any order you want though.

Setting the basic auth page rule as an Always Top rule

Save this rule.

Saving the basic auth page rule

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

Making a new configuration release to push out the page rule

Click this button.

Clicking the button to start the configuration release

Ship it!

Shipping the configuration release

It is fully synchronized.

The configuration fully synchronized to the gateway clusters

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

The new page rule pushed to all gateway clusters and servers

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

Configuration changes applying without server reload, restart, or binary upgrade

Test basic auth in a browser and with curl

Let’s access the gateway application. We can see that the page triggers the web browser’s authentication dialog instead of showing the web page content.

The browser authentication dialog triggered by HTTP basic auth

First, try clicking the Cancel button. We can see the page turns into a “401 Unauthorized” error page.

Clicking Cancel on the browser authentication dialog

The 401 Unauthorized error page after cancelling authentication

Refresh the page.

Refreshing the page to retry basic auth

And this time, we enter the correct username and password we previously prepared.

Entering the correct username and password in the authentication dialog

Now we can access the web page.

The web page content shown after passing HTTP basic auth

Next, let’s test the HTTP basic auth setting on the command line with the curl utility.

On the terminal, we send a test request.

curl -sSI http://test-edge.com

Sending a test request with curl on the command line

We can see that the response is 401 Unauthorized.

The curl response showing 401 Unauthorized

Let’s specify the basic auth credential with the “u” option this time.

Specifying the basic auth credential with the curl -u option

Here it is the user name.

The user name in the curl basic auth credential

And here goes the password in clear text.

The password shown in clear text in the curl command

Run this command.

We can see that the response is 200 OK now. We passed the authentication, and the server returned the final response.

The curl response showing 200 OK after passing basic auth

Configure global HTTP basic auth across all applications

In addition to configuring basic auth inside Edge applications, we can also configure it in Edge’s global configuration. Global auth user groups are visible to all the Edge applications.

Basic auth is one of several gateway-level security actions in OpenResty Edge. For stronger protection you can layer it with the built-in CAPTCHA to block bots or the Web Application Firewall, all enabled through the same page-rule mechanism.

Add a global basic auth user group

Go to the Global Config page.

The Global Config page in the OpenResty Edge console

Enter the Global Basic Auth Credentials page.

The Global Basic Auth Credentials page

Click this button to add a new Basic Auth user group.

Adding a new global Basic Auth user group

Type the group name.

Typing the global Basic Auth user group name

Save it.

Saving the new global Basic Auth user group

Now that we’ve created a new global Basic Auth user group.

The newly created global Basic Auth user group

We can also click the edit button to add a new user for this group.

Clicking the edit button to add a user to the global group

Click this button to add a new user.

Clicking the button to add a new user to the global group

Type in the user name “Kelly”.

Typing the global Basic Auth user name Kelly

Enter the password.

Entering the global Basic Auth user password

And save it.

Saving the new global Basic Auth user

Configure the global auth for application

Now we have a new user in the “tutorial-global-group” user group. Let’s see how to configure the global credential for our gateway application.

The new user Kelly in the tutorial-global-group user group

Close this page.

Closing the global Basic Auth credentials page

Go to the application list page.

The application list page in the OpenResty Edge console

Search the test-edge.com domain.

Searching for the test-edge.com domain

Let’s enter this application.

Entering the test-edge.com application

Go to the Page Rules page.

Opening the Page Rules page for the application

Edit this rule.

Editing the basic auth page rule

Click the drop-down list to switch the user group.

Clicking the drop-down list to switch the basic auth user group

We can choose a global user group here. After the change is saved and released, the user credentials in that global user group are used instead.

Choosing a global user group for the basic auth page rule

Frequently asked questions

How do I enable HTTP basic auth in OpenResty Edge?

Create a Basic Auth user group and add users to it, then add a page rule with the “Enable Basic Authentication” action and select that user group. Make a configuration release to push the rule to your gateway servers. From then on the protected routes return a 401 until a valid username and password are supplied.

Does changing basic auth require a gateway reload or restart?

No. Releasing the configuration pushes the new page rule to all gateway clusters and servers without any reload, restart, or binary upgrade, so the change takes effect efficiently even at large scale.

How do I test HTTP basic auth with curl?

Send a plain request such as curl -sSI http://test-edge.com and you get a 401 Unauthorized response. Add credentials with the -u option — curl -sSI -u Sam:PASSWORD http://test-edge.com — and a correct username and password return 200 OK.

Why do I get a “401 Unauthorized” after enabling basic auth?

That is the expected behavior when no credentials, or wrong ones, are sent. A browser shows its authentication dialog; cancelling it renders the 401 page. Entering the username and password you configured in the user group lets the request through.

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.