Introduction to ngx lua API

This section mainly takes you through the commonly used ngx_lua API.

1. nginx lua directives and api

ngx_lua has more than 60 instructions (Directive) and more than 140  APIs (as of 2019-3-26).

Official lua documentation:

https://www.nginx.com/resources/wiki/modules/lua/

https://github.com/openresty/lua-nginx-module

1. The instruction  is a method provided by ngx_lua for Nginx to call location, which rewriteis at the same level as that of Nginx  . Instructions have their own scope, for example: content_by_lua_fileonly works on locationand location ifinside:

2. API  refers to a series of methods or constants implemented by ngx_lua based on lua code, following the grammatical rules of lua. Can only be used in lua code blocks or lua files.

E.g:

content_by_lua '
    ngx.say("<p>hello, world</p>")
';

Among them content_by_luaare instructions, acting on locationblocks; it ngx.say()is the API provided by ngx_lua.

The location of the instructions and API can be found on the official documentation :

Next, we use ngx_lua to complete another small function: implement base64 decoding and re-encode json output. Some instructions and APIs will be used in the code.

Second, test the ngx lua API

Lua code:

nginx / conf / Contact / decode_info.lua

-- 实现base64的解码并重新json编码输出

local json = require "cjson"

ngx.req.read_body()
local args = ngx.req.get_post_args()

if not args or not args.info then
        ngx.exit(ngx.HTTP_BAD_REQUEST)
end

local client_ip = ngx.var.remote_var or '127.0.0.1'
local user_agnet = ngx.req.get_headers()['user_agent'] or ''
local info = ngx.decode_base64(args.info)

local res = {}
res.client_ip = client_ip
res.user_agnet = user_agnet
res.info = info

ngx.say(json.encode(res))

Modify nginx.conf, add:

location /decode_info {
    content_by_lua_file conf/lua/decode_info.lua;
}
$php -r "echo base64_encode('test');"
dGVzdA==
$ curl -XPOST -d "info=dGVzdA==" http://127.0.0.1:8080/decode_info
{"user_agnet":"curl\/7.19.7","client_ip":"127.0.0.1","info":"test"}

Description:
1. It requireis the keyword of introducing other libraries in lua. The cjson introduced here  .
2. When we want to read the post data in http, we need to use it ngx.req.read_body(). The API reads the client request body synchronously without blocking the Nginx event loop.
3. ngx.req.get_post_args() Used to obtain post request data.
4. ngx.var.remote_varThe variable in nginx is actually obtained remote_var. In other words, ngx.var.xxxit is actually the variable in the nginx obtained xxx. E.g

For details of nginx variables, see: [Alphabetical index of variables} ( http://nginx.org/en/docs/varindex.html ). ngx_lua  ngx.var API see: ngx.var.VARIABLE .
5. ngx.req.get_headers() Used to read the header parameter of nginx. Lua table is returned.
6. ngx.decode_base64()Used for base64 character string decoding. The corresponding coding API is  ngx.encode_base64().

Published 524 original articles · praised 172 · 100,000+ views

Guess you like

Origin blog.csdn.net/INGNIGHT/article/details/104838157