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.
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-httpBasic 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.bodyAdvanced 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
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Open Source Tech Hub
Sharing cutting-edge internet technologies and practical AI resources.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
