Adding Custom Response Headers in OpenResty Edge
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.
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.
Save it.
To push out the new rule, we need to create a new release.
Ship it!
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.
Use “System Hostname” As a Built-in Variable for Value
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.
Save it.
Again, we need to create a new release.
Ship it!
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.
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.