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.

Embeded image

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.

Screenshot

We already have an upstream defined in our earlier tutorial.

Screenshot

This my_backend upstream has a backend server defined already.

Screenshot

Note the IP address of the backend server ends with number 191. We will use this IP address later.

Screenshot

And we also have a page rule already defined.

Screenshot

This page rule sets up a reverse proxy to the upstream we just saw.

Screenshot

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.

Embeded image

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'

screenshot 1

Then we log into the backend server of the IP ending with 191:

ssh 54.213.103.191

screenshot 2

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.

screenshot 3

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.

Embeded image

Now we try to modify the Host header of the upstream requests.

Screenshot

Let’s edit the existing proxy page rule.

Screenshot

Add a new action, “set proxy host”.

Screenshot

Let’s search for “Set proxy host”.

Screenshot

Screenshot

Select it.

Screenshot

Fill in the host name, blah.com.

Screenshot

Save our changes.

Screenshot

As always, we need to make a new release to push out our edited page rule.

Screenshot

Click on this button.

Screenshot

Ship it!

Screenshot

It is fully synchronized.

Screenshot

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.

Embeded image

This time, the Host request header received on the backend server should be blah.com. Let’s check it out!

Embeded image

Test of the new page rule

On the terminal, we send a request to our gateway servers again.

curl -sSI 'http://test-edge.com'

screenshot 1

Then log into the backend server of the IP ending with 191 again.

ssh 54.213.103.191

screenshot 2

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.

screenshot 3

This is what I’d like to cover today.

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.