"Openresty" passing parameters to Lua code

Source: Internet
Author: User

When I described the Forminputnginxmodule module earlier, I understood how openresty obtained the data of the post submission.

Then, if you need to process this data through LUA, you need to pass the data as a parameter into Lua, and Lua gets the data and returns the results to the Nginx to complete the process.

First, there is a POST request:

1 varJSON = {2Data"hello!"3 };4 $.post (5     'Save', 6 JSON,7 function (callback) {8 alert (callback);9     }Ten);

Then the Nginx configuration:

1 user root;2Worker_processes2;3 4Error_log logs/Error.log;5PID logs/Nginx.pid;6 7 Events {8Worker_connections1024x768;9 # multi_accept on;Ten } One  A http { - include mime.types; -  theAccess_log logs/Access.log; -      - server { -Listen the; + server_name localhost; -         +Location/ { ARoot/var/www/Aceeditor; at index index.htm index.html; -        } -             -Location/Save { - set_form_input $data data; - echo $data; in        } -     } to}

When we introduce the Forminputnginxmodule module, we see that this configuration obtains the data of the post submission through the Set_form_input method, and successfully returns the result to the foreground.

Now, we need to process the data on the post, so we pass data as a parameter to the LUA code, process it through the LUA code, and we need to return the result to Nginx.

Here, change the configuration of the nginx.conf 26~29 line:

1 location/Save {2    set_form_input $data data; 3     ' 4         Local " Zhang!!!  "5         return ngx.arg[1]. s; 6     ' $data; 7     echo $re; 8 }

Among them, the Set_by_lua method:

Syntax:set_by_lua $res <lua-script-str> [$arg 1 $arg 2 ...]

Function: Pass the parameter list to LUA and assign the return value of LUA to the RES variable.

Here, the Set_by_lua method passes data as a parameter to the LUA code, obtains this parameter in Lua via Ngx.arg[n], and then returns the result to Nginx after processing, then assigns the value to the variable Re,nginx and returns the re to the foreground. N in Ngx.arg[n] indicates the order in which the parameters are passed.

Look at the effect:

  

As we can see, the foreground correctly obtains the result data that the Nginx passes through the LUA processing.

If you need to separate the LUA code, you can use the Set_by_lua_file method.

So there are LUA file M.lua:

1 Local " Zhang! asdsd!!  "2return ngx.arg[1]. S

Then change the configuration above to:

1 location/Save {2    set_form_input $data data; 3     Set_by_lua_file $re/var/www/aceeditor/M.lua $data; 4     echo $re; 5 }

Look at the results:

  

"Openresty" passing parameters to Lua code

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.