Ngx_lua module API description

Source: Internet
Author: User
: This article mainly introduces the ngx_lua module API description. if you are interested in the PHP Tutorial, refer to it. Ngx_lua module API description

# Ngx commands
Lua_code_cache on | off;
Purpose: enable or disable the Lua code cache and affect the following commands: set_by_lua_file, content_by_lua_file, rewrite_by_lua_file, access_by_lua_file, and force loading or reload the Lua module. to modify the LUA code when the cache is enabled, restart nginx. Cache is usually disabled during development.
Scope: main, server, location, location if
Lua_regex_cache_max_entries 1024;
Role: unknown (it seems to be the maximum number of cached regular expression processing results)
Lua_package_path.../path ...;
Purpose: Set the extension library path written with lua code.
Example: lua_package_path '/foo/bar /?. Lua;/blah /?. Lua ;;';
Lua_package_cpath '/bar/baz /?. So;/blah /?. So ;;';
Purpose: Set the lua library path for C extension.
Set_by_lua $ var' '[$ Arg1 $ arg2];
Set_by_lua_file $ var [$ Arg1 $ arg2...];
Purpose: Set an Nginx variable. the variable value is returned from the return operation in the lua script to implement complicated value assignment logic. this is blocking and the Lua code must be very fast.
In addition, the existing ngx variables can be passed as parameters to the Lua script for access by ngx. arg [1], ngx. arg [2], and other methods.
Scope: main, server, location, server if, location if
Processing phase: rewrite
Content_by_lua' ';
Content_by_lua_file luafile;
Scope: location, location if
Note: The content processor receives the request processing and outputs the response. content_by_lua directly writes the shorter Lua code in the nginx configuration file, and the latter uses the lua file.
Rewrite_by_lua' '
Rewrite_by_lua_file lua_file;
Scope: http, server, location, location if
Execute internal URL rewriting or external redirection, such as pseudo-static URL rewriting. It is executed at the end of the rewrite processing stage by default.
Note: When rewrite_by_lua is used, the rewrite_log on is enabled, and the corresponding rewrite log is not displayed.
Access_by_lua 'lua code ';
Access_by_lua_file lua_file.lua;
Purpose: it is used for access control. for example, you can use the following method to allow access from only intranet ip addresses.
Access_by_lua'
If ngx. req. get_uri_args () ["token"] ~ = "123" then
Return ngx. exit (403)
End ';
Scope: http, server, location, location if
Header_filter_by_lua 'lua code ';
Header_filter_by_lua_file path_file.lua;
Purpose: Set the header and cookie;
Lua_need_request_body on | off;
Purpose: whether to read the request body, which is similar to the ngx. req. read_body () function. However, this method is not officially recommended.
Lua_shared_dict shared_data 10 m;
Purpose: Set a shared global variable table to share among all worker processes. In the lua script, you can access it as follows:
Example: local shared_data = ngx. shared. shared_data
10 m.
Init_by_lua 'lua code ';
Init_by_lua_file lua_file.lua;
Scope: http
Note: The g‑master process is executed when the configuration is loaded. it is usually used to initialize the global configuration/pre-load the Lua module.
Init_worker_by_lua 'lua code ';
Init_worker_by_lua_file luafile. lua;
Scope: http

Note: The timer called when each Nginx Worker process starts. if the Master process is not allowed, it will only be called after init_by_lua. it is usually used to regularly pull configuration/data or back-end service health checks.

######################

Methods and constants
######################

Ngx. arg [index] # ngx command parameter. when this variable is used in set_by_lua or set_by_lua_file, it is read-only and refers to configuring the command input parameters. ngx. var. varname # read and write NGINX variable values. it is best to cache the variable values in the lua script to avoid memory leakage during the lifecycle of the current request ngx. config. ngx_lua_version # Current ngx_lua module version ngx. config. nginx_version # nginx version ngx. worker. exiting # whether the current worker process is shutting down ngx. worker. pid # PIDngx of the current worker process. config. nginx_configure # At compilation. /configure command option ngx. config. prefix # The prefix option core constans: # ngx_lua core constant ngx. OK (0) ngx. ERROR (-1) ngx. AGAIN (-2) ngx. DONE (-4) ngx. DECLINED (-5) ngx. nilhttp method constans: # It is often used in ngx. location. catpure and ngx. location. the capture_multi method is called. ngx. HTTP_GETngx.HTTP_HEADngx.HTTP_PUTngx.HTTP_POSTngx.HTTP_DELETEngx.HTTP_OPTIONS ngx. HTTP_MKCOL ngx. HTTP_COPY ngx. HTTP_MOVE ngx. HTTP_PROPFIND ngx. HTTP_PROPPATCH ngx. HTTP_LOCK ngx. HTTP_UNLOCK ngx. HTTP_PATCH ngx. HTTP_TRACE http status constans: # http request status constant ngx. HTTP_ OK (200) ngx. HTTP_CREATED (201) ngx. HTTP_SPECIAL_RESPONSE (300) ngx. HTTP_MOVED_PERMANENTLY (301) ngx. HTTP_MOVED_TEMPORARILY (302) ngx. HTTP_SEE_OTHER (303) ngx. HTTP_NOT_MODIFIED (304) ngx. HTTP_BAD_REQUEST (400) ngx. HTTP_UNAUTHORIZED (401) ngx. httpp_forbidden (403) ngx. HTTP_NOT_FOUND (404) ngx. HTTP_NOT_ALLOWED (405) ngx. HTTP_GONE (410) ngx. HTTP_INTERNAL_SERVER_ERROR (500) ngx. HTTP_METHOD_NOT_IMPLEMENTED (501)) Ngx. http: service_unavailable (503) ngx. HTTP_GATEWAY_TIMEOUT (504) Nginx log level constants: # Error log level constant, these parameters are often in ngx. used in the log method. ngx. STDERRngx. EMERGngx. ALERTngx. CRITngx. ERRngx. WARNngx. NOTICEngx. INFOngx. DEBUG ################### methods in the API: ################# print () # and ngx. the print () method is different. print () is equivalent to ngx. log () ngx. ctx # This is a lua table that is used to save the ngx context variables and is valid throughout the request lifecycle. for details, refer to the official ngx. location. capture () # send a subrequest. for detailed usage, see the official documentation. Ngx. location. capture_multi () # send multiple subrequests. for detailed usage, see the official documentation. Ngx. status # read or write the status of the current request. it must be called before the corresponding output header. ngx. header. HEADER # access or set http header information. For more information, see the official documentation. Ngx. req. set_uri () # set the URI of the current request. For more information, see the official documentation ngx. set_uri_args (args) # redefines the URI parameter of the current request according to the args parameter. ngx. req. get_uri_args () # returns a lua table containing all the URL parameters of the current request, ngx. req. get_post_args () # returns a lua table, including the POST parameter ngx of all current requests. req. get_headers () # returns a lua table containing information about the current request header. ngx. req. set_header () # set a field value of the current request header. the subrequest of the current request is not affected. ngx. req. read_body () # synchronously read the body information of the client without blocking other ngnix events. [details] ngx. req. discard_body () # explicitly discard the bodyngx of the client request. req. get_body_d Ata () # obtain the client's request body in the form of a string ngx. req. get_body_file () # obtain the file name ngx when sending a file request. req. set_body_data () # set the BODYngx requested by the client. req. set_body_file () # use filename to specify the file data of the current request. Ngx.req.clear_header()# ngngx.exe c (uri, args) # execute an internal redirect, according to the uri and the request parameter ngx. redirect (uri, status) # execute 301 or 302 redirection. Ngx. send_headers () # send the specified response header ngx. headers_sent # determine whether the header is sent to the client ngx. headers_sent = truengx. print (str) # ngx. say () # functions similar to ngx. print, but ngx will be wrapped after the say method is output. log (log. level ,...) # write nginx logs ngx. flush () # output the buffer content to the page (refresh response) ngx. exit (http-status) # end the request and output the status code ngx. eof () # explicitly specify to close the output stream ngx. escape_uri () # URI encoding (this function does not encode the comma, whereas php's urlencode will encode the code) ngx. unescape_uri () # uri decoding ngx. encode_args (table) # resolve tabel to url parameter ngx. decode_args (uri) # encode the parameter string as a tablengx. encode_base64 (str) # BASE64 encoding ngx. decode_base64 (str) # BASE64 decodes ngx. crc32_short (str) # crs32_short hash ngx of the string. crc32_long (str) # crs32_long hash ngx of the string. hmac_sha1 (str) # hmac_sha1 hash ngx of the string. md5 (str) # return hexadecimal MD5ngx. md5_bin (str) # returns the binary MD5ngx. today () # returns the current date yyyy-mm-ddngx.time () # returns the current timestamp ngx. now () # returns the current time ngx. update_time () # returns ngx after refreshing. localtime () # returns yyyy-mm-dd hh: ii: ssngx. utctime () # returns the utc time ngx in the format of yyyy-mm-dd hh: ii: ss. cookie_time (sec) # returns the time ngx for COOKIE use. http_time (sec) # return the time ngx that can be used by the http header. parse_http_time (str) # The time when the HTTP header is parsed ngx. is_subrequest # whether a subrequest (value: true or false) ngx. re. match (subject, regex, options, ctx) # ngx Regular Expression Matching. for details, refer to ngx on the official website. re. gmatch (subject, regex, opt) # global regular expression matching ngx. re. sub (sub, reg, opt) # Match and replace (unknown) ngx. re. gsub () # unknown ngx. shared. DICT # ngx. shared. DICT is a table that stores all the global memory sharing variables ngx. shared. DICT. get ngx. shared. DICT. get_stale ngx. shared. DICT. set ngx. shared. DICT. safe_set ngx. shared. DICT. add ngx. shared. DICT. safe_add ngx. shared. DICT. replace ngx. shared. DICT. delete ngx. shared. DICT. incr ngx. shared. DICT. flush_all ngx. shared. DICT. flush_expired ngx. shared. DICT. get_keysndk.set_var.DIRECTIVE # do not understand

# Reference:

Http://wiki.nginx.org/HttpLuaModuleZh#Core_constants ngx_lua official documentation
Http://blog.csdn.net/imlsz/article/details/42915473 for the official API Main Content Translation
Http://jinnianshilongnian.iteye.com/blog/2186448 has detailed examples of ngx_lua user documentation
Brief introduction of ngx_lua module method by http://www.cnblogs.com/wangxusummer/p/4309007.html

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the ngx_lua module API description, including the content, hope to be helpful to friends who are interested in the PHP Tutorial.

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.