AboutLuaModuleWeb DevelopmentThe FAQ is the content to be introduced in this article, mainly throughLUA web developmentThe questions raised in this document are solved and learned. For details, refer to this article.
How to obtain the HTTP Request Header
Access NginX built-in variable ngx. var. http_HEADER directly in ngx_lua to obtain the content of the request HEADER. For common special headers such as Content-Type and Cookie, NginX also uses special variables for independent storage. For example, the "Content-Type" header can be stored through ngx. var. get the content_type variable.
How to GET Parameters
Access the NginX built-in variable ngx. var. arg_PARAMETER in ngx_lua to obtain the get parameter.
How to obtain POST Request body data
To obtain the complete POST Request body data, you can access the NginX built-in variable ngx. var. request_body Note: NginX does not automatically read the request body before processing the request by default. Therefore, you must explicitly use the form-input-nginx module to obtain the request body from this variable, otherwise, the variable content is always empty !). If you want to obtain the form parameters submitted in POST mode, you can also use the form-input-nginx module to save the parsing process. For example:
Nginx code
- location /form {
- set_form_input $name;
- content_by_lua '
- local name = ngx.var.name or "";
- local say = ngx.say
- say("My name is: ", name)
- ';
- }
-
- location /form {
- set_form_input $name;
- content_by_lua '
- local name = ngx.var.name or "";
- local say = ngx.say
- say("My name is: ", name)
- ';
- }
How to Set/get http Response Headers
We have designed the corresponding API interface, which will be implemented soon.
How to Use Lua external Module
You can use require to reference it, just like in normal Lua code. Note that there are two methods to reference external modules through require. The old method is:
Lua code
- require("xxx")
- require("xxx")
In this way, the module namespace table is directly imported into the current global environment. The new syntax is:
Lua code
- local xxx = require("xxx")
- local xxx = require("xxx")
In this way, the module namespace table is cached in a local variable with the same name, which makes access faster and does not pollute the current global environment. But the most important thing is that in the old format ngx_lua, the module cannot be accessed after being imported! This is determined by the implementation principle of ngx_lua.
Ngx_lua uses coroutine every request to run user code. The global environment of coroutine is re-associated, so user code is equivalent to running in a sandbox, after a request is processed, all modifications to the global environment produced by the user code are discarded to avoid cross-impact between multiple requests and reduce the risk of Memory leakage caused by misuse of the global environment. The require uses the Global shared package. loaded table to cache data of loaded modules, so as to avoid repeated loading of modules. Obviously, this structure will inevitably cause the module namespace table injected into the global environment through require in the first request to be inaccessible in subsequent requests, because package will be used in subsequent requests. the loaded table already contains data from the previously loaded module. Therefore, require will not inject the namespace table into the current global environment again, causing all subsequent operations dependent on the module to fail.
In view of this problem, we recommendWeb DevelopmentPeople always use the new require writing method, that is, using the local variable cache module table). For code that cannot update the require writing method for some reason, you can clear the package before starting to process the request. in the loaded table, the module data is forcibly loaded and injected into the Global Environment. Note that Loading modules each time may cause a performance bottleneck !), For example:
Lua code
- package.loaded.xxx = nil
- require("xxx")
Summary: AboutLuaModuleWeb DevelopmentThe answers to frequently asked questions have been introduced. I hope you can learn this article to help you!