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

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.

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

screenshot 1

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

1
ssh 54.213.103.191

screenshot 2

Check the last entry in the server software’s access log file.

1
tail -1 /usr/local/openresty/nginx/logs/access.log

The Host header of the forwarded request is indeed test-edge.com.

screenshot 3

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

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

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

screenshot 1

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

1
ssh 54.213.103.191

screenshot 2

Check the last entry in the access log file.

1
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 Vimeo account and/or our YouTube channel. Thank you!

About This Article and Associated Video

This article and its associated video are both generated automatically by our OpenResty Showman product from a simple screenplay file.

About The Author

Yichun Zhang is the creator of the OpenResty® open source project. He is also the founder and CEO of the OpenResty Inc. company. He contributed a dozen open source Nginx 3rd-party modules, quite some Nginx and LuaJIT core patches, and designed products like OpenResty Edge, OpenResty XRay, and OpenResty Showman.

Translations

We provide the Chinese translation for this article on blog.openresty.com.cn. We welcome interested readers to contribute translations in other natural languages as long as the full article is translated without any omissions. We thank them in advance.

We are hiring

We always welcome talented and enthusiastic engineers to join our team at OpenResty Inc. to explore various open source software’s internals and build powerful analyzers and visualizers for real world applications built atop the open source software. If you are interested, please send your resume to talents@openresty.com . Thank you!