Nginx API for LUA

Source: Internet
Author: User
Tags lua
Nginx API for Lua Introduction
More APIs to view Http://wiki.nginx.org/HttpLuaModuleZh

Various *_by_lua and *_by_lua_file profile services are in the nginx.conf file. These LUA APIs can only be run within 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 be introduced as an external file

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

Strongly do not recommend the use of Package.seeall logo, this will cause a lot of unintended consequences.

You can also refer to the LUA package 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 the Nginx is non-blocking, and if not, it can be a dead loop or a linear drop in performance. Disk operations have relatively small amounts of data that can be used in a standard LUA IO library, but large file reading and writing should be avoided as much as possible. In order to perform nginx performance, it is strongly recommended that all network and disk I/O operations Nginx (through Ngx.location.capture methods and similar) not block nginx processes. Ngx.arg

Syntax: val = ngx.arg[index]

Environment: set_by_lua*,body_filter_by_lua*

When it runs in Set_by_lua or set_by_lua_file instructions, this table is read-only and includes input parameters for the configuration instruction, 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 the table is used according to the context of the Body_filter_by_lua or body_filter_by_lua_file, the first element, the input block that is saved in the output code of the filter and the second component holds the flag of "EOF", 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, can also be overridden. When the LUA string value set to zero or empty is 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 you read it, you can write it, for example:

    Location/foo {
        set $my _var '; # This line are required to create $my _var in 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.

Giving 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 released when memory is terminated, so when you need to read the Nginx variable, your LUA variable caches the Nginx variable, for example:

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

Prevents memory leaks within the 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 and is nil in Lua, similar to the cjson.null of Lua-cjson. 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     (added in the v0.8.2 release)

These variables are often invoked in the ngx.location.capture or Ngx.location.capture_multi methods of 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 (+)
  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) (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*, Rewri

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.