zoukankan      html  css  js  c++  java
  • HttpLuaModule——翻译(Nginx API for Lua)

    现在我已经将翻译的内容放到:http://wiki.nginx.org/HttpLuaModuleZh

    Nginx API for Lua

    Introduction

    各种各样的*_by_lua和*_by_lua_file配置文件服务在都在nginx.conf文件内。这些LUA API只能运行在这些配置文件里面。

    这个API有两个标准的包NGX和NDK。这个包在ngx_lua内默认的包。

    这个软件包可以这样的引入外部的文件

        local say = ngx.say
     
        module(...)
     
        function foo(a) 
            say(a) 
        end

    强烈不推荐使用package.seeall标志,这样会引起很多意想不到的后果。

    也可以直接引用LUA包:

        local ngx = require "ngx"
        local ndk = require "ndk"

    在我们的代码里面使用网络I/O操作时,最好使用LUA的API,因为NGINX是非阻塞的,如果不这样做可能死循环或者导致性能的直线下降。磁盘操作的数据量相对较小,可以采用标准的Lua的IO库,但巨大的文件阅读和写作应尽可能避免。为了发挥NGINX的性能,强烈建议所有的网络和磁盘I/O操作Nginx的子请求(通过ngx.location.capture方法和类似)都不要阻塞NGINX进程。

    ngx.arg

    语法:val = ngx.arg[index]

    环境:set_by_lua*,body_filter_by_lua*

    当它运行在set_by_lua或者set_by_lua_file指令的时候,这个table是只读的并且包括配置指令的输入参数,例如:value = ngx.arg[n]

    这里有个例子:

        location /foo {
            set $a 32;
            set $b 56;
     
            set_by_lua $res
                'return tonumber(ngx.arg[1]) + tonumber(ngx.arg[2])'
                $a $b;
     
            echo $sum;
        }

    输出的结果是 88

    当table是根据body_filter_by_lua或body_filter_by_lua_file的上下文使用的时候,第一个元素,在过滤器的输出代码中保存的输入数据块和所述第二元件持有“EOF”的标志,表示整个输出数据流的结束的布尔标志。

    数据块和“EOF”标志传递给下游的Nginx的输出滤波器,直接对应的表元素赋值,也可以覆盖。当设置为零或空的Lua字符串值ngx.arg的[1],没有数据块会被传递给所有的下游的Nginx的输出滤波器。

    ngx.var.VARIABLE

    语法:ngx.var.var_name

    语境:set_by_lua*,rewrite_by_lua*,access_by_lua...

    读写NGNIX变量。

        value = ngx.var.some_nginx_variable_name
        ngx.var.some_nginx_variable_name = value

    不仅仅可以读,也可以写,例如: 

        location /foo {
            set $my_var ''; # this line is required to create $my_var at config time
            content_by_lua '
                ngx.var.my_var = 123;
                ...
            ';
        }

    当然,NGNIX不能凭空被添加。一些特殊的NGINX变量,比如$args和$limit_rate能够被赋值,有些不能,比如$arg_PARAMETER。

    给ngx.var.Foo一个nil,将会取消NGINX的$Foo变量。

    警告:当从nginx读取变量,NGINX会给每一个请求的内存池分配内存,他在内存终止的时候会释放出来,所以当你需要读取NGINX变量的时候,你的LUA变量会缓存NGINX变量,例如:

        local val = ngx.var.some_var
        --- use the val repeatedly later

    防止临时请求请求周期内的内存泄漏。

    Core constants

    环境: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)

    注意:只有三种常量被NGNIX LUA使用(ngx.exit()只接收 NGX_OK, NGX_ERROR, 和NGX_DECLINED)

    ngx.null是NULL,在LUA中是nil,类似于lua-cjson的cjson.null。

    HTTP method constants

    环境: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) 

    这些变量经常在ngx.location.capture 或者 ngx.location.capture_multi方法中被调用

    HTTP status constants

    环境: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 (200)
      value = ngx.HTTP_CREATED (201)
      value = ngx.HTTP_SPECIAL_RESPONSE (300)
      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 (400)
      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 (500)
      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

    语法:print(...)

    环境: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.NOTICE的日志级别写入到nginx的error.log文件。他等价于:ngx.log(ngx.NOTICE, ...)

    LUA可以接收nil参数,并且等价于字符串"nil"。同样适用于ngx的布尔型的变量"true"和"false"。ngx.null将会以字符串"null"输出。

    NGINX有一个硬编码的错误,一条日志只能在2048个字节内。如果多余这些个数,将会被截取。如果想要修改这个限制,需要修改NGINX的源代码,位于src/core/ngx_log.h的NGX_MAX_ERROR_STR宏。

  • 相关阅读:
    使用tcmalloc编译启动时宕机
    使用tcmalloc编译出现undefined reference to `sem_init'
    使用AddressSanitizer做内存分析(一)——入门篇
    VIM-美化你的标签栏
    Entity Framework Code First (六)存储过程
    Entity Framework Code First (五)Fluent API
    Entity Framework Code First (四)Fluent API
    Entity Framework Code First (三)Data Annotations
    Entity Framework Code First (二)Custom Conventions
    Entity Framework Code First (一)Conventions
  • 原文地址:https://www.cnblogs.com/liqiu/p/3214680.html
Copyright © 2011-2022 走看看