2018-10-25

作者: 喵喵唔的老巢 | 来源:发表于2018-10-25 09:40 被阅读0次

    Nginx下Lua处理阶段与使用范围

    init_by_lua http

    set_by_lua server, server if, location, location if rewrite_by_lua

    http, server, location, location if access_by_lua http, server,

    location, location if content_by_lua location, location if

    header_filter_by_lua http, server, location, location if

    body_filter_by_lua http, server, location, location if log_by_lua

    http, server, location, location if init_by_lua:

    在nginx重新加载配置文件时,运行里面lua脚本,常用于全局变量的申请。

    例如lua_shared_dict共享内存的申请,只有当nginx重起后,共享内存数据才清空,这常用于统计。 set_by_lua:

    设置一个变量,常用与计算一个逻辑,然后返回结果 该阶段不能运行Output API、Control API、Subrequest

    API、Cosocket API rewrite_by_lua: 在access阶段前运行,主要用于rewrite

    access_by_lua: 主要用于访问控制,能收集到大部分变量,类似status需要在log阶段才有。 这条指令运行于nginx

    access阶段的末尾,因此总是在 allow 和 deny 这样的指令之后运行,虽然它们同属 access 阶段。

    content_by_lua:

    阶段是所有请求处理阶段中最为重要的一个,运行在这个阶段的配置指令一般都肩负着生成内容(content)并输出HTTP响应。

    header_filter_by_lua: 一般只用于设置Cookie和Headers等 该阶段不能运行Output

    API、Control API、Subrequest API、Cosocket API body_filter_by_lua:

    一般会在一次请求中被调用多次, 因为这是实现基于 HTTP 1.1 chunked 编码的所谓“流式输出”的。

    该阶段不能运行Output API、Control API、Subrequest API、Cosocket API

    log_by_lua:

    该阶段总是运行在请求结束的时候,用于请求的后续操作,如在共享内存中进行统计数据,如果要高精确的数据统计,应该使用body_filter_by_lua。

    该阶段不能运行Output API、Control API、Subrequest API、Cosocket API Nginx顺序

    Nginx 处理每一个用户请求时,都是按照若干个不同阶段(phase)依次处理的,而不是根据配置文件上的顺序。 Nginx

    处理请求的过程一共划分为 11 个阶段,按照执行顺序依次是

    post-read、server-rewrite、find-config、rewrite、post-rewrite、

    preaccess、access、post-access、try-files、content、log. post-read:

    读取请求内容阶段 Nginx读取并解析完请求头之后就立即开始运行 例如模块 ngx_realip 就在 post-read

    阶段注册了处理程序,它的功能是迫使 Nginx 认为当前请求的来源地址是指定的某一个请求头的值。 server-rewrite

    Server请求地址重写阶段 当 ngx_rewrite 模块的set配置指令直接书写在 server 配置块中时,基本上都是运行在

    server-rewrite 阶段 find-config 配置查找阶段 这个阶段并不支持 Nginx 模块注册处理程序,而是由

    Nginx 核心来完成当前请求与 location 配置块之间的配对工作。 rewrite Location请求地址重写阶段 当

    ngx_rewrite 模块的指令用于 location 块中时,便是运行在这个 rewrite 阶段。

    另外,ngx_set_misc(设置md5、encode_base64等) 模块的指令,还有 ngx_lua 模块的

    set_by_lua 指令和 rewrite_by_lua 指令也在此阶段。 post-rewrite 请求地址重写提交阶段 由

    Nginx 核心完成 rewrite 阶段所要求的“内部跳转”操作,如果 rewrite 阶段有此要求的话。 preaccess

    访问权限检查准备阶段 标准模块 ngx_limit_req 和 ngx_limit_zone

    就运行在此阶段,前者可以控制请求的访问频度,而后者可以限制访问的并发度。 access 访问权限检查阶段 标准模块

    ngx_access、第三方模块 ngx_auth_request 以及第三方模块 ngx_lua 的 access_by_lua

    指令就运行在这个阶段。 配置指令多是执行访问控制性质的任务,比如检查用户的访问权限,检查用户的来源 IP 地址是否合法

    post-access 访问权限检查提交阶段 主要用于配合 access 阶段实现标准 ngx_http_core 模块提供的配置指令

    satisfy 的功能。 satisfy all(与关系) satisfy any(或关系) try-files

    配置项try_files处理阶段 专门用于实现标准配置指令 try_files 的功能 如果前 N-1

    个参数所对应的文件系统对象都不存在,try-files 阶段就会立即发起“内部跳转”到最后一个参数(即第 N 个参数)所指定的

    URI. content 内容产生阶段 Nginx 的 content

    阶段是所有请求处理阶段中最为重要的一个,因为运行在这个阶段的配置指令一般都肩负着生成“内容” 并输出 HTTP 响应的使命。

    相关文章

      网友评论

        本文标题:2018-10-25

        本文链接:https://www.haomeiwen.com/subject/uuqqtqtx.html