SoFunction
Updated on 2025-04-14

What is OpenResty, the difference between OpenResty and Nginx?

In today's high concurrency and low latency Internet scenarios, Nginx, as a lightweight and high-performance web server, has almost become the standard configuration of infrastructure. However, the limitations of traditional Nginx are also obvious: static configuration and logic expansion rely on C module development, making it seem stretched in scenarios where dynamic business logic is needed.
The emergence of OpenResty broke this deadlock. It upgrades the "static proxy server" to a "dynamic application platform" by deeply integrating Lua scripts into Nginx.

Part 1: What is OpenResty?

1. The birth background of OpenResty

OpenResty was founded in 2009 by Chinese developer Zhang Yichun (agentzh), with the original intention of solving Nginx's shortcomings in dynamic business processing. By deeply combining LuaJIT (high-performance Lua compiler) with Nginx, developers can write business logic directly in the request processing process without relying on external services or complex C module development.

2. Core capabilities: Dynamic script-driven web platform

  • Lua script embedding: Embed Lua code at various request stages of Nginx (such as authentication, routing, and response processing) to implement dynamic logic.
  • Non-blocking high concurrency: Inheriting the event-driven model of Nginx, a single process can easily support hundreds of thousands of concurrent connections.
  • Full-stack middleware support: directly operate database, cache, and message queues through built-in libraries (such as lua-resty-redis, lua-resty-mysql).
  • Hot update: There is no need to restart the service after modifying the code, and it takes effect in real time, greatly improving development efficiency.

3. Typical application scenarios

  • API gateway: dynamic routing, authentication, traffic control.
  • Web Application Firewall (WAF): Intercept malicious requests in real time.
  • Edge computing: Process data at edge nodes close to the user (such as JSON parsing, A/B testing).
  • High-performance microservices: directly operate the database and replace some back-end services.

Part 2: OpenResty vs Nginx: Comparison and Contact

1. Core connection: a consistent underlying architecture

  • Basic homologous: OpenResty is built on the core code of Nginx, reusing its event-driven model, reverse proxy, load balancing and other core functions.
  • Configuration compatibility: All Nginx configuration files (such as  ) can be used directly in OpenResty, which has low learning costs.
  • Module Sharing: OpenResty supports traditional Nginx's C modules (such as ngx_http_rewrite_module) and extends the Lua ecosystem.

2. Core differences: From "static proxy" to "dynamic platform"

Dimension

Nginx

OpenResty

Functional positioning

High-performance static server/reverse proxy

Dynamic application platform + full-featured gateway

Programming capabilities

Supports C module development only

Native Lua scripts that support hot loading

Business logic processing

Rely on external services or complex module extensions

Embed Lua code directly into the request process

Middleware interaction

Need to call external services through reverse proxy

Built-in library direct connection Redis/MySQL/Kafka

Typical scenarios

Static resource hosting, load balancing

Dynamic API Gateway, Edge Computing, WAF

Development efficiency

Low (requires compiled C code)

High (Lua scripts take effect immediately)

Performance overhead

Very low (pure C implementation)

Close to Nginx (LuaJIT efficient compilation)

Example comparison: Implement a "dynamic routing by user identity" function

  • Nginx solution:
    It is necessary to write a C module resolution request header, or call an external authentication service through a reverse proxy, which has high latency and complex architecture.
  • OpenResty Solution:
    Write 10 lines of Lua code in the access_by_lua_block stage, directly read the routing rules in Redis, and forward the request dynamically.

Part 3: How to choose? Applicable scenario analysis

1. Select a typical scenario for Nginx

  • Static resource hosting: Distribute HTML/CSS/JS files or images.
  • Basic reverse proxy: forward the request to the backend Tomcat, service.
  • SSL End and Caching: Configure HTTPS and Caching policies.
  • Simple load balancing: use basic strategies such as polling and weight allocation.

2. Select a typical scenario for OpenResty

  • Dynamic flow control: Adjust the current limit threshold or fuse strategy based on real-time flow.
  • Edge business logic: Complete data desensitization and request verification before the request reaches the backend.
  • Lightweight microservices: Directly operate database implementation APIs (such as GET /user/:id).
  • Security protection: Implement custom WAF rules through Lua scripts.

Part 4: Analysis of the core technology of OpenResty

1. LuaJIT: Why choose Lua?

  • Lightweight and efficient: Lua language is concise, and LuaJIT's JIT compilation makes its performance close to C.
  • Embed Friendly: Lua virtual machines are small in size and are suitable for embedding into Nginx.
  • Coroutine support: implement non-blocking I/O operations through coroutines to avoid callback hell.

2. Stage-based request processing

OpenResty divides request processing into 11 stages (such as rewrite, access, content), and developers can insert Lua scripts in each stage:

location /api {
    access_by_lua_block {
        -- Authentication logic:examine JWT Token
        local token = .get_headers()["Authorization"]
        if not validate_token(token) then
            (403)
        end
    }
    content_by_lua_block {
        -- Generate a response:from MySQL Query data
        local db = require ""
        local res, err = db:query("SELECT * FROM users")
        ((res))
    }
}

Here is the article about OpenResty, what is the difference between OpenResty and Nginx? That’s all for the article. For more information about the differences between OpenResty and Nginx, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!