In windows, openresty uses lua for interface forwarding and secondary encapsulation, and openrestylua
Requirement: according to the customer's requirements, you can use lua in ngx to encapsulate the interface and re-forward it to the user or a third party.
Scenario: If you have requirements for return values, the interface shields fields, or performs some business verification.
1. Download openresty in windows and decompress it to complete the lua development environment in windows.
2. Configuration:
A. Configure the following code under http in nginx. conf:
Include mime. types; default_type application/octet-stream; lua_package_path "/lualib /?. Lua; "; # lua module lua_package_cpath"/lualib /?. So; "; # c module include lua. conf; # import the custom lua configuration file resolver 8.8.8.8;
B. Create the lua. conf file in the same directory of nginx. conf to store the routing configuration of lua.
# Lua. conf server {charset UTF-8; # Set the encoding to listen 80; server_name _; location/user {default_type 'text/html'; content_by_lua_file lua/api/userController. lua; # relative to the nginx installation directory }}
C. Create the "api" folder in the lua folder under the ngx root directory and add userController. lua processing file class to it. The Code is as follows:
Local request_method = ngx. var. request_methodlocal args = nil -- 1. Obtain the parameter value and obtain the front-end submission parameter if "GET" = request_method then args = ngx. req. get_uri_args () elseif "POST" = request_method then ngx. req. read_body () args = ngx. req. get_post_args () end -- 2. Combine the url to request a Get/Post request and obtain the Parameter local http = require "resty. http "local httpc = http. new () local url = "http: // xxxxx/user/login /".. args ["userid"] .. "/".. args ["pass"] local resStr -- response result local res, err = httpc: request_uri (url, {method = "GET", -- args = str, body = "a = 1 & B = 2", headers = {["Content-Type"] = "application/json ",}}) -- 3. In the example of restarting the parameter combination, you can handle local cjson = require "cjson" local sampleJson = [[{"age": "23", "testArray ": {"array": [8, 9, 11, 14, 25]}, "Himi": "himigame.com"}]; -- parses the json string local data = cjson. decode (sampleJson); -- print the age field ngx in the json string. say (data ["age"]); -- print the first value in the array (lua starts counting from 0 by default) ngx. say (data ["testArray"] ["array"] [1]); -- 4. Print the output of the new returned value ngx. say (res. body)
3. Start the ngx server, access address, and route to user