Skip to main content

Command Palette

Search for a command to run...

Set up a Simplest Reverse Proxy and Load Balancer with OpenResty Edge

Updated
6 min read

Today I’d demonstrate how to setup the simplest reverse proxy and load balancer in OpenResty Edge.

edge1.png

We manage all the gateway server nodes and their configurations in a central place, the Edge Admin web console.

edge2.png

Let’s go to a web console of OpenResty Edge. This is our sample deployment of the console. Every user would have their own deployment.

Let’s login using our user name and password.

edge3.png

Other authentication methods are also configurable.

Sign in now.

edge4.png

We’re now at the application list page. There are many existing applications we created previously. Each application is like a virtual host or virtual server in the same gateway.

Here we’d create a new application.

edge5.png

We will just create one of the HTTP type. It is the default.

edge6.png

We assign a single domain, test-edge.com, to this application.

edge7.png

We may add more domains, including wildcard domains.

edge8.png

We only care about the 80 port in this example.

edge9.png

Let’s create this application!

edge10.png

Now we are inside this new application. It’s empty.

Let’s go to the Upstreams page.

edge11.png

Obviously, we currently have no upstreams defined.

Create a new upstream for our backend servers.

edge12.png

We give this upstream a name, say, my_backend.

edge13.png

For simplicity, we just use the HTTP protocol here.

edge14.png

We may always want to use HTTPS for the real thing.

edge15.png

Here we need the backend server’s IP address.

edge16.png

We’ve already prepared a sample backend server at this IP address.

edge17.png

It simply returns the default index page of the open source OpenResty server software.

It could be anything that speaks HTTP.

edge18.png

We can now fill out the host field for the backend server.

edge19.png

We keep the 80 port intact.

edge20.png

We may add more servers to this upstream in the future.

edge21.png

Now save this upstream.

edge22.png

We can see this my_backend upstream is already there.

edge23.png

Now let’s create a new page rule to actually make use of this upstream.

edge24.png

We currently don’t have any page rules defined.

Create a new page rule.

edge25.png

For this page rule, we do not specify a condition. This way it will apply to all incoming requests.

edge26.png

We could, however, limit this proxy page rule to certain requests only.

edge27.png

We disable the condition again.

edge28.png

Let’s add a proxy target here.

edge29.png

Let’s choose an upstream.

edge30.png

Here we have our newly created upstream present.

edge31.png

We also have some pre-defined global upstreams. They can be reused by all the applications including this one.

edge32.png

We select our my_backend upstream.

edge33.png

Our upstream has only one server.

So the balancing policy does not really matter here.

edge34.png

We’d just keep the default round robin policy.

We may also want to enable caching of the responses. We’ll cover this topic in another video.

edge35.png

Finally, create this rule for real.

edge36.png

We can see the proxy page rule is already listed on the page rule list.

edge37.png

The last step is to make a new configuration release. It will push out our pending changes to all our gateway servers.

edge38.png

Let’s click on this link to make a new release.

edge39.png

We have a chance to review our changes before pushing them out.

This is our first change.

edge40.png

It is our addition of the my_backend upstream.

edge41.png

This is our second change.

edge42.png

edge43.png

This is indeed our proxy page rule.

Now we make a release to all our gateway servers.

edge44.png

Ship it!

edge45.png

We can watch the configuration synchronization progress at real time. It is pushed out to the whole gateway network.

edge46.png

Now it is fully synchronized. As we can see, this sample deployment has 13 servers in the gateway network.

We do incremental config synchronization across the whole network.

We live-update config on the request level. None of the application-level configuration changes require server reload, restart, or binary upgrade. So it is very scalable even when you have many different users making frequent releases.

edge47.png

We can also check all the gateway servers grouped by clusters.

edge48.png

This is just our sample deployment around the world.

edge49.png

Our users are free to deploy their gateway servers anywhere they like. Or spanning different clouds and hosting services.

edge50.png

This column shows the configuration synchronization status for each gateway server.

edge51.png

We can test a gateway server near San Francisco here.

edge52.png

Its public IP address is this.

edge53.png

We copy this IP address to test this server directly.

On the terminal, we can use curl to test this San Francisco gateway server.

 curl -sS -H 'Host: test-edge.com' 'http://138.68.231.133/' | less

edge54.png

Note that we specify the Host request header. This is because the same server is serving many different virtual hosts.

Send the request.

edge55.png

It works as expected! We got the default OpenResty index page just like accessing the backend server directly.

We can check the response header too via the -I option of curl.

curl -I -H 'Host: test-edge.com' 'http://138.68.231.133/'

edge56.png

There are some headers created by the OpenResty Edge gateway software.

Alternatively, we could bind the IP address to the host name in this local /etc/hosts file. Then we’ll be able to point a web browser to this domain directly.

cat /etc/hosts

edge57.png

For the real setup, we should add the gateway server IP addresses to our DNS name servers.

dig test-edge.com

Here we haven’t configured this domain’s DNS records yet. We’ll demonstrate it in another video.

edge58.png

OpenResty Edge can also work as an authoritative DNS server network at the same time.

edge59.png

This is optional though. The user could still choose to use 3rd-party DNS name servers. This is what I’d like to cover today.

edge60.png

If you like this tutorial, please subscribe to this blog site 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 (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.

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.

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.

More from this blog

OpenResty

27 posts