Set Different Host Headers for Upstream Requests in OpenResty Edge
Today I’d show how to configure a different Host
header for the requests sent to the upstream or backend servers. In this diagram, the client sends a request with the host test-edge.com
. But the request sent to the origin site is another host, blah.com
.
Before setting the Host header of the upstream request
As always, let’s go to the OpenResty Edge’s Admin web console. This is our sample deployment of the console. Every user has her own local deployment.
We still use our continuing sample application for the test-edge.com domain.
Let’s enter this application.
We already have an upstream defined in our earlier tutorial.
This my_backend
upstream has a backend server defined already.
Note the IP address of the backend server ends with number 191
. We will use this IP address later.
And we also have a page rule already defined.
This page rule sets up a reverse proxy to the upstream we just saw.
We’ll send a test request to the gateway servers, and then check the Host
request header received on the backend server. By default, it must be identical.
On the terminal, we use curl
to send a request to our gateway servers via test-edge.com
. We assume this domain is already resolved to our gateway servers.
curl -sSI 'http://test-edge.com'
Then we log into the backend server of the IP ending with 191
:
ssh 54.213.103.191
Check the last entry in the server software’s access log file.
tail -1 /usr/local/openresty/nginx/logs/access.log
The Host
header of the forwarded request is indeed test-edge.com
.
Set the Host header of the upstream request
Now we’d configure OpenResty Edge to make it send a different Host header to the backend, say, blah.com
.
Now we try to modify the Host header of the upstream requests.
Let’s edit the existing proxy page rule.
Add a new action, “set proxy host”.
Let’s search for “Set proxy host”.
Select it.
Fill in the host name, blah.com
.
Save our changes.
As always, we need to make a new release to push out our edited page rule.
Click on this button.
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.
This time, the Host
request header received on the backend server should be blah.com
. Let’s check it out!
Test of the new page rule
On the terminal, we send a request to our gateway servers again.
curl -sSI 'http://test-edge.com'
Then log into the backend server of the IP ending with 191
again.
ssh 54.213.103.191
Check the last entry in the access log file.
tail -1 /usr/local/openresty/nginx/logs/access.log
We see that the Host
header of the forwarded request becomes blah.com
, as expected.
This is what I’d like to cover today.
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.