NOTE

1.2 OpenResty

OpenResty installation, Lua basics, and ngx HTTP APIs.

Web Servers / NginxCreated Updated 1 min readhistorical

This is a historical learning note and may contain outdated or incomplete understanding.

1. What It Is

Nginx enhanced with Lua. Lua can be used to access MySQL, Redis, HTTP, and so on.

2. Installation

2.1. Common Lua Components

3. Lua Basics

Lua

4. OpenResty Packages

4.1. HTTP

  • Request line
-- Get GET request parameters
local arg=ngx.req.get_uri_args()
for k,v in pairs(arg) do
    ngx.say("[GET] key:",k," v:",v)
    ngx.say("line break")
end

-- Get request parameters for a POST request
ngx.req.read_body() -- The body must be read before parsing body parameters
local arg = ngx.req.get_post_args()
for k,v in pairs(arg) do
    ngx.say("[POST] key:",k," v:",v)
    ngx.say("<br>")
end
  • Request headers
-- Get headers
local headers = ngx.req.get_headers()
for k,v in pairs(headers) do
    ngx.say("[header] name;",k," v:",v)
    ngx.say("<br>")
end
  • Request body
-- Get body information
local data = ngx.req.get_body_data()
ngx.say(data)
ngx.req.get_body_data() -- The request body may occasionally be unreadable and return nil directly. If the request body has not yet been read, call ngx.req.read_body() first (or enable lua_need_request_body to force this module to read the request body; this method is not recommended).

5. References

Discussion

Sign in with GitHub to comment. Discussions are stored as GitHub Issues.View on GitHub