Pushing static resources to OpenResty Edge gateways
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.
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.
We start by uploading the static resources to OpenResty Edge.
Go to the Global Static File page.
Create a directory.
We name it “test”.
Create.
Here is the directory we create.
Enter the directory.
Then we upload the file.
Select the file “bird.jpg”.
Save the file.
Create a subdirectory.
We name it “images”.
Create.
Enter the directory.
Then we upload another file.
Select the file “cat.jpg”.
Save the file.
Create a page rule to use the uploaded static resources
The files have been uploaded, so let’s use them next.
We can still use our continuing sample application, test-edge.com.
Enter the application.
Go to the Page Rules page.
Here’s a page rule created in an earlier video tutorial.
Let’s create a new page rule to return a file directly.
We enable a rule condition to check for the URI /images/bird.jpg
.
We choose the string equality operator.
Enter the value /images/bird.jpg
to match the static file.
And turn on the Content.
Select a file that we just uploaded.
Expand the directory.
Select image “bird.jpg”.
We insert this page rule before the proxy page rule.
Create the rule.
We need to make a new release to push out our new changes, as always.
Click on this button.
Ship it!
Our new release is now synchronized to all our gateway servers.
Test
Then we send an HTTP request with URI “images/bird.jpg”.
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.
Edit the Page Rule.
Choose the operator “Prefix matches”.
Enter the value /images
to match the static resource location only.
Select the directory that we just created.
“Ignore URI Prefix” allows the requested URL to be different from the actual resource URI, which we will demonstrate later.
Save the rule.
We need to make a new release to push out our new changes, as always.
Click on this button.
Ship it!
Our new release is now synchronized to all our gateway servers.
Then we send a request with URI “images/cat.jpg”.
The request returned a cat image in the subdirectory “images”.
What will happen if we request “images/bird.jpg”?
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.
Edit the Page Rule.
We still use the “test” directory.
Then choose to ignore “String”.
Type “/images/”.
Save the rule.
We need to make a new release to push out our new changes, as always.
Click on this button.
Ship it!
Our new release is now synchronized to all our gateway servers.
we send a request with the “/images” prefix.
The request returned a bird image.
Then we request the image in the subdirectory “images”.
The request returned a cat image.
Then we send a request with the “/no” prefix of a non-existent resource.
The error page was returned.
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.