httpluamodule--translation (Nginx API for Lua)

Source: Internet
Author: User

Now I have put the translated content to: Http://wiki.nginx.org/HttpLuaModuleZh

Nginx API for LuaIntroduction

A variety of *_by_lua and *_by_lua_file profile services are in the nginx.conf file. These LUA APIs can only be run in these configuration files.

This API has two standard packages NGX and NDK. This package is the default package within the Ngx_lua.

This package can introduce external files like this

    Local say = Ngx.say     module (...)     function foo (a)         say (a)     end

It is strongly not recommended to use the Package.seeall logo, which can cause many unintended consequences.

You can also refer to LUA packages directly:

    Local NGX = require "NGX"    local NDK = require "NDK"

When using network I/O operations in our code, it is best to use the LUA API, because Nginx is non-blocking, and if you do not do this, you may die loops or cause a straight down in performance. The amount of data on disk operations is relatively small and can be used in the standard LUA IO library, but great file reading and writing should be avoided as much as possible. In order to play nginx performance, it is strongly recommended that all network and disk I/O operation Nginx Sub-requests (through the Ngx.location.capture method and similar) do not block the nginx process.

Ngx.arg

Syntax: val = ngx.arg[index]

Environment: set_by_lua*,body_filter_by_lua*

When it runs on a set_by_lua or set_by_lua_file instruction, the table is read-only and includes the input parameters of the configuration directive, for example: value = Ngx.arg[n]

Here's an example:

    Location/foo {        set $a;        Set $b;         Set_by_lua $res            ' return Tonumber (ngx.arg[1]) + tonumber (ngx.arg[2]) '            $a $b;         echo $sum;    }

The result of the output is 88.

When table is used according to the context of Body_filter_by_lua or body_filter_by_lua_file, the first element, the input data block saved in the output code of the filter, and the second element holds the "EOF" flag, A Boolean flag that represents the end of the entire output data stream.

The data block and the "EOF" flag are passed to the downstream Nginx output filter, directly corresponding to the Table element assignment value, can also be overridden. When set to zero or empty LUA string value ngx.arg [1], no data block is passed to all downstream nginx output filters.

Ngx.var.VARIABLE

Syntax: Ngx.var.var_name

Context: Set_by_lua*,rewrite_by_lua*,access_by_lua ...

Read and write Ngnix variables.

    Value = ngx.var.some_nginx_variable_name    Ngx.var.some_nginx_variable_name = value

Not only can read, can also write, for example:

    Location/foo {        set $my _var '; # This line was required to the Create $my _var at config time        Content_by_lua '            ngx . Var.my_var = 123;            ...        ';    }

Of course, Ngnix cannot be added out of thin air. Some special nginx variables, such as $args and $limit_rate, can be assigned, some cannot, such as $arg_parameter.

Give Ngx.var.Foo a nil, will cancel the Nginx $foo variable.

warning : When reading a variable from Nginx, Nginx allocates memory to each requested memory pool, which is freed when memory is terminated, so your LUA variable caches nginx variables when you need to read Nginx variables, for example:

    Local val = Ngx.var.some_var    ---Use the Val repeatedly later

Prevents memory leaks during a temporary request request cycle.

Core Constants

Environment: init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, Body_filter_ By_lua, *log_by_lua*, ngx.timer.*

  Ngx. OK (0)  Ngx. ERROR ( -1)  ngx. AGAIN ( -2)  ngx. Done ( -4)  ngx. Declined (-5)

Note: Only three constants are used by Ngnix Lua (Ngx.exit () only receives NGX_OK, Ngx_error, and ngx_declined)

Ngx.null is null, which is nil in Lua, similar to Lua-cjson's cjson.null.

HTTP Method Constants

Environment: init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, Body_filter_ By_lua, log_by_lua*, ngx.timer.*

  Ngx. Http_get  Ngx. Http_head  Ngx. Http_put  Ngx. Http_post  Ngx. Http_delete  Ngx. Http_options   (added in the V0.5.0RC24 release)  Ngx. Http_mkcol     (added in the v0.8.2 release)  Ngx. Http_copy      (added in the v0.8.2 release)  Ngx. Http_move      (added in the v0.8.2 release)  Ngx. Http_propfind  (added in the v0.8.2 release)  Ngx. Http_proppatch (added in the v0.8.2 release)  Ngx. Http_lock      (added in the v0.8.2 release)  Ngx. Http_unlock    (added in the v0.8.2 release)  Ngx. Http_patch     (added in the v0.8.2 release)  Ngx. Http_trace     

These variables are often called in ngx.location.capture or Ngx.location.capture_multi methods.

HTTP Status Constants

Environment: init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, Body_filter_ By_lua, log_by_lua*, ngx.timer.*

  Value = Ngx. HTTP_OK (+)  value = Ngx. http_created (201)  value = Ngx. Http_special_response ()  value = Ngx. http_moved_permanently (301)  value = Ngx. Http_moved_temporarily (302)  value = Ngx. Http_see_other (303)  value = Ngx. Http_not_modified (304)  value = Ngx. Http_bad_request (+)  value = Ngx. Http_unauthorized (401)  value = Ngx. Http_forbidden (403)  value = Ngx. Http_not_found (404)  value = Ngx. Http_not_allowed (405)  value = Ngx. Http_gone (410)  value = Ngx. Http_internal_server_error (+)  value = Ngx. http_method_not_implemented (501)  value = Ngx. Http_service_unavailable (503)  value = Ngx. Http_gateway_timeout (504) (first added in the v0.3.1rc38 release)
Nginx Log Level constants
  Ngx. STDERR  Ngx. Emerg  Ngx. ALERT  Ngx. Crit  Ngx. ERR  Ngx. WARN  Ngx. NOTICE  ngx.info  ngx. DEBUG
Print

Syntax: Print (...)

Environment: init_by_lua*, set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, Body_filter_ By_lua, log_by_lua*, ngx.timer.*

To NGX. The notice log level is written to the Nginx Error.log file. He is equivalent to: Ngx.log (NGX. NOTICE, ...)

Lua can receive the nil parameter and is equivalent to the string "nil". The same applies to the variable "true" and "false" of the Boolean type of NGX. The ngx.null will be output with the string "null".

Nginx has a hard-coded error, a log can only be within 2048 bytes. If these numbers are superfluous, they will be intercepted. If you want to modify this limit, you need to modify the Nginx source code, located in the SRC/CORE/NGX_LOG.H NGX_MAX_ERROR_STR macro.

Http://www.cnblogs.com/liqiu/p/3214680.html

httpluamodule--translation (Nginx API for Lua) (GO)

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.