With OpenResty Edge you can store static files directly on the gateway’s Global Static File storage and use a page rule to return them straight from the edge, without back-to-source to an origin — which is higher performance and more reliable. You can match a single file by its exact URI, serve a whole directory by URI prefix, and optionally strip a prefix from the request with Ignore URI Prefix. This tutorial walks through uploading the files and wiring up each matching mode.

Serving static files directly from OpenResty Edge without back-to-source

Upload Static Files to OpenResty Edge

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.

OpenResty Edge Admin web console sample deployment

We start by uploading the static files to OpenResty Edge.

Starting to upload static files to OpenResty Edge

Go to the Global Static File page.

The OpenResty Edge Global Static File page

Create a directory.

Creating a directory on the Global Static File page

We name it “test”.

Naming the new static file directory test

Create.

Confirming creation of the test directory

Here is the directory we create.

The newly created test directory in the list

Enter the directory.

Entering the test directory

Then we upload the file.

Uploading a file into the test directory

Select the file “bird.jpg”.

Selecting the file bird.jpg to upload

bird.jpg selected for upload

Save the file.

Saving the uploaded bird.jpg file

Create a subdirectory.

Creating a subdirectory

We name it “images”.

Naming the subdirectory images

Create.

Confirming creation of the images subdirectory

Enter the directory.

Entering the images subdirectory

Then we upload another file.

Uploading another file into the images subdirectory

Select the file “cat.jpg”.

Selecting the file cat.jpg to upload

cat.jpg selected for upload

Save the file.

Saving the uploaded cat.jpg file

Serve a Static File with a Page Rule

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

Preparing to use the uploaded static files

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

The sample application test-edge.com

Enter the application.

Entering the test-edge.com application

Go to the Page Rules page.

Opening the Page Rules page

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

An existing page rule from an earlier tutorial

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

Creating a new page rule to return a file directly

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

Adding a rule condition to check the URI /images/bird.jpg

We choose the string equality operator.

Choosing the string-equality operator

Selecting the string-equality operator for the URI condition

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

Entering /images/bird.jpg to match the static file

And turn on the Content.

Turning on the Content option in the page rule

Select a file that we just uploaded.

Selecting an uploaded file to return

Browsing the uploaded static files

Expand the directory.

Expanding the test directory

Select image “bird.jpg”.

Selecting the bird.jpg image to return

We insert this page rule before the proxy page rule.

Inserting the page rule before the proxy page rule

Create the rule.

Creating the page rule

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

Starting a new release to push the change

Click on this button.

Clicking the release button

Ship it!

Shipping the new release

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

The new release synchronized to all gateway servers

Test the Static File Page Rules

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

Sending an HTTP request for images/bird.jpg

A bird image was returned, which is what we expected precisely.

Serve a Whole Directory by Prefix

After demonstrating the return file, next, we illustrate the directory.

Go to the Page Rules page.

Returning to the Page Rules page

Edit the Page Rule.

Editing the page rule

Choose the operator “Prefix matches”.

Choosing the Prefix matches operator

Selecting the Prefix matches operator

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

Entering /images to match the static resource location

The /images prefix condition

Select the directory that we just created.

Selecting the uploaded directory to serve

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

The Ignore URI Prefix option

Save the rule.

Saving the page rule

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

Starting a new release to push the change

Click on this button.

Clicking the release button

Ship it!

Shipping the new release

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

The new release synchronized to all gateway servers

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

Sending a request for images/cat.jpg

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

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

Sending a request for images/bird.jpg

The request returned the error page. There is no “bird.jpg” file in the “images” subdirectory.

Ignore the URI Prefix

Next, we demonstrate how to ignore the URI prefix.

Go to the Page Rules page.

Returning to the Page Rules page

Edit the Page Rule.

Editing the page rule again

We still use the “test” directory.

Keeping the test directory selected

Then choose to ignore “String”.

Choosing to ignore a String prefix

Type “/images/”.

Entering /images/ as the prefix to ignore

Save the rule.

Saving the page rule

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

Starting a new release to push the change

Click on this button.

Clicking the release button

Ship it!

Shipping the new release

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

The new release synchronized to all gateway servers

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

Sending a request with the /images prefix

The request returned a bird image.

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

Requesting the image in the images subdirectory

The request returned a cat image.

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

Sending a request with a non-existent /no prefix

The error page was returned.

The error page returned for the non-existent resource

This stores and serves static files directly at the gateway. If instead you want to cache dynamic responses fetched from an origin, see caching rules in OpenResty Edge; to serve assets globally as your own private CDN, see building a private CDN with OpenResty Edge.

FAQ

Can OpenResty Edge serve static files without going back to the origin?

Yes. Once a file is uploaded to the Global Static File storage, a page rule returns it directly from the gateway, so the request does not need to back-to-source. This is higher performance and more reliable.

How do I serve a single file versus a whole directory?

Use a page rule with the string-equality operator to match one exact URI (for example /images/bird.jpg), or use the “Prefix matches” operator to serve an entire uploaded directory under a URI prefix (for example /images).

What does “Ignore URI Prefix” do?

It allows the requested URL to differ from the actual resource URI by stripping a prefix (for example /images/) from the request before the file is looked up.

How do I push a new static file or page rule live?

Make a new release. The release is then synchronized to all your gateway servers.

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.