In this tutorial, we will show you how to use OpenResty Edge Add a custom response header in the without restarting or reloading the server process.

Add a new response header with a string as a value

Let's go OpenResty Edge Admin Web Console for. This is the sample deployment of our console. Each user has its own local deployment. We can continue to use the previous example application, "test edge. com".

Screenshot

Enter the application.

Screenshot

First, enter the "Page Rules" page.

Screenshot

Click here to add a new page rule.

Screenshot

Add an action.

Screenshot

We select Add response header.

Screenshot

The response header has two parts, its name and corresponding value.

Screenshot

Name the response header "X-TEST-EXTRA-HEADER".

Screenshot

For the values here, you can choose to use strings or built-in variables. Let's test with strings first.

Screenshot

Enter "hello world".

Screenshot

Ensure that this rule runs before any existing normal rules.

Screenshot

Save this page rule.

Screenshot

As usual, we need to release a new version to push our changes.

Screenshot

release!

Screenshot

The new version has now been synchronized to all gateway servers.

Screenshot

Now, the new page rules have been pushed to all gateway clusters and servers.

 Embeded video

These configuration changes do not require a server reload, restart, or binary upgrade. So it is very efficient and scalable.

 Embeded image

Let's test it on the gateway server.

Screenshot

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

Screenshot

Then test on the terminal.

Screenshot

Here is the "X-TEST-EXTRA-HEADER" response header we added.

Screenshot

Built in variable with "System Hostname" as the value

Next, let's try using the built-in variable as the value of the response header.

Screenshot

Edit this page rule.

Screenshot

Select the built-in variable as the value of the response header.

Screenshot

Currently, we provide two options for built-in variables. One is "Request Header", which means you can retrieve request information. You need to specify "Request Header" by name, such as "Origin", "Referer", etc.

Screenshot

The other is "System Hostname", which is the name of the gateway server we set up earlier.

Screenshot

Let's try "System Hostname".

Screenshot

Save it.

Screenshot

As just now, we need to publish to push this new page rule.

Screenshot

release!

Screenshot

Now it has been pushed to all gateway clusters and servers.

Screenshot

First, we can check the host name of the previous server.

Screenshot

The host name of this gateway server is as follows.

Screenshot

Then switch to the terminal.

Screenshot

The value of the "X-TEST-EXTRA-HEADER" response header has been changed to the hostname we just saw.

Screenshot

Let's test another gateway server, such as this one in Frankfurt, whose host name is as follows.

Screenshot

Switch back to the terminal again.

Screenshot

Now the value of the response header is the host name of the Frankfurt server.

Screenshot

About OpenResty Edge

OpenResty Edge It is a fully functional gateway software that we independently developed and is most suitable for microservices and distributed traffic. It provides various functions such as page rules, Web Application Firewall (WAF), load balancing, etc.

If you like this tutorial, please subscribe to this blog site and our YouTube Channel or Station B channel thank you!

About the author

Zhang Yichun is open source OpenResty ® Project founder and OpenResty Inc. CEO and founder of the company.

Zhang Yichun (Github ID: agentzh) was born in Jiangsu, China, and now lives in the U.S. Bay Area. He was an advocate and leader of China's early open source technology and culture, and once worked for many internationally renowned high-tech enterprises, such as Cloudflare Yahoo, Alibaba, the pioneer of "edge computing", "dynamic tracking" and "machine programming", has more than 22 years of programming and 16 years of open source experience. As the leader of open source projects with more than 40 million global domain name users, he OpenResty ® High tech enterprises created by open source projects OpenResty Inc. It is located in the center of Silicon Valley in the United States. Its two main products OpenResty XRay (Utilize Dynamic tracking Technology) and OpenResty Edge (The all-purpose gateway software most suitable for microservices and distributed traffic), widely favored by many listed and large enterprises worldwide. Besides OpenResty, Zhang Yichun has contributed more than one million lines of code to many open source projects, including Linux kernel, Nginx LuaJIT GDB SystemTap LLVM , Perl, etc., and has written more than 60 open source software libraries.

Follow us

If you like this article, please follow us OpenResty Inc. Corporate Blog Site You are also welcome to scan our WeChat official account:

Our

translate

We provide English version Original text and Chinese translation (this article). We also welcome readers to provide translations in other languages. As long as the full text translation is not omitted, we will consider using it. Thank you very much!