nginx lua api翻译

Aug 08, 2016 am 09:30 AM
api lua nbsp nginx

    nginx的lua模块提供了很多lua的函数API给用户使用,以便让lua更好的操作nginx
  • 24.1 Introduction
      这里说的Nginx api for lua,指的就是在nginx.conf文件中用*_by_lua 和*_by_lua_file指令 使用lua代码,为lua提供的专门的api。
  • 24.2 ngx.arg

    syntax: val = ngx.arg[index]

    context: set_by_lua*, body_filter_by_lua*

    通过用valua = ngx.arg[n],让nginx的变量作为参数传入lua给lua调用,使用方式如下列的代码
    <span>location</span> /foo <span>{</span><span>set</span><span>$a</span><span>32</span><span>;</span><span>set</span><span>$b</span><span>56</span><span>;</span>
     
            set_by_lua <span>$sum</span><span>'return tonumber(ngx.arg[1]) + tonumber(ngx.arg[2])'</span><span>$a</span><span>$b</span><span>;</span>
     
            echo <span>$sum</span><span>;</span><span>}</span>
    Copier après la connexion
    $sum 的值最后是88。
  • 24.3 ngx.var.VARIABLE

    syntax: ngx.var.VAR_NAME

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*

    可以通过以下的代码进行读写nginx的变量
    value = ngx.var.some_nginx_variable_name
    ngx.var.some_nginx_variable_name=value
    <span>location</span> /foo <span>{</span><span>set</span><span>$my_var</span><span>''</span><span>;</span><span><em># this line is required to create $my_var at config time</em></span>
            content_by_lua <span>'
                ngx.var.my_var = 123;
                ...
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    在lua代码里面就可以得到或者设置nginx.conf的变量,比较下跟上面的nginx.arg有什么区别~
  • 24.4 Core constants
    context: 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 <span>(</span><span>0</span><span>)</span>
      ngx.ERROR <span>(</span>-<span>1</span><span>)</span>
      ngx.AGAIN <span>(</span>-<span>2</span><span>)</span>
      ngx.DONE <span>(</span>-<span>4</span><span>)</span>
      ngx.DECLINED <span>(</span>-<span>5</span><span>)</span>
    Copier après la connexion

  • 24.5 HTTP method constants
    context: 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)
    Copier après la connexion
    一些http method的常量,一般用在ngx.location.capture 和ngx.location.capture_multi 这几个API的调用中~
  • 24.6 HTTP status constants
    context: 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 <span>=</span> ngx.HTTP_OK <span>(</span><span>200</span><span>)</span>
      value <span>=</span> ngx.HTTP_CREATED <span>(</span><span>201</span><span>)</span>
      value <span>=</span> ngx.HTTP_SPECIAL_RESPONSE <span>(</span><span>300</span><span>)</span>
      value <span>=</span> ngx.HTTP_MOVED_PERMANENTLY <span>(</span><span>301</span><span>)</span>
      value <span>=</span> ngx.HTTP_MOVED_TEMPORARILY <span>(</span><span>302</span><span>)</span>
      value <span>=</span> ngx.HTTP_SEE_OTHER <span>(</span><span>303</span><span>)</span>
      value <span>=</span> ngx.HTTP_NOT_MODIFIED <span>(</span><span>304</span><span>)</span>
      value <span>=</span> ngx.HTTP_BAD_REQUEST <span>(</span><span>400</span><span>)</span>
      value <span>=</span> ngx.HTTP_UNAUTHORIZED <span>(</span><span>401</span><span>)</span>
      value <span>=</span> ngx.HTTP_FORBIDDEN <span>(</span><span>403</span><span>)</span>
      value <span>=</span> ngx.HTTP_NOT_FOUND <span>(</span><span>404</span><span>)</span>
      value <span>=</span> ngx.HTTP_NOT_ALLOWED <span>(</span><span>405</span><span>)</span>
      value <span>=</span> ngx.HTTP_GONE <span>(</span><span>410</span><span>)</span>
      value <span>=</span> ngx.HTTP_INTERNAL_SERVER_ERROR <span>(</span><span>500</span><span>)</span>
      value <span>=</span> ngx.HTTP_METHOD_NOT_IMPLEMENTED <span>(</span><span>501</span><span>)</span>
      value <span>=</span> ngx.HTTP_SERVICE_UNAVAILABLE <span>(</span><span>503</span><span>)</span>
      value <span>=</span> ngx.HTTP_GATEWAY_TIMEOUT <span>(</span><span>504</span><span>)</span><span>(</span>first added in the v0.3.1rc38 release<span>)</span>
    Copier après la connexion
    http响应状态的常量
  • 24.7 Nginx log level constants
    context: 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.STDERR
      ngx.EMERG
      ngx.ALERT
      ngx.CRIT
      ngx.ERR
      ngx.WARN
      ngx.NOTICE
      ngx.INFO
      ngx.DEBUG
    Copier après la connexion
    nginx日志的一些级别常量,一般用在 ngx.log的api中
  • 24.8 print

    syntax: print(...)

    context: init_by_lua*, init_worker_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.*

    这个API是采用ngx.NOTICE的日志级别将参数的值写入error.log文件中,等同于ngx.log(ngx.NOTICE,...)
  • 24.9 ngx.ctx
    context: init_worker_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.ctx.xxx  xxx是任意的一个变量名,ngx.ctx可以看成一个临时字典,作用域是每一个请求,也就是说不同的request是不同的ngx.ctx
    如下面的例子
    <span>location</span> /test <span>{</span>
            rewrite_by_lua <span>'
                ngx.say("foo = ", ngx.ctx.foo)
                ngx.ctx.foo = 76
            '</span><span>;</span>
            access_by_lua <span>'
                ngx.ctx.foo = ngx.ctx.foo + 3
            '</span><span>;</span>
            content_by_lua <span>'
                ngx.say(ngx.ctx.foo)
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    输出的结果是
        foo = nil
        <span>79</span>
    Copier après la connexion
     这个ngx.ctx.foo的实例是贯穿在一个请求中rewrite、access还有content三个周期。
    再看下面这个例子
    <span>location</span> /sub <span>{</span>
            content_by_lua <span>'
                ngx.say("sub pre: ", ngx.ctx.blah)
                ngx.ctx.blah = 32
                ngx.say("sub post: ", ngx.ctx.blah)
            '</span><span>;</span><span>}</span>
     
        <span>location</span> /main <span>{</span>
            content_by_lua <span>'
                ngx.ctx.blah = 73
                ngx.say("main pre: ", ngx.ctx.blah)
                local res = ngx.location.capture("/sub")
                ngx.print(res.body)
                ngx.say("main post: ", ngx.ctx.blah)
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    结果是
        main pre: <span>73</span>
        sub pre: nil
        sub post: <span>32</span>
        main post: <span>73</span>
    Copier après la connexion
    ngx.location.capture是请求另外一个链接,所以可以看到,sub和main中的ngx.ctx是不同的实例。
    另外在init_worker_by_lua的指令中,我们可以初始化ngx.ctx,通过字典的形式
    ngx.ctx = {foo = 32,bar = 54}
  • 24.10 ngx.location.capture

    syntax: res = ngx.location.capture(uri, options?)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    通过这个api,lua可以访问本server的其他location,只能是同一个server的。用法如下
     res <span>=</span> ngx.location.capture<span>(</span>uri<span>)</span>
    Copier après la connexion
    返回的是一个response对象,这个对象持有其他包括res.status、res.body以及res.header三个对象。
    -capture函数除了url还有其他参数可以选择,包括
    -method  设置访问的method类型
    -body 设置访问子请求的httpbody内容
    -args设置请求的参数
        ngx.location.capture<span>(</span><span>'/foo?a=1'</span>,
            <span>{</span> args <span>=</span><span>{</span> b <span>=</span><span>3</span>, c <span>=</span><span>':'</span><span>}</span><span>}</span><span>)</span>
    Copier après la connexion
    等价于下面的调用,都是传递请求参数
        ngx.location.capture<span>(</span><span>'/foo?a=1&b=3&c=%3a'</span><span>)</span>
    Copier après la connexion
    -ctx 之前在ngx.ctx说的,在一个请求中独享一个ngx.ctx。但是在这里通过ctx,可以传入一个字典,然后通过子请求设置ngx.ctx的值,从而,我们在父请求中得到子请求ngx.ctx的值。

    <span>location</span> /sub <span>{</span>
            content_by_lua <span>'
                ngx.ctx.foo = "bar";
            '</span><span>;</span><span>}</span><span>location</span> /lua <span>{</span>
            content_by_lua <span>'
                local ctx = {}
                res = ngx.location.capture("/sub", { ctx = ctx })
     
                ngx.say(ctx.foo);
                ngx.say(ngx.ctx.foo);
            '</span><span>;</span><span>}</span>
    Copier après la connexion

    结果如下,仅仅是设置ctx这个字典的值,而不是共享ngx.ctx

        bar
        nil
    Copier après la connexion
    -vars 这个参数用来在父请求中设置nginx的变量值并向子请求传递,这个方法比通过url参数传递更加有效果。
    <span>location</span> /other <span>{</span>
            content_by_lua <span>'
                ngx.say("dog = ", ngx.var.dog)
                ngx.say("cat = ", ngx.var.cat)
            '</span><span>;</span><span>}</span>
     
        <span>location</span> /lua <span>{</span><span>set</span><span>$dog</span><span>''</span><span>;</span><span>set</span><span>$cat</span><span>''</span><span>;</span>
            content_by_lua <span>'
                res = ngx.location.capture("/other",
                    { vars = { dog = "hello", cat = 32 }});
     
                ngx.print(res.body)
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    结果如下
       dog = hello
        cat = 32
    Copier après la connexion
    -copy_all_vars 拷贝父请求的nginx的变量给子请求
    <span>location</span> /other <span>{</span><span>set</span><span>$dog</span><span>"$dog world"</span><span>;</span>
            echo <span>"$uri dog: $dog"</span><span>;</span><span>}</span>
     
        <span>location</span> /lua <span>{</span><span>set</span><span>$dog</span><span>'hello'</span><span>;</span>
            content_by_lua <span>'
                res = ngx.location.capture("/other",
                    { copy_all_vars = true });
     
                ngx.print(res.body)
                ngx.say(ngx.var.uri, ": ", ngx.var.dog)
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    结果如下
        /other dog: hello world
        /lua: hello
    Copier après la connexion
    -share_all_vars 同享父请求和子请求的nginx的变量。
    <span>location</span> /other <span>{</span><span>set</span><span>$dog</span><span>"$dog world"</span><span>;</span>
            echo <span>"$uri dog: $dog"</span><span>;</span><span>}</span>
     
        <span>location</span> /lua <span>{</span><span>set</span><span>$dog</span><span>'hello'</span><span>;</span>
            content_by_lua <span>'
                res = ngx.location.capture("/other",
                    { share_all_vars = true });
     
                ngx.print(res.body)
                ngx.say(ngx.var.uri, ": ", ngx.var.dog)
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    结果如下
        /other dog: hello world
        /lua: hello world
    Copier après la connexion
    这个比copy_all_vars优先。
    -always_forward_body 如果body属性没有设置,这个属性设置为true,那将发送父请求的httpbody给子请求。
  • 24.11 ngx.location.capture_multi

    syntax: res1, res2, ... = ngx.location.capture_multi({ {uri, options?}, {uri, options?}, ... })

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    类似于上面的ngx.location.capture ,不过支持并行请求多个子请求
     res1, res2, res3 <span>=</span> ngx.location.capture_multi<span>{</span><span>{</span><span>"/foo"</span>, <span>{</span> args <span>=</span><span>"a=3&b=4"</span><span>}</span><span>}</span>,
            <span>{</span><span>"/bar"</span><span>}</span>,
            <span>{</span><span>"/baz"</span>, <span>{</span> method <span>=</span> ngx.HTTP_POST, body <span>=</span><span>"hello"</span><span>}</span><span>}</span>,
        <span>}</span>
     
        <span>if</span> res1.status <span>==</span> ngx.HTTP_OK <span>then</span>
            ...
        <span>end</span>
     
        <span>if</span> res2.body <span>==</span><span>"BLAH"</span><span>then</span>
            ...
        <span>end</span>
    Copier après la connexion
    <span><em>-- construct the requests table</em></span><span>local</span> reqs <span>=</span><span>{</span><span>}</span><span>table.insert</span><span>(</span>reqs, <span>{</span><span>"/mysql"</span><span>}</span><span>)</span><span>table.insert</span><span>(</span>reqs, <span>{</span><span>"/postgres"</span><span>}</span><span>)</span><span>table.insert</span><span>(</span>reqs, <span>{</span><span>"/redis"</span><span>}</span><span>)</span><span>table.insert</span><span>(</span>reqs, <span>{</span><span>"/memcached"</span><span>}</span><span>)</span>
     
        <span><em>-- issue all the requests at once and wait until they all return</em></span><span>local</span> resps <span>=</span><span>{</span> ngx.location.capture_multi<span>(</span>reqs<span>)</span><span>}</span>
     
        <span><em>-- loop over the responses table</em></span><span>for</span> i, resp <span>in</span><span>ipairs</span><span>(</span>resps<span>)</span><span>do</span><span><em>-- process the response table "resp"</em></span><span>end</span>
    Copier après la connexion

  • 24.12 ngx.status
    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua, log_by_lua*
    读取或者设置当前的请求响应状态,这个应该在发送内容给浏览器之前执行
        ngx.status <span>=</span> ngx.HTTP_CREATED
        status <span>=</span> ngx.status
    Copier après la connexion

  • 24.13 ngx.header.HEADER

    syntax: ngx.header.HEADER = VALUE

    syntax: value = ngx.header.HEADER

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua, log_by_lua*

    获取或者设置http header的值
    <span><em>-- equivalent to ngx.header["Content-Type"] = 'text/plain'</em></span>
        ngx.header.content_type <span>=</span><span>'text/plain'</span><span>;</span>
     
        ngx.header<span>[</span><span>"X-My-Header"</span><span>]</span><span>=</span><span>'blah blah'</span><span>;</span>
    Copier après la connexion
    多个值的可以像下面那样设置
        ngx.header<span>[</span><span>'Set-Cookie'</span><span>]</span><span>=</span><span>{</span><span>'a=32; path=/'</span>, <span>'b=4; path=/'</span><span>}</span>
    Copier après la connexion
        Set-Cookie: <span>a</span>=<span>32</span>; <span>path</span>=<span>/</span>
        Set-Cookie: <span>b</span>=<span>4</span>; <span>path</span>=<span>/</span>
    Copier après la connexion
    特别是在header_filter_by_lua有效果,如
    <span>location</span> /test <span>{</span><span>set</span><span>$footer</span><span>''</span><span>;</span>
     
            <span>proxy_pass</span><span>http</span>://some-backend<span>;</span>
     
            header_filter_by_lua <span>'
                if ngx.header["X-My-Header"] == "blah" then
                    ngx.var.footer = "some value"
                end
            '</span><span>;</span>
     
            echo_after_body <span>$footer</span><span>;</span><span>}</span>
    Copier après la connexion

  • 24.14 ngx.resp.get_headers

    syntax: headers = ngx.resp.get_headers(max_headers?, raw?)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua, log_by_lua*

    <span>local</span> h <span>=</span> ngx.resp.get_headers<span>(</span><span>)</span><span>for</span> k, v <span>in</span><span>pairs</span><span>(</span>h<span>)</span><span>do</span>
        ...
    <span>end</span>
    Copier après la connexion
    在lua中得到http请求的响应头,以字典的形式
  • 24.15 ngx.req.start_time

    syntax: secs = ngx.req.start_time()

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*, log_by_lua*

    <span>local</span> request_time <span>=</span> ngx.now<span>(</span><span>)</span> - ngx.req.start_time<span>(</span><span>)</span>
    Copier après la connexion
    用来获取此次请求发起的时候的时间,用来模拟$request_time。
  • 24.16 ngx.req.http_version

    syntax: num = ngx.req.http_version()

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*

    获取请求的http version
  • 24.17 ngx.req.raw_header

    syntax: str = ngx.req.raw_header(no_request_line?)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*

    得到原http header的字符串文本内容
      ngx.<span>print</span><span>(</span>ngx.req.raw_header<span>(</span><span>)</span><span>)</span>
    Copier après la connexion
    结果是
        GET /t HTTP/1.1
        Host: localhost
        Connection: close
        Foo: bar
    Copier après la connexion

  • 24.18 ngx.req.get_method

    syntax: method_name = ngx.req.get_method()

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*

    获得当前请求的method 名字
  • 24.19 ngx.req.set_method

    syntax: ngx.req.set_method(method_id)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*

    设置覆盖此次请求的method名字
  • 24.20 ngx.req.set_uri

    syntax: ngx.req.set_uri(uri, jump?)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*

    作用与rewrite相同,其中jump默认为false,false的时候
      ngx.req.set_uri<span>(</span><span>"/foo"</span>, <span>false</span><span>)</span>
    Copier après la connexion
    结果是
    <span>rewrite</span> ^ /foo <span>break</span><span>;</span>
    Copier après la connexion
    如果jump为true,那就是
    ngx.req.set_uri<span>(</span><span>"/foo"</span>, <span>true</span><span>)</span>
    Copier après la connexion
    结果是
    <span>rewrite</span> ^ /foo last<span>;</span>
    Copier après la connexion
  • 24.21 ngx.req.set_uri_args

    syntax: ngx.req.set_uri_args(args)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*

    rewrite当前的请求参数
    ngx.req.set_uri_args<span>(</span><span>"a=3&b=hello%20world"</span><span>)</span>
    Copier après la connexion
    或者
    ngx.req.set_uri_args<span>(</span><span>{</span> a <span>=</span><span>3</span>, b <span>=</span><span>"hello world"</span><span>}</span><span>)</span>
    Copier après la connexion

  • 24.22 ngx.req.get_uri_args

    syntax: args = ngx.req.get_uri_args(max_args?)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua, log_by_lua*

    获得请求的参数
    <span>location</span><span>=</span> /test <span>{</span>
            content_by_lua <span>'
                local args = ngx.req.get_uri_args()
                for key, val in pairs(args) do
                    if type(val) == "table" then
                        ngx.say(key, ": ", table.concat(val, ", "))
                    else
                        ngx.say(key, ": ", val)
                    end
                end
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    当访问的是 GET /test?foo=bar&bar=baz&bar=blah 结果就是
       foo: bar
        bar: baz, blah
    Copier après la connexion

  • 24.23 ngx.req.get_post_args

    syntax: args, err = ngx.req.get_post_args(max_args?)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua, log_by_lua*

    得到post提交的参数
    <span>location</span><span>=</span> /test <span>{</span>
            content_by_lua <span>'
                ngx.req.read_body()
                local args, err = ngx.req.get_post_args()
                if not args then
                    ngx.say("failed to get post args: ", err)
                    return
                end
                for key, val in pairs(args) do
                    if type(val) == "table" then
                        ngx.say(key, ": ", table.concat(val, ", "))
                    else
                        ngx.say(key, ": ", val)
                    end
                end
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    当我们用curl提交参数时
    <span><em># Post request with the body 'foo=bar&bar=baz&bar=blah'</em></span>
        $ curl <span>--data</span><span>'foo=bar&bar=baz&bar=blah'</span> localhost<span>/</span><span>test</span>
    Copier après la connexion
    结果是
        foo: bar
        bar: baz, blah
    Copier après la connexion

  • 24.24 ngx.req.get_headers

    syntax: headers = ngx.req.get_headers(max_headers?, raw?)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua, log_by_lua*

    获取当前请求的头信息
    <span>local</span> h <span>=</span> ngx.req.get_headers<span>(</span><span>)</span><span>for</span> k, v <span>in</span><span>pairs</span><span>(</span>h<span>)</span><span>do</span>
            ...
        <span>end</span>
    Copier après la connexion
        ngx.say<span>(</span><span>"Host: "</span>, ngx.req.get_headers<span>(</span><span>)</span><span>[</span><span>"Host"</span><span>]</span><span>)</span>
    Copier après la connexion

  • 24.25 ngx.req.set_header

    syntax: ngx.req.set_header(header_name, header_value)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua

    设置当前的请求头
     ngx.req.set_header<span>(</span><span>"Content-Type"</span>, <span>"text/css"</span><span>)</span>
    Copier après la connexion
      ngx.req.set_header<span>(</span><span>"Foo"</span>, <span>{</span><span>"a"</span>, <span>"abc"</span><span>}</span><span>)</span>
    Copier après la connexion

  • 24.26 ngx.req.clear_header

    syntax: ngx.req.clear_header(header_name)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, body_filter_by_lua*

    清除某一个请求头
  • 24.27 ngx.req.read_body

    syntax: ngx.req.read_body()

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    在不阻塞nginx事件轮询的情况下读取客户端请求的body
        ngx.req.read_body<span>(</span><span>)</span><span>local</span> args <span>=</span> ngx.req.get_post_args<span>(</span><span>)</span>
    Copier après la connexion
  • 24.28 ngx.req.discard_body

    syntax: ngx.req.discard_body()

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    明确丢弃客户端请求body
  • 24.29 ngx.req.get_body_data

    syntax: data = ngx.req.get_body_data()

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    以字符串的形式获得客户端的请求body内容
  • 24.30 ngx.req.get_body_file

    syntax: file_name = ngx.req.get_body_file()

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    当发送文件请求的时候,获得文件的名字
  • 24.31 ngx.req.set_body_data

    syntax: ngx.req.set_body_data(data)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    设置客户端请求的body
  • 24.32 ngx.req.set_body_file

    syntax: ngx.req.set_body_data(data)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    通过filename来指定当前请求的file data。
  • 24.33 ngx.req.init_body

    syntax: ngx.req.init_body(buffer_size?)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*

    创建一个当前请求的空白body的buffer,后续可以自己写入请求的body。
      ngx.req.init_body<span>(</span><span>128</span><span>*</span><span>1024</span><span>)</span><span><em>-- buffer is 128KB</em></span><span>for</span> chunk <span>in</span> next_data_chunk<span>(</span><span>)</span><span>    do</span>
            ngx.req.append_body<span>(</span>chunk<span>)</span><span><em>-- each chunk can be 4KB</em></span><span>end</span>
        ngx.req.finish_body<span>(</span><span>)</span>
    Copier après la connexion

  • 24.34 ngx.req.append_body

    syntax: ngx.req.append_body(data_chunk)

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*

    追加当前请求的http body
  • 24.35 ngx.req.finish_body

    syntax: ngx.req.finish_body()

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*

    标志完成ngx.req.init_body的数据追加。
  • 24.36 ngx.req.socket

    syntax: tcpsock, err = ngx.req.socket()

    syntax: tcpsock, err = ngx.req.socket(raw)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    这个方法会返回一个只读的cosocket对象,用来读取当前请求的request http body内容。
  • 24.37 ngx.exec

    syntax: ngx.exec(uri, args?)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    执行内部跳转根据url和请求参数
       ngx.exec<span>(</span><span>'/some-location'</span><span>)</span><span>;</span>
        ngx.exec<span>(</span><span>'/some-location'</span>, <span>'a=3&b=5&c=6'</span><span>)</span><span>;</span>
        ngx.exec<span>(</span><span>'/some-location?a=3&b=5'</span>, <span>'c=6'</span><span>)</span><span>;</span>
    Copier après la connexion
    <span>location</span> /foo <span>{</span>
            content_by_lua <span>'
                ngx.exec("@bar", "a=goodbye");
            '</span><span>;</span><span>}</span>
     
        <span>location</span><span>@bar</span><span>{</span>
            content_by_lua <span>'
                local args = ngx.req.get_uri_args()
                for key, val in pairs(args) do
                    if key == "a" then
                        ngx.say(val)
                    end
                end
            '</span><span>;</span><span>}</span>
    Copier après la connexion
  • 24.38 ngx.redirect

    syntax: ngx.redirect(uri, status?)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    执行301或者302的重定向。
  • 24.39 ngx.send_headers

    syntax: ok, err = ngx.send_headers()

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    指定响应头。
  • 24.40 ngx.headers_sent

    syntax: value = ngx.headers_sent

    context: set_by_lua*, rewrite_by_lua*, access_by_lua*, content_by_lua*

    判断头部是否发送给客户端了。
  • 24.41 ngx.print

    syntax: ok, err = ngx.print(...)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    发送数据给客户端响应页面
    <span>local</span><span>table</span><span>=</span><span>{</span><span>"hello, "</span>,
            <span>{</span><span>"world: "</span>, <span>true</span>, <span>" or "</span>, <span>false</span>,
                <span>{</span><span>": "</span>, <span>nil</span><span>}</span><span>}</span><span>}</span>
        ngx.<span>print</span><span>(</span><span>table</span><span>)</span>
    Copier après la connexion

  • 24.42 ngx.say

    syntax: ok, err = ngx.say(...)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    作用类似于ngx.print
  • 24.43 ngx.log

    syntax: ngx.log(log_level, ...)

    context: init_by_lua*, init_worker_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.*

    向error.log中记录日志
  • 24.44 ngx.flush

    syntax: ok, err = ngx.flush(wait?)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    将flush内容到客户端页面
  • 24.45 ngx.exit

    syntax: ngx.exit(status)

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*, header_filter_by_lua*, ngx.timer.*

        ngx.status <span>=</span> ngx.HTTP_GONE
        ngx.say<span>(</span><span>"This is our own content"</span><span>)</span><span><em>-- to cause quit the whole request rather than the current phase handler</em></span>
        ngx.<span>exit</span><span>(</span>ngx.HTTP_OK<span>)</span>
    Copier après la connexion
    当status>=200的时候,直接停止当前请求的后续操作,并且返回状态码
    当status==0的时候,跳过此次代码片段,并且继续执行下面的。
  • 24.46 ngx.eof

    syntax: ok, err = ngx.eof()

    context: rewrite_by_lua*, access_by_lua*, content_by_lua*

    <span>location</span><span>=</span> /async <span>{</span><span>keepalive_timeout</span><span>0</span><span>;</span>
            content_by_lua <span>'
                ngx.say("got the task!")
                ngx.eof()  -- a descent HTTP client will close the connection at this point
                -- access MySQL, PostgreSQL, Redis, Memcached, and etc here...
            '</span><span>;</span><span>}</span>
    Copier après la connexion
    明确指定关闭结束输出流。
  • 24.47 ngx.sleep
  • 24.48 ngx.escape_uri
  • 24.49 ngx.unescape_uri
  • 24.50 ngx.encode_args
  • 24.51 ngx.decode_args
  • 24.52 ngx.encode_base64
  • 24.53 ngx.decode_base64
  • 24.54 ngx.crc32_short
  • 24.55 ngx.crc32_long
  • 24.56 ngx.hmac_sha1
  • 24.57 ngx.md5
  • 24.58 ngx.md5_bin
  • 24.59 ngx.sha1_bin
  • 24.60 ngx.quote_sql_str
  • 24.61 ngx.today
  • 24.62 ngx.time
  • 24.63 ngx.now
  • 24.64 ngx.update_time
  • 24.65 ngx.localtime
  • 24.66 ngx.utctime
  • 24.67 ngx.cookie_time
  • 24.68 ngx.http_time
  • 24.69 ngx.parse_http_time
  • 24.70 ngx.is_subrequest
  • 24.71 ngx.re.match
  • 24.72 ngx.re.find
  • 24.73 ngx.re.gmatch
  • 24.74 ngx.re.sub
  • 24.75 ngx.re.gsub
  • 24.76 ngx.shared.DICT
  • 24.77 ngx.shared.DICT.get
  • 24.78 ngx.shared.DICT.get_stale
  • 24.79 ngx.shared.DICT.set
  • 24.80 ngx.shared.DICT.safe_set
  • 24.81 ngx.shared.DICT.add
  • 24.82 ngx.shared.DICT.safe_add
  • 24.83 ngx.shared.DICT.replace
  • 24.84 ngx.shared.DICT.delete
  • 24.85 ngx.shared.DICT.incr
  • 24.86 ngx.shared.DICT.flush_all
  • 24.87 ngx.shared.DICT.flush_expired
  • 24.88 ngx.shared.DICT.get_keys
  • 24.89 ngx.socket.udp
  • 24.90 udpsock:setpeername
  • 24.91 udpsock:send
  • 24.92 udpsock:receive
  • 24.93 udpsock:close
  • 24.94 udpsock:settimeout
  • 24.95 ngx.socket.tcp
  • 24.96 tcpsock:connect
  • 24.97 tcpsock:sslhandshake
  • 24.98 tcpsock:send
  • 24.99 tcpsock:receive
  • 24.100 tcpsock:receiveuntil
  • 24.101 tcpsock:close
  • 24.102 tcpsock:settimeout
  • 24.103 tcpsock:setoption
  • 24.104 tcpsock:setkeepalive
  • 24.105 tcpsock:getreusedtimes
  • 24.106 ngx.socket.connect
  • 24.107 ngx.get_phase
  • 24.108 ngx.thread.spawn
  • 24.109 ngx.thread.wait
  • 24.110 ngx.thread.kill
  • 24.111 ngx.on_abort
  • 24.112 ngx.timer.at
  • 24.113 ngx.config.debug
  • 24.114 ngx.config.prefix
  • 24.115 ngx.config.nginx_version
  • 24.116 ngx.config.nginx_configure
  • 24.117 ngx.config.ngx_lua_version
  • 24.118 ngx.worker.exiting
  • 24.119 ngx.worker.pid
  • 24.120 ndk.set_var.DIRECTIVE
  • 24.121 coroutine.create
  • 24.122 coroutine.resume
  • 24.123 coroutine.yield
  • 24.124 coroutine.wrap
  • 24.125 coroutine.running
  • 24.126 coroutine.status
  • 以上就介绍了nginx lua api翻译,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

    Déclaration de ce site Web
    Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn

    Outils d'IA chauds

    Undresser.AI Undress

    Undresser.AI Undress

    Application basée sur l'IA pour créer des photos de nu réalistes

    AI Clothes Remover

    AI Clothes Remover

    Outil d'IA en ligne pour supprimer les vêtements des photos.

    Undress AI Tool

    Undress AI Tool

    Images de déshabillage gratuites

    Clothoff.io

    Clothoff.io

    Dissolvant de vêtements AI

    Video Face Swap

    Video Face Swap

    Échangez les visages dans n'importe quelle vidéo sans effort grâce à notre outil d'échange de visage AI entièrement gratuit !

    Outils chauds

    Bloc-notes++7.3.1

    Bloc-notes++7.3.1

    Éditeur de code facile à utiliser et gratuit

    SublimeText3 version chinoise

    SublimeText3 version chinoise

    Version chinoise, très simple à utiliser

    Envoyer Studio 13.0.1

    Envoyer Studio 13.0.1

    Puissant environnement de développement intégré PHP

    Dreamweaver CS6

    Dreamweaver CS6

    Outils de développement Web visuel

    SublimeText3 version Mac

    SublimeText3 version Mac

    Logiciel d'édition de code au niveau de Dieu (SublimeText3)

    Comment vérifier si Nginx est démarré Comment vérifier si Nginx est démarré Apr 14, 2025 pm 01:03 PM

    Comment confirmer si Nginx est démarré: 1. Utilisez la ligne de commande: SystemCTl Status Nginx (Linux / Unix), netStat -ano | Findstr 80 (Windows); 2. Vérifiez si le port 80 est ouvert; 3. Vérifiez le message de démarrage NGINX dans le journal système; 4. Utilisez des outils tiers, tels que Nagios, Zabbix et Icinga.

    Comment configurer le nom de domaine du serveur cloud dans nginx Comment configurer le nom de domaine du serveur cloud dans nginx Apr 14, 2025 pm 12:18 PM

    Comment configurer un nom de domaine NGINX sur un serveur cloud: Créez un enregistrement A pointant vers l'adresse IP publique du serveur cloud. Ajoutez des blocs d'hôtes virtuels dans le fichier de configuration Nginx, en spécifiant le port d'écoute, le nom de domaine et le répertoire racine du site Web. Redémarrez Nginx pour appliquer les modifications. Accéder à la configuration du test de nom de domaine. Autres notes: Installez le certificat SSL pour activer HTTPS, assurez-vous que le pare-feu autorise le trafic Port 80 et attendez que la résolution DNS prenne effet.

    Comment vérifier le nom du conteneur Docker Comment vérifier le nom du conteneur Docker Apr 15, 2025 pm 12:21 PM

    Vous pouvez interroger le nom du conteneur Docker en suivant les étapes: répertorier tous les conteneurs (Docker PS). Filtrez la liste des conteneurs (à l'aide de la commande grep). Obtient le nom du conteneur (situé dans la colonne "Noms").

    Comment vérifier la version nginx Comment vérifier la version nginx Apr 14, 2025 am 11:57 AM

    Les méthodes qui peuvent interroger la version Nginx sont: utilisez la commande nginx -v; Afficher la directive de version dans le fichier nginx.conf; Ouvrez la page d'erreur Nginx et affichez le titre de la page.

    Comment démarrer le serveur Nginx Comment démarrer le serveur Nginx Apr 14, 2025 pm 12:27 PM

    Le démarrage d'un serveur Nginx nécessite différentes étapes en fonction des différents systèmes d'exploitation: Système Linux / Unix: Installez le package NGINX (par exemple, en utilisant Apt-Get ou Yum). Utilisez SystemCTL pour démarrer un service NGINX (par exemple, sudo systemctl start nginx). Système Windows: téléchargez et installez les fichiers binaires Windows. Démarrer Nginx à l'aide de l'exécutable Nginx.exe (par exemple, nginx.exe -c conf \ nginx.conf). Peu importe le système d'exploitation que vous utilisez, vous pouvez accéder au serveur IP

    Comment configurer Nginx dans Windows Comment configurer Nginx dans Windows Apr 14, 2025 pm 12:57 PM

    Comment configurer Nginx dans Windows? Installez Nginx et créez une configuration d'hôte virtuelle. Modifiez le fichier de configuration principale et incluez la configuration de l'hôte virtuel. Démarrer ou recharger nginx. Testez la configuration et affichez le site Web. Activer sélectivement SSL et configurer les certificats SSL. Définissez sélectivement le pare-feu pour permettre le trafic Port 80 et 443.

    Comment créer des conteneurs pour Docker Comment créer des conteneurs pour Docker Apr 15, 2025 pm 12:18 PM

    Créer un conteneur dans Docker: 1. Tirez l'image: docker pull [Nom du miroir] 2. Créer un conteneur: docker run [Options] [Nom du miroir] [Commande] 3. Démarrez le conteneur: docker start [Nom du conteneur]

    Comment démarrer un conteneur par Docker Comment démarrer un conteneur par Docker Apr 15, 2025 pm 12:27 PM

    Étapes de démarrage du conteneur Docker: Tirez l'image du conteneur: Exécutez "Docker Pull [Mirror Name]". Créer un conteneur: utilisez "Docker Create [Options] [Mirror Name] [Commandes et paramètres]". Démarrez le conteneur: exécutez "docker start [nom de conteneur ou id]". Vérifiez l'état du conteneur: vérifiez que le conteneur s'exécute avec "Docker PS".

    See all articles