From 80e81b207796c4b39e37cf420b77318d7d1ec43d Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 27 Jan 2025 19:21:04 +0100 Subject: [PATCH 01/12] Create openresty-case-study --- src/content/docs/openresty/openresty-case-study | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 src/content/docs/openresty/openresty-case-study diff --git a/src/content/docs/openresty/openresty-case-study b/src/content/docs/openresty/openresty-case-study new file mode 100644 index 000000000..e69de29bb From 697263dfb2b962e7cba44fa053bd7fd25aa0513b Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 27 Jan 2025 20:08:51 +0100 Subject: [PATCH 02/12] Update openresty-case-study --- .../docs/openresty/openresty-case-study | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/src/content/docs/openresty/openresty-case-study b/src/content/docs/openresty/openresty-case-study index e69de29bb..7d3fd8fa3 100644 --- a/src/content/docs/openresty/openresty-case-study +++ b/src/content/docs/openresty/openresty-case-study @@ -0,0 +1,60 @@ +--- +title: Case Study: OpenResty +author: Erik Torkildsen +tags: openresty, web-server, lua, nginx, case-study +--- + +## Introduction +OpenResty is a high-performance web platform that embeds the Lua scripting language into **Nginx**, enabling developers to build dynamic web applications, APIs, and gateways. By combining Nginx’s speed with Lua’s flexibility, OpenResty solves problems like real-time request processing, edge computing, and scalable API management. Its unique integration of Lua scripting directly into Nginx makes it a standout tool for low-latency, high-concurrency use cases. + +--- + +## Brief History +- **2007**: Created by **Yichun Zhang** (agentzh) to address scalability issues at Taobao (Alibaba’s e-commerce platform). +- **2011**: Open-sourced, gaining adoption for its lightweight scripting capabilities. +- **2015**: Adopted by **Cloudflare** for edge security tools like Web Application Firewalls (WAF). +- **2017**: Became the foundation for **Kong API Gateway**, a widely-used open-source API management tool. +- **Today**: OpenResty powers critical infrastructure for fintech, e-commerce, and CDN providers, with a thriving community of contributors. + +--- + +## Main Features + +| Feature | Description | +|-----------------------|-----------------------------------------------------------------------------| +| **Lua Scripting** | Embed Lua code directly into Nginx for dynamic routing, authentication, and request/response manipulation. | +| **High Performance** | Leverages Nginx’s event-driven architecture and LuaJIT for sub-millisecond latency. | +| **Extensibility** | Integrates with Redis, MySQL, Kafka, and more via pre-built Lua modules (e.g., `lua-resty-redis`). | +| **Scalability** | Handles 10,000+ concurrent connections with minimal CPU/memory usage. | + +--- + +## Market Comparison + +### **OpenResty vs. Nginx** +| | OpenResty | Nginx | +|----------------------|------------------------------------|------------------------------------| +| **Scripting** | Lua scripting for dynamic logic | Static configuration only | +| **Use Case** | APIs, gateways, edge computing | Static content, reverse proxying | +| **Performance** | Faster for custom logic | Slightly faster for static tasks | + +### **OpenResty vs. Node.js** +| | OpenResty | Node.js | +|----------------------|------------------------------------|------------------------------------| +| **Concurrency** | Non-blocking I/O with Lua+Nginx | Event-driven JavaScript | +| **Ecosystem** | Smaller, focused on web tasks | Massive npm library for general use| +| **Latency** | Lower (microseconds) | Higher (milliseconds) | + +### **OpenResty vs. Envoy** +| | OpenResty | Envoy | +|----------------------|------------------------------------|------------------------------------| +| **Configuration** | Lua scripting for flexibility | YAML/JSON for declarative setup | +| **Service Mesh** | Manual implementation | Built-in service mesh support | +| **Use Case** | Custom gateways, edge logic | Cloud-native microservices | + +--- + +## Getting Started +Here is a step by step guide on how to use OpenResty with Docker. + +### Step 1: From f06b234b4cfc5711b8abf87eb994e47b08bc8119 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 27 Jan 2025 20:14:51 +0100 Subject: [PATCH 03/12] added .md in file name yes i forgot --- .../openresty/{openresty-case-study => openresty-case-study.md} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/content/docs/openresty/{openresty-case-study => openresty-case-study.md} (100%) diff --git a/src/content/docs/openresty/openresty-case-study b/src/content/docs/openresty/openresty-case-study.md similarity index 100% rename from src/content/docs/openresty/openresty-case-study rename to src/content/docs/openresty/openresty-case-study.md From ec525d70e6cbcddd6602f3b2a49a576c2b6b8db6 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 27 Jan 2025 20:17:59 +0100 Subject: [PATCH 04/12] Update openresty-case-study.md --- src/content/docs/openresty/openresty-case-study.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index 7d3fd8fa3..d378aca13 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -1,5 +1,5 @@ --- -title: Case Study: OpenResty +title: OpenResty Case Study author: Erik Torkildsen tags: openresty, web-server, lua, nginx, case-study --- From 27cfc1cabef7fcadf435937a3c9b908e457cad32 Mon Sep 17 00:00:00 2001 From: Erik Date: Mon, 27 Jan 2025 20:22:57 +0100 Subject: [PATCH 05/12] Update openresty-case-study.md --- src/content/docs/openresty/openresty-case-study.md | 1 - 1 file changed, 1 deletion(-) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index d378aca13..9a656774f 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -14,7 +14,6 @@ OpenResty is a high-performance web platform that embeds the Lua scripting langu - **2011**: Open-sourced, gaining adoption for its lightweight scripting capabilities. - **2015**: Adopted by **Cloudflare** for edge security tools like Web Application Firewalls (WAF). - **2017**: Became the foundation for **Kong API Gateway**, a widely-used open-source API management tool. -- **Today**: OpenResty powers critical infrastructure for fintech, e-commerce, and CDN providers, with a thriving community of contributors. --- From 0ac63adac039144cde446be05c116414d9f117e8 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 28 Jan 2025 03:02:18 +0100 Subject: [PATCH 06/12] Update openresty-case-study.md --- .../docs/openresty/openresty-case-study.md | 143 +++++++++++++++++- 1 file changed, 140 insertions(+), 3 deletions(-) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index 9a656774f..36ccdcde1 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -1,7 +1,7 @@ --- title: OpenResty Case Study author: Erik Torkildsen -tags: openresty, web-server, lua, nginx, case-study +tags: openresty, web-server, lua, nginx, docker, case-study --- ## Introduction @@ -28,6 +28,23 @@ OpenResty is a high-performance web platform that embeds the Lua scripting langu --- +## Strengths + +- **Blazing Fast Performance**: Built on Nginx and LuaJIT, OpenResty handles 10,000+ concurrent connections with minimal latency, outperforming Node.js and Python for network-bound tasks. +- **Dynamic Scripting**: Lua integration allows logic injection at any stage of the request lifecycle (e.g., authentication, rate limiting). +- **Lightweight**: Consumes fewer resources than traditional app servers, making it ideal for edge devices and microservices. +- **Extensible Ecosystem**: Pre-built modules (e.g., lua-resty-redis, lua-resty-mysql) simplify integrations with databases and APIs. +- **Community Support**: Active open-source community with 500+ contributors and enterprise adoption (e.g., Cloudflare, Kong). + +## Weaknesses + +- **Steep Learning Curve**: Requires expertise in both Nginx and Lua, which are niche compared to JavaScript or Python. +- **Limited Ecosystem**: Fewer libraries and tools compared to Node.js or Go. +- **Niche Use Cases**: Optimized for gateways/proxies, not CPU-heavy tasks like machine learning or complex business logic. +- **Debugging Complexity**: Troubleshooting Lua scripts within Nginx configurations can be challenging. + +--- + ## Market Comparison ### **OpenResty vs. Nginx** @@ -54,6 +71,126 @@ OpenResty is a high-performance web platform that embeds the Lua scripting langu --- ## Getting Started -Here is a step by step guide on how to use OpenResty with Docker. +Here is a step by step guide on how to set up OpenResty with Docker. + +> You can alternatively download OpenResty from their [official OpenResty website](https://openresty.org/en/download.html) eliminating the need for Docker, but I found the set up to be harder, so I won't cover how to do that here. + +### Step 1: Install Docker + +If you don't already have Docker installed, download and install it from the [official Docker website](https://www.docker.com/). + +- **Windows/Mac:** Install Docker Desktop. +- **Linux:** Follow your distribution's instructions (e.g., using `apt` or `yum`). + +After installation, verify Docker is installed by running: +```bash +docker --version +``` + +### Step 2: Create a New Project Directory + +Set up a directory for your OpenResty project. This will contain the necessary files like configurations and HTML files. +```bash +mkdir openresty-docker-setup +cd openresty-docker-setup +``` + +### Step 3: Create the OpenResty Configuration File + +Create an OpenResty configuration file, `nginx.conf`, to define how OpenResty handles requests. +```bash +touch nginx.conf +``` +Add the following to `nginx.conf`: +```nginx +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + server { + listen 80; + + location / { + default_type text/html; + content_by_lua_block { + ngx.say("Hello, OpenResty running on Docker!") + } + } + } +} +``` +This basic configuration: +- Starts an OpenResty server on port 80. +- Uses Lua to generate dynamic content ("Hello, OpenResty running on Docker!"). + +### Step 4: Create a Dockerfile + +Create a `Dockerfile` to define the Docker image for OpenResty. +```bash +touch Dockerfile +``` +Add the following content: +```Dockerfile +FROM openresty/openresty:alpine + +COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf + +EXPOSE 80 + +CMD ["openresty", "-g", "daemon off;"] +``` +- **Base image:** Uses the official OpenResty image (`openresty/openresty:alphine`). +- **Copy configuration:** Copies your `nginx.conf` to the correct location in the container. +- **Expose port 80:** Makes OpenResty accessible on port 80. +- **Command:** Starts OpenResty when the container runs. + +### Step 5: Build the Docker Image + +In the same directory, build your Docker image using the following command: +```bash +docker build -t openresty-demo . +``` +- `-t openresty-demo`: Tags the image as `openresty-demo`. + +### Step 6: Run the OpenResty Container + +Run the container based on your image: +```bash +docker run -d -p 8080:80 openresty-demo +``` +- `-d`: Runs the container in detached mode (background). +- `-p 8080:80`: Maps port 80 in the container to port 8080 on your local machine. +- `openresty-demo`: Specifies the image to use. + +### Step 7: Test Your Setup + +Open a web browser and go to `http://localhost:8080`. You should see the text: + +``` +Hello, OpenResty running on Docker! +``` +If you see this message, your OpenResty server is up and running! + +--- + +## Conclusion + +OpenResty is a **high-performance, Lua-driven extension of Nginx** that excels in scenarios demanding speed, customization, and low resource usage. + +### Key Advantages: + +- Ideal for building **API gateways, edge computing tools, and real-time request processors.** +- Outperforms traditional web servers (e.g., Apache) in high-concurrency environments. + +### Limitations: + +- Less suited for full-stack applications or CPU-intensive workloads. +- Requires significant effort to master Lua and Nginx’s internals. + +### Future Outlook: + +As edge computing and microservices grow, OpenResty’s role in modern infrastructure will expand. While it won’t replace general-purpose frameworks like Django or Express.js, it remains unmatched for **low-latency, high-throughput web tasks.** -### Step 1: From 94bbb6c3db0745864dab7e7e79c1698a17f9e91e Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 28 Jan 2025 04:53:34 +0100 Subject: [PATCH 07/12] Update openresty-case-study.md --- .../docs/openresty/openresty-case-study.md | 280 +++++++++++++++--- 1 file changed, 232 insertions(+), 48 deletions(-) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index 36ccdcde1..f0a750e26 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -4,69 +4,86 @@ author: Erik Torkildsen tags: openresty, web-server, lua, nginx, docker, case-study --- -## Introduction -OpenResty is a high-performance web platform that embeds the Lua scripting language into **Nginx**, enabling developers to build dynamic web applications, APIs, and gateways. By combining Nginx’s speed with Lua’s flexibility, OpenResty solves problems like real-time request processing, edge computing, and scalable API management. Its unique integration of Lua scripting directly into Nginx makes it a standout tool for low-latency, high-concurrency use cases. +## Introduction +In today’s world of real-time web applications and APIs, performance and scalability are critical. OpenResty, a high-performance web platform, combines the speed of **Nginx** with the flexibility of **Lua scripting**. This powerful combination allows developers to create dynamic web applications, APIs, and gateways while maintaining low latency and high concurrency. Whether it’s real-time request processing, edge computing, or scalable API management, OpenResty excels at tasks where speed and efficiency are paramount. + --- -## Brief History -- **2007**: Created by **Yichun Zhang** (agentzh) to address scalability issues at Taobao (Alibaba’s e-commerce platform). -- **2011**: Open-sourced, gaining adoption for its lightweight scripting capabilities. -- **2015**: Adopted by **Cloudflare** for edge security tools like Web Application Firewalls (WAF). -- **2017**: Became the foundation for **Kong API Gateway**, a widely-used open-source API management tool. +## Brief History + +### **Early Development (2007-2011)** +- Created by **Yichun Zhang** (agentzh) to tackle scalability issues at Taobao, Alibaba’s e-commerce platform. +- Enhanced Nginx with embedded Lua scripting for dynamic content handling. + +### **Open Source Expansion (2011-2017)** +- OpenResty gained adoption for its lightweight design and high performance. +- **2015**: Adopted by Cloudflare for edge computing tools like Web Application Firewalls (WAF). +- **2017**: Became the foundation for the **Kong API Gateway**, now a leading API management tool. + +### **Modern Usage** +- Today, OpenResty powers microservices, dynamic APIs, and edge applications in enterprises worldwide. --- ## Main Features -| Feature | Description | -|-----------------------|-----------------------------------------------------------------------------| -| **Lua Scripting** | Embed Lua code directly into Nginx for dynamic routing, authentication, and request/response manipulation. | -| **High Performance** | Leverages Nginx’s event-driven architecture and LuaJIT for sub-millisecond latency. | -| **Extensibility** | Integrates with Redis, MySQL, Kafka, and more via pre-built Lua modules (e.g., `lua-resty-redis`). | -| **Scalability** | Handles 10,000+ concurrent connections with minimal CPU/memory usage. | +| Feature | Description | Use Case | +|-----------------------|-----------------------------------------------------------------------------|-------------------------------------| +| **Lua Scripting** | Embed Lua code directly into Nginx for dynamic routing, authentication, and request/response manipulation. | Custom APIs, request validation | +| **High Performance** | Leverages Nginx’s event-driven architecture and LuaJIT for sub-millisecond latency. | Real-time applications, proxies | +| **Extensibility** | Integrates with Redis, MySQL, Kafka, and more via pre-built Lua modules (e.g., `lua-resty-redis`). | Database-backed APIs, caching | +| **Scalability** | Handles 10,000+ concurrent connections with minimal CPU/memory usage. | High-traffic websites, microservices| + --- -## Strengths +## Strengths -- **Blazing Fast Performance**: Built on Nginx and LuaJIT, OpenResty handles 10,000+ concurrent connections with minimal latency, outperforming Node.js and Python for network-bound tasks. -- **Dynamic Scripting**: Lua integration allows logic injection at any stage of the request lifecycle (e.g., authentication, rate limiting). -- **Lightweight**: Consumes fewer resources than traditional app servers, making it ideal for edge devices and microservices. -- **Extensible Ecosystem**: Pre-built modules (e.g., lua-resty-redis, lua-resty-mysql) simplify integrations with databases and APIs. -- **Community Support**: Active open-source community with 500+ contributors and enterprise adoption (e.g., Cloudflare, Kong). +- **Blazing Fast Performance**: + Powered by LuaJIT and Nginx, OpenResty handles over 10,000 concurrent connections with low latency. + *Example*: Ideal for serving large-scale APIs or real-time web applications. -## Weaknesses +- **Dynamic Scripting**: + Lua allows logic injection at any stage of the request lifecycle, enabling custom behavior for requests. + *Example*: Implementing advanced rate-limiting algorithms. -- **Steep Learning Curve**: Requires expertise in both Nginx and Lua, which are niche compared to JavaScript or Python. -- **Limited Ecosystem**: Fewer libraries and tools compared to Node.js or Go. -- **Niche Use Cases**: Optimized for gateways/proxies, not CPU-heavy tasks like machine learning or complex business logic. -- **Debugging Complexity**: Troubleshooting Lua scripts within Nginx configurations can be challenging. +- **Extensible Ecosystem**: + Pre-built modules like `lua-resty-redis` make it easy to connect with databases, caches, or messaging queues. + +--- + +## Weaknesses + +- **Steep Learning Curve**: + Requires familiarity with both Lua and Nginx, which are less commonly used than Python or JavaScript. + *Tip*: Start with small examples to bridge the gap. + +- **Debugging Complexity**: + Debugging Lua scripts embedded in Nginx configurations can be challenging. + *Example*: Issues often require logging or specialized tools for troubleshooting. --- ## Market Comparison ### **OpenResty vs. Nginx** -| | OpenResty | Nginx | -|----------------------|------------------------------------|------------------------------------| -| **Scripting** | Lua scripting for dynamic logic | Static configuration only | -| **Use Case** | APIs, gateways, edge computing | Static content, reverse proxying | -| **Performance** | Faster for custom logic | Slightly faster for static tasks | +| Feature | OpenResty | Nginx | +|----------------------|--------------------------------------|------------------------------------| +| **Dynamic Logic** | Lua scripting for flexibility | Static configuration only | +| **Best Use Case** | APIs, gateways, edge computing | Static sites, simple reverse proxy | ### **OpenResty vs. Node.js** -| | OpenResty | Node.js | -|----------------------|------------------------------------|------------------------------------| -| **Concurrency** | Non-blocking I/O with Lua+Nginx | Event-driven JavaScript | -| **Ecosystem** | Smaller, focused on web tasks | Massive npm library for general use| -| **Latency** | Lower (microseconds) | Higher (milliseconds) | +| Feature | OpenResty | Node.js | +|----------------------|---------------------------------------|--------------------------------------| +| **Concurrency** | Optimized for sub-millisecond latency | Event-driven, higher latency | +| **Best Use Case** | APIs and edge logic | Full-stack apps, heavy business logic| ### **OpenResty vs. Envoy** -| | OpenResty | Envoy | +| Feature | OpenResty | Envoy | |----------------------|------------------------------------|------------------------------------| -| **Configuration** | Lua scripting for flexibility | YAML/JSON for declarative setup | -| **Service Mesh** | Manual implementation | Built-in service mesh support | -| **Use Case** | Custom gateways, edge logic | Cloud-native microservices | +| **Configuration** | Lua scripting for custom behavior | YAML/JSON for declarative setup | +| **Best Use Case** | Custom API gateways | Cloud-native service meshes | --- @@ -82,7 +99,7 @@ If you don't already have Docker installed, download and install it from the [of - **Windows/Mac:** Install Docker Desktop. - **Linux:** Follow your distribution's instructions (e.g., using `apt` or `yum`). -After installation, verify Docker is installed by running: +After installation, verify Docker is installed by running (in Terminal): ```bash docker --version ``` @@ -168,7 +185,6 @@ docker run -d -p 8080:80 openresty-demo ### Step 7: Test Your Setup Open a web browser and go to `http://localhost:8080`. You should see the text: - ``` Hello, OpenResty running on Docker! ``` @@ -176,21 +192,189 @@ If you see this message, your OpenResty server is up and running! --- +### Optional: Expand the Project with Dynamic Content + +Now that you have OpenResty running with Docker and serving content, let's expand on the project to include dynamic HTML. This involves: + +- Creating Lua modules for reusable logic. +- Using HTML templates for dynamic content. +- Configuring OpenResty to handle both static and dynamic routes. + +We'll work step-by-step through the necessary files and configurations. + +### Step 8: Update `nginx.conf` + +```nginx +worker_processes 1; + +events { + worker_connections 1024; +} + +http { + include mime.types; + default_type text/html; + + # Add Lua module search path + lua_package_path "/usr/local/openresty/nginx/lua/?.lua;;"; + + server { + listen 80; + + # Dynamic HTML + location / { + content_by_lua_block { + local template = require "resty.template" + local my_module = require "my_module" + + # Fetch dynamic data from Lua module + local data = my_module.get_data() + + # Render HTML template with data + template.render("my_template.html", data) + } + } + } +} +``` +- `lua_package_path`: Defines where Lua modules are stored. We use `/usr/local/openresty/nginx/lua/` to match the Docker setup. +- **Route `/`:** + - Loads `lua-resty-template` for HTML rendering. + - Fetches dynamic data from a Lua module (`my_module`). + - Passes the data to `my_template.html` for rendering. + +### Step 9: Create an HTML Template + +Add a file named `my_template.html` to define the structure of the dynamic page. This file is located at `/usr/local/openresty/nginx/html/` inside the container. +```bash +touch my_template.html +``` +Add the following content: +```html + + + + {{ title }} + + +

{{ heading }}

+

Message: {{ message }}

+ + +``` +- `{{ title }}`, `{{ heading }}`, and `{{ message }}` are placeholders. +- The template engine replaces these placeholders with data from the Lua module. + +### Step 10: Add Lua Module + +Create a Lua file named `my_module.lua` in the `lua/` directory: +```bash +mkdir lua +cd lua +touch my_module.lua +``` +Add the following content: +```lua +local _M = {} + +function _M.get_data() + return { + title = "My Dynamic Page", + heading = "Hello, OpenResty!", + message = "Data loaded from a separate Lua module." + } +end + +return _M +``` +- `my_module.lua`: Provides dynamic data (as a Lua table) to be rendered in the template. + +### Step 11: Update the Dockerfile + +Your Dockerfile needs to include the new Lua modules and HTML template. Here's the updated version: +```Dockerfile +FROM openresty/openresty:alpine-fat + +# Install lua-resty-template (for dynamic HTML rendering) +RUN opm get bungle/lua-resty-template + +# Copy Lua modules +COPY lua /usr/local/openresty/nginx/lua/ + +# Copy HTML templates +COPY my_template.html /usr/local/openresty/nginx/html/ + +# Copy custom Nginx configuration +COPY nginx.conf /usr/local/openresty/nginx/conf/nginx.conf + +# Expose port 80 +EXPOSE 80 + +# Start OpenResty +CMD ["openresty", "-g", "daemon off;"] +``` +- `lua-resty-template` is installed for rendering templates. +- Lua modules and templates are copied into the appropriate directories. +- The `nginx.conf` file is copied to configure the web server. + +### Step 12: Build and Run the Project + +First check if you have any running containers and then stop and remove them. +```bash +docker ps +docker stop +docker rm +``` + +**Build the Docker Image** +Run this command in the terminal: +```bash +docker build -t openresty-dynamic . +``` + +**Run the Container** +Start the container and expose port 80 to access it locally: +```bash +docker run -d -p 8080:80 openresty-dynamic +``` + +**Test Your Dynamic Page** +- Visit `http://localhost:8080` to see the dynamic page: + **Output:** A rendered HTML page with the data provided by `my_module`. + +### Step 13: Next Steps + +- **Customize the Data:** Modify my_module.lua to fetch data from a database or API. +- **Add More Routes:** Define additional routes in nginx.conf for new features. +- **Deploy the Project:** Use a cloud provider (like AWS, GCP, or Azure) to make the project accessible to others. + +### Summary + +In this step-by-step guide, we created a basic OpenResty setup and expaned it to serve dynamic content: +- **Dynamic Page:** Data-driven HTML rendered with Lua. +By combining OpenResty, Lua modules, and Docker, you have a powerful yet lightweight framework for creating dynamic web applications. + +--- + ## Conclusion -OpenResty is a **high-performance, Lua-driven extension of Nginx** that excels in scenarios demanding speed, customization, and low resource usage. +OpenResty is a `high-performance, Lua-driven extension of Nginx` that shines in scenarios requiring speed, scalability, and customization. Throughout this case study, we explored its setup, features, and practical applications for building dynamic web experiences. -### Key Advantages: +### Key Takeaways: -- Ideal for building **API gateways, edge computing tools, and real-time request processors.** -- Outperforms traditional web servers (e.g., Apache) in high-concurrency environments. +- `Performance:` Built on Nginx, OpenResty efficiently handles high-concurrency workloads with low latency. +- `Flexibility:` Lua scripting enables powerful customizations, making OpenResty ideal for edge computing and microservices. +- `Integration:` It combines seamlessly with Nginx’s ecosystem, offering a lightweight yet capable alternative to full-stack frameworks. ### Limitations: -- Less suited for full-stack applications or CPU-intensive workloads. -- Requires significant effort to master Lua and Nginx’s internals. +Despite its strengths, OpenResty has a few challenges: + +- A `steep learning curve` for those new to Lua or Nginx. +- A `smaller ecosystem` compared to tools like Node.js or Python frameworks. +- Limited suitability for heavy business logic or end-to-end application development. -### Future Outlook: +### Final Thoughts: -As edge computing and microservices grow, OpenResty’s role in modern infrastructure will expand. While it won’t replace general-purpose frameworks like Django or Express.js, it remains unmatched for **low-latency, high-throughput web tasks.** +As industries increasingly adopt `edge computing and microservices`, OpenResty is well-positioned to meet modern demands for low-latency, high-throughput solutions. While it may not replace general-purpose frameworks, its strengths make it invaluable for `specialized server-side tasks`. For developers prioritizing performance and control, OpenResty is an outstanding choice. From 2c4d85dde9d7e2e9d0c696551ce03ca6c99f845b Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 28 Jan 2025 05:00:07 +0100 Subject: [PATCH 08/12] Update openresty-case-study.md --- .../docs/openresty/openresty-case-study.md | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index f0a750e26..5d14929d2 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -92,6 +92,8 @@ Here is a step by step guide on how to set up OpenResty with Docker. > You can alternatively download OpenResty from their [official OpenResty website](https://openresty.org/en/download.html) eliminating the need for Docker, but I found the set up to be harder, so I won't cover how to do that here. +--- + ### Step 1: Install Docker If you don't already have Docker installed, download and install it from the [official Docker website](https://www.docker.com/). @@ -104,6 +106,8 @@ After installation, verify Docker is installed by running (in Terminal): docker --version ``` +--- + ### Step 2: Create a New Project Directory Set up a directory for your OpenResty project. This will contain the necessary files like configurations and HTML files. @@ -112,6 +116,8 @@ mkdir openresty-docker-setup cd openresty-docker-setup ``` +--- + ### Step 3: Create the OpenResty Configuration File Create an OpenResty configuration file, `nginx.conf`, to define how OpenResty handles requests. @@ -143,6 +149,8 @@ This basic configuration: - Starts an OpenResty server on port 80. - Uses Lua to generate dynamic content ("Hello, OpenResty running on Docker!"). +--- + ### Step 4: Create a Dockerfile Create a `Dockerfile` to define the Docker image for OpenResty. @@ -164,6 +172,8 @@ CMD ["openresty", "-g", "daemon off;"] - **Expose port 80:** Makes OpenResty accessible on port 80. - **Command:** Starts OpenResty when the container runs. +--- + ### Step 5: Build the Docker Image In the same directory, build your Docker image using the following command: @@ -172,6 +182,8 @@ docker build -t openresty-demo . ``` - `-t openresty-demo`: Tags the image as `openresty-demo`. +--- + ### Step 6: Run the OpenResty Container Run the container based on your image: @@ -182,6 +194,8 @@ docker run -d -p 8080:80 openresty-demo - `-p 8080:80`: Maps port 80 in the container to port 8080 on your local machine. - `openresty-demo`: Specifies the image to use. +--- + ### Step 7: Test Your Setup Open a web browser and go to `http://localhost:8080`. You should see the text: @@ -202,6 +216,8 @@ Now that you have OpenResty running with Docker and serving content, let's expan We'll work step-by-step through the necessary files and configurations. +--- + ### Step 8: Update `nginx.conf` ```nginx @@ -243,6 +259,8 @@ http { - Fetches dynamic data from a Lua module (`my_module`). - Passes the data to `my_template.html` for rendering. +--- + ### Step 9: Create an HTML Template Add a file named `my_template.html` to define the structure of the dynamic page. This file is located at `/usr/local/openresty/nginx/html/` inside the container. @@ -265,6 +283,8 @@ Add the following content: - `{{ title }}`, `{{ heading }}`, and `{{ message }}` are placeholders. - The template engine replaces these placeholders with data from the Lua module. +--- + ### Step 10: Add Lua Module Create a Lua file named `my_module.lua` in the `lua/` directory: @@ -289,6 +309,8 @@ return _M ``` - `my_module.lua`: Provides dynamic data (as a Lua table) to be rendered in the template. +--- + ### Step 11: Update the Dockerfile Your Dockerfile needs to include the new Lua modules and HTML template. Here's the updated version: @@ -317,6 +339,8 @@ CMD ["openresty", "-g", "daemon off;"] - Lua modules and templates are copied into the appropriate directories. - The `nginx.conf` file is copied to configure the web server. +--- + ### Step 12: Build and Run the Project First check if you have any running containers and then stop and remove them. @@ -342,12 +366,16 @@ docker run -d -p 8080:80 openresty-dynamic - Visit `http://localhost:8080` to see the dynamic page: **Output:** A rendered HTML page with the data provided by `my_module`. +--- + ### Step 13: Next Steps - **Customize the Data:** Modify my_module.lua to fetch data from a database or API. - **Add More Routes:** Define additional routes in nginx.conf for new features. - **Deploy the Project:** Use a cloud provider (like AWS, GCP, or Azure) to make the project accessible to others. +--- + ### Summary In this step-by-step guide, we created a basic OpenResty setup and expaned it to serve dynamic content: From 7a34abf023c2cbda0f4895de60ef09a7fe75876f Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 28 Jan 2025 05:07:23 +0100 Subject: [PATCH 09/12] Update openresty-case-study.md --- src/content/docs/openresty/openresty-case-study.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index 5d14929d2..55b4556eb 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -363,7 +363,7 @@ docker run -d -p 8080:80 openresty-dynamic ``` **Test Your Dynamic Page** -- Visit `http://localhost:8080` to see the dynamic page: +- Visit `http://localhost:8080` to see the dynamic page: **Output:** A rendered HTML page with the data provided by `my_module`. --- @@ -390,9 +390,9 @@ OpenResty is a `high-performance, Lua-driven extension of Nginx` that shines in ### Key Takeaways: -- `Performance:` Built on Nginx, OpenResty efficiently handles high-concurrency workloads with low latency. -- `Flexibility:` Lua scripting enables powerful customizations, making OpenResty ideal for edge computing and microservices. -- `Integration:` It combines seamlessly with Nginx’s ecosystem, offering a lightweight yet capable alternative to full-stack frameworks. +- **Performance:** Built on Nginx, OpenResty efficiently handles high-concurrency workloads with low latency. +- **Flexibility:** Lua scripting enables powerful customizations, making OpenResty ideal for edge computing and microservices. +- **Integration:** It combines seamlessly with Nginx’s ecosystem, offering a lightweight yet capable alternative to full-stack frameworks. ### Limitations: From 9820c55733b5d1668d5f1038b572b17af279c2e2 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 28 Jan 2025 05:18:45 +0100 Subject: [PATCH 10/12] Update openresty-case-study.md --- src/content/docs/openresty/openresty-case-study.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index 55b4556eb..4ada4e87e 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -406,3 +406,15 @@ Despite its strengths, OpenResty has a few challenges: As industries increasingly adopt `edge computing and microservices`, OpenResty is well-positioned to meet modern demands for low-latency, high-throughput solutions. While it may not replace general-purpose frameworks, its strengths make it invaluable for `specialized server-side tasks`. For developers prioritizing performance and control, OpenResty is an outstanding choice. +--- + +## References + +[OpenResty Documentation](https://openresty.org/en/) - Official documentation for OpenResty, including installation and Lua scripting guides. + +## Additional Resources + +[Hello World HTTP Example in OpenResty](https://www.youtube.com/watch?v=eSfYLvVQMxw&list=PLlR4WakbzQp0iUvHwJeBcG5MKWgGa_ahU&index=11) - YouTube tutorial explaining OpenResty basics and setup. +[OpenResty GitHub Repository](https://github.com/openresty/openresty) - Source code, modules, and community contributions for OpenResty. +[LuaRocks](https://luarocks.org/) - A package manager for Lua, useful for OpenResty developers. + From b13dacb2bcc0a830dca9b58b53fb0bcdbb29468c Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 28 Jan 2025 05:20:59 +0100 Subject: [PATCH 11/12] Update openresty-case-study.md --- src/content/docs/openresty/openresty-case-study.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index 4ada4e87e..3dfcd9397 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -414,7 +414,7 @@ As industries increasingly adopt `edge computing and microservices`, OpenResty i ## Additional Resources -[Hello World HTTP Example in OpenResty](https://www.youtube.com/watch?v=eSfYLvVQMxw&list=PLlR4WakbzQp0iUvHwJeBcG5MKWgGa_ahU&index=11) - YouTube tutorial explaining OpenResty basics and setup. -[OpenResty GitHub Repository](https://github.com/openresty/openresty) - Source code, modules, and community contributions for OpenResty. -[LuaRocks](https://luarocks.org/) - A package manager for Lua, useful for OpenResty developers. +[Hello World HTTP Example in OpenResty](https://www.youtube.com/watch?v=eSfYLvVQMxw&list=PLlR4WakbzQp0iUvHwJeBcG5MKWgGa_ahU&index=11) - YouTube tutorial explaining OpenResty basics and setup. +[OpenResty GitHub Repository](https://github.com/openresty/openresty) - Source code, modules, and community contributions for OpenResty. +[LuaRocks](https://luarocks.org/) - A package manager for Lua, useful for OpenResty developers. From e1c2dd9b8888a77cbd55f07a5f8d14efe09d4f07 Mon Sep 17 00:00:00 2001 From: Erik Date: Tue, 28 Jan 2025 05:21:42 +0100 Subject: [PATCH 12/12] Update openresty-case-study.md --- src/content/docs/openresty/openresty-case-study.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/content/docs/openresty/openresty-case-study.md b/src/content/docs/openresty/openresty-case-study.md index 3dfcd9397..d5a385c51 100644 --- a/src/content/docs/openresty/openresty-case-study.md +++ b/src/content/docs/openresty/openresty-case-study.md @@ -410,11 +410,11 @@ As industries increasingly adopt `edge computing and microservices`, OpenResty i ## References -[OpenResty Documentation](https://openresty.org/en/) - Official documentation for OpenResty, including installation and Lua scripting guides. +- [OpenResty Documentation](https://openresty.org/en/) - Official documentation for OpenResty, including installation and Lua scripting guides. ## Additional Resources -[Hello World HTTP Example in OpenResty](https://www.youtube.com/watch?v=eSfYLvVQMxw&list=PLlR4WakbzQp0iUvHwJeBcG5MKWgGa_ahU&index=11) - YouTube tutorial explaining OpenResty basics and setup. -[OpenResty GitHub Repository](https://github.com/openresty/openresty) - Source code, modules, and community contributions for OpenResty. -[LuaRocks](https://luarocks.org/) - A package manager for Lua, useful for OpenResty developers. +- [Hello World HTTP Example in OpenResty](https://www.youtube.com/watch?v=eSfYLvVQMxw&list=PLlR4WakbzQp0iUvHwJeBcG5MKWgGa_ahU&index=11) - YouTube tutorial explaining OpenResty basics and setup. +- [OpenResty GitHub Repository](https://github.com/openresty/openresty) - Source code, modules, and community contributions for OpenResty. +- [LuaRocks](https://luarocks.org/) - A package manager for Lua, useful for OpenResty developers.