Today I’d show how to serve static resources on OpenResty Edge. Using OpenResty Edge to serve static resources does not need to back-to-source, which is higher performance and more reliable, but it is not required.

Embeded image

Upload static resources

Let’s go to the OpenResty Edge’s Admin web console. This is our sample deployment of the console. Every user has their own local deployment.

Screenshot

We start by uploading the static resources to OpenResty Edge.

Screenshot

Go to the Global Static File page.

Screenshot

Create a directory.

Screenshot

We name it “test”.

Screenshot

Create.

Screenshot

Here is the directory we create.

Screenshot

Enter the directory.

Screenshot

Then we upload the file.

Screenshot

Select the file “bird.jpg”.

Screenshot

Screenshot

Save the file.

Screenshot

Create a subdirectory.

Screenshot

We name it “images”.

Screenshot

Create.

Screenshot

Enter the directory.

Screenshot

Then we upload another file.

Screenshot

Select the file “cat.jpg”.

Screenshot

Screenshot

Save the file.

Screenshot

Create a page rule to use the uploaded static resources

The files have been uploaded, so let’s use them next.

Screenshot

We can still use our continuing sample application, test-edge.com.

Screenshot

Enter the application.

Screenshot

Go to the Page Rules page.

Screenshot

Here’s a page rule created in an earlier video tutorial.

Screenshot

Let’s create a new page rule to return a file directly.

Screenshot

We enable a rule condition to check for the URI /images/bird.jpg.

Screenshot

We choose the string equality operator.

Screenshot

Screenshot

Enter the value /images/bird.jpg to match the static file.

Screenshot

And turn on the Content.

Screenshot

Select a file that we just uploaded.

Screenshot

Screenshot

Expand the directory.

Screenshot

Select image “bird.jpg”.

Screenshot

We insert this page rule before the proxy page rule.

Screenshot

Create the rule.

Screenshot

We need to make a new release to push out our new changes, as always.

Screenshot

Click on this button.

Screenshot

Ship it!

Screenshot

Our new release is now synchronized to all our gateway servers.

Screenshot

Test

Then we send an HTTP request with URI “images/bird.jpg”.

Screenshot

A bird image was returned, which is what we expected precisely. After demonstrating the return file, next, we illustrate the directory.

Go to the Page Rules page.

Screenshot

Edit the Page Rule.

Screenshot

Choose the operator “Prefix matches”.

Screenshot

Screenshot

Enter the value /images to match the static resource location only.

Screenshot

Screenshot

Select the directory that we just created.

Screenshot

“Ignore URI Prefix” allows the requested URL to be different from the actual resource URI, which we will demonstrate later.

Screenshot

Save the rule.

Screenshot

We need to make a new release to push out our new changes, as always.

Screenshot

Click on this button.

Screenshot

Ship it!

Screenshot

Our new release is now synchronized to all our gateway servers.

Screenshot

Then we send a request with URI “images/cat.jpg”.

Screenshot

The request returned a cat image in the subdirectory “images”.

What will happen if we request “images/bird.jpg”?

Screenshot

The request returned the error page. There is no “bird.jpg” file in the “images” subdirectory. Next, we demonstrate how to ignore the URI prefix.

Go to the Page Rules page.

Screenshot

Edit the Page Rule.

Screenshot

We still use the “test” directory.

Screenshot

Then choose to ignore “String”.

Screenshot

Type “/images/”.

Screenshot

Save the rule.

Screenshot

We need to make a new release to push out our new changes, as always.

Screenshot

Click on this button.

Screenshot

Ship it!

Screenshot

Our new release is now synchronized to all our gateway servers.

Screenshot

we send a request with the “/images” prefix.

Screenshot

The request returned a bird image.

Then we request the image in the subdirectory “images”.

Screenshot

The request returned a cat image.

Then we send a request with the “/no” prefix of a non-existent resource.

Screenshot

The error page was returned.

Screenshot

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.