In windows, openresty uses lua for interface forwarding and secondary encapsulation, and openrestylua

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.