In this tutorial, we will show how to add custom response headers in OpenResty Edge without restarting or reloading servers.
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.
Enter the application.
First go to “Page Rules” page.
Click here to add a new rule.
Let’s add an action.
We will choose “Add response header”.
The header have two parts, the name and the value.
Name the header as “X-TEST-EXTRA-HEADER”.
For the value, you can use string or built-in variable, we try the string first.
Type in “hello world”.
Make sure this rule runs before any existing normal ones.
To push out the new rule, we need to create a new release.
It is fully synchronized.
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.
Let’s test it out on a gateway server.
We copy the IP address of this San Francisco gateway server.
Then test it on the terminal.
The “X-TEST-EXTRA-HEADER” is here.
Next let’s try built-in variable for value of response header.
Edit this rule.
Let’s choose built-in variable for header value.
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.
Another one is “System Hostname”, which is just the gateway server’s name we set.
Let’s try “System Hostname” here.
Again, we need to create a new release.
It is fully synchronized now.
First we can check the hostname for the previous server.
The hostname for this gateway server is like this.
Then switch to terminal.
The value of “X-TEST-EXTRA-HEADER” has changed to the hostname.
Try another gateway server, like this one in Frankfurt, the hostname is like this.
Switch to terminal again.
The value is now the Frankfurt server’s hostname.
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.