Mastering lua-resty-http: Asynchronous HTTP Requests in OpenResty

This article introduces lua-resty-http, a non‑blocking Lua library for OpenResty, outlines its key features, typical use cases, installation steps, and provides both basic and advanced code examples—including configuration, Lua scripts, and caching strategies—to help developers efficiently perform HTTP requests within Nginx.

Open Source Tech Hub
Open Source Tech Hub
Open Source Tech Hub
Mastering lua-resty-http: Asynchronous HTTP Requests in OpenResty

Overview

lua-resty-http is a Lua library for OpenResty that enables non‑blocking HTTP requests from within Nginx. OpenResty combines Nginx and LuaJIT to provide a full‑featured web platform.

Project address: https://github.com/ledgetech/lua-resty-http

Features

Asynchronous non‑blocking : leverages Nginx’s event loop so requests run in the background without blocking the main thread.

Connection‑pool management : creates and reuses TCP connections to reduce handshake latency.

Rich API : supports timeout settings, proxy configuration, custom headers, redirects, authentication, etc.

SSL/TLS support : built‑in HTTPS with custom certificates.

Error handling : provides detailed error messages for debugging.

Typical Use Cases

Data fetching from RESTful APIs.

Calling external web services from OpenResty applications.

Automated testing of HTTP endpoints.

Sending logs or metrics to remote servers.

Cache invalidation based on HTTP responses.

Installation

opm get ledgetech/lua-resty-http

Basic Usage

local httpc = require("resty.http").new()

local res, err = httpc:request_uri("http://example.com/helloworld", {
    method = "POST",
    body = "a=1&b=2",
    headers = {
        ["Content-Type"] = "application/x-www-form-urlencoded"
    },
})

if not res then
    ngx.log(ngx.ERR, "request failed: ", err)
    return
end

local status = res.status
local length = res.headers["Content-Length"]
local body   = res.body

Advanced Usage

Configuration file example:

server {
    listen 80;
    server_name openresty.tinywan.com;

    location /lua_http_test {
        default_type "text/html";
        lua_code_cache off;
        content_by_lua_file conf/lua/lua_http_test.lua;
    }
}

Lua script (lua_http_test.lua):

local httpc = require("resty.http").new()

local res, err = httpc:request_uri("https://www.workerman.net/u/Tinywan", {
    method = "GET",
    body = "name=Tinywan&age=24",
    headers = {
        ["Content-Type"] = "application/x-www-form-urlencoded"
    },
    ssl_verify = false,
})

if not res then
    ngx.log(ngx.ERR, "request failed: ", err)
    return
end

ngx.say(res.body)

Testing with curl:

$ curl -i http://openresty.tinywan.com/lua_http_test
HTTP/1.1 200 OK
...
<!doctype html>
...

Project Application

The article also provides a full‑featured module that combines Redis caching, lock handling, and HTTP fetching to illustrate how to build a robust cache‑first data retrieval layer. The complete Lua script is shown in the source and includes functions for reading/writing Redis, acquiring locks, and falling back to backend HTTP calls.

Rendered Request Result

Rendered request result
Rendered request result
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CacheAsynchronousHTTPNGINXlua-resty-http
Open Source Tech Hub
Written by

Open Source Tech Hub

Sharing cutting-edge internet technologies and practical AI resources.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.