In this tutorial, we will show how to add custom response headers in OpenResty Edge without restarting or reloading servers.

Add New Respond Header and Use String As Value

First, go to the OpenResty Edge web console. This is our demo deployment of the console. Each user would have their own deployment. We will use our sample application for the “test-edge.com” domain.

Screenshot

Enter the application.

Screenshot

First go to “Page Rules” page.

Screenshot

Click here to add a new rule.

Screenshot

Let’s add an action.

Screenshot

We will choose “Add response header”.

Screenshot

The header have two parts, the name and the value.

Screenshot

Name the header as “X-TEST-EXTRA-HEADER”.

Screenshot

For the value, you can use string or built-in variable, we try the string first.

Screenshot

Type in “hello world”.

Screenshot

Make sure this rule runs before any existing normal ones.

Screenshot

Save it.

Screenshot

To push out the new rule, we need to create a new release.

Screenshot

Ship it!

Screenshot

It is fully synchronized.

Screenshot

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

Embeded video

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

Embeded image

Let’s test it out on a gateway server.

Screenshot

We copy the IP address of this San Francisco gateway server.

Screenshot

Then test it on the terminal.

Screenshot

The “X-TEST-EXTRA-HEADER” is here.

Screenshot

Use “System Hostname” As a Built-in Variable for Value

Next let’s try built-in variable for value of response header.

Screenshot

Edit this rule.

Screenshot

Let’s choose built-in variable for header value.

Screenshot

Here we provide two options for built-in variable currently. One is “Request Header”, which means you can retrieve the request information. You need to specify the “Request Header” by name like “Origin”, “Referer” etc.

Screenshot

Another one is “System Hostname”, which is just the gateway server’s name we set.

Screenshot

Let’s try “System Hostname” here.

Screenshot

Save it.

Screenshot

Again, we need to create a new release.

Screenshot

Ship it!

Screenshot

It is fully synchronized now.

Screenshot

First we can check the hostname for the previous server.

Screenshot

The hostname for this gateway server is like this.

Screenshot

Then switch to terminal.

Screenshot

The value of “X-TEST-EXTRA-HEADER” has changed to the hostname.

Screenshot

Try another gateway server, like this one in Frankfurt, the hostname is like this.

Screenshot

Switch to terminal again.

Screenshot

The value is now the Frankfurt server’s hostname.

Screenshot

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.