Filter模块执行顺序

Filter模块的执行顺序是在执行configure文件时决定的,configure文件执行完成后生成objs/ngx_modules.c,文件中定义了一个数组ngx_module_t *ngx_modules[],数组的顺序就是模块执行顺序的逆序。

一般而言,在模块的config文件中配置ngx_module_typeHTTP_FILTER后生成的默认执行顺序在ngx_http_copy_filter_module之后。

一个典型的ngx_module_t *ngx_modules[]数组如下:

ngx_module_t *ngx_modules[] = {
&ngx_core_module,
&ngx_errlog_module,
&ngx_conf_module,
&ngx_events_module,
&ngx_event_core_module,
&ngx_iocp_module,
&ngx_select_module,
&ngx_http_module,
&ngx_http_core_module,
&ngx_http_log_module,
&ngx_http_upstream_module,
&ngx_http_static_module,
&ngx_http_autoindex_module,
&ngx_http_index_module,
&ngx_http_mirror_module,
&ngx_http_try_files_module,
&ngx_http_auth_basic_module,
&ngx_http_access_module,
&ngx_http_limit_conn_module,
&ngx_http_limit_req_module,
&ngx_http_geo_module,
&ngx_http_map_module,
&ngx_http_split_clients_module,
&ngx_http_referer_module,
&ngx_http_proxy_module,
&ngx_http_fastcgi_module,
&ngx_http_uwsgi_module,
&ngx_http_scgi_module,
&ngx_http_memcached_module,
&ngx_http_empty_gif_module,
&ngx_http_browser_module,
&ngx_http_upstream_hash_module,
&ngx_http_upstream_ip_hash_module,
&ngx_http_upstream_least_conn_module,
&ngx_http_upstream_keepalive_module,
&ngx_http_upstream_zone_module,
&ngx_http_write_filter_module,
&ngx_http_header_filter_module,
&ngx_http_chunked_filter_module,
&ngx_http_range_header_filter_module,
&ngx_http_gzip_filter_module,
&ngx_http_postpone_filter_module,
&ngx_http_ssi_filter_module,
&ngx_http_charset_filter_module,
&ngx_http_sub_filter_module,
&ngx_http_gunzip_filter_module,
&ngx_http_userid_filter_module,
&ngx_http_headers_filter_module,
/* 自定义模块地址 */
&ngx_http_copy_filter_module,
&ngx_http_range_body_filter_module,
&ngx_http_not_modified_filter_module,
NULL
};

最优先执行ngx_http_not_modified_filter_module,最后执行ngx_core_module,自定义模块在ngx_http_copy_filter_module之后,ngx_http_headers_filter_module之前。

如果处于特殊目的,希望将自己的模块穿插在内部模块直接,例如在ngx_http_sub_filter_module之后再执行,自定义的脚本。可以通过修改config文件的方式实现。示例如下:

ngx_module_type=HTTP_FILTER
ngx_module_name=ngx_http_mytest_module
ngx_addon_name=$ngx_module_name
ngx_module_srcs="$ngx_addon_dir/ngx_http_mytest_module.c" . auto/module next=ngx_http_charset_filter_module
HTTP_FILTER_MODULES=`echo $HTTP_FILTER_MODULES \
| sed "s/$ngx_module_name//" \
| sed "s/$next/$next $ngx_module_name/"`

通过sed命令修改HTTP_FILTER_MODULES变量,将ngx_http_mytest_module插入到ngx_http_charset_filter_module之后。

如果将模块作为动态库编译,还可以使用config中的ngx_module_order参数配置,说明如下:

ngx_module_order

Set the load order for the module which is useful for HTTP_FILTER and HTTP_AUX_FILTER module types.

The order is stored in a reverse list. The ngx_http_copy_filter_module is near the bottom of the list so is one of the first to be executed. This reads the data for other filters. Near the top of the list is ngx_http_write_filter_module which writes the data out and is one of the last to be executed.

The format for this option is typically the current module’s name followed by a whitespace separated list of modules to insert before, and therefore execute after. The module will be inserted before the last module in the list that is found to be currently loaded.

By default for filter modules this is set to "$ngx_module_name ngx_http_copy_filter" which will insert the module before the copy filter in the list and therefore will execute after the copy filter. For other module types the default is empty.

【注意】ngx_module_order仅对动态模块有效,对静态模块无效。

nginx开发_Filter模块执行顺序的更多相关文章

  1. Nginx 编译设置模块执行顺序

    Nginx编译时,配置"--add-module=xxx"可以加入模块,当我们需要按照指定顺序来设置过滤模块执行顺序时,先配置的"--add-module=xxx&quo ...

  2. Nginx开发HTTP模块入门

    Nginx开发HTTP模块入门 我们以一个最简单的Hello World模块为例,学习Nginx的模块编写.假设我们的模块在nginx配置文件中的指令名称为hello_world,那我们就可以在ngi ...

  3. simulink模块执行顺序

    1.simulink各模块执行顺序 Simulink模块的执行顺序都是序贯进行的,也就是沿着信号的流向进行.没有输入的模块先进行计算,更新状态量与输出,需要输入信号的模块等到输入信号准备ready之后 ...

  4. Nginx 配置指令的执行顺序(一)

    大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个 location 配置块使用了多个 Nginx 模块的配置指令时,这些指令的执行顺序很可能会跟它们的书写顺序大相径庭.于是许多人选择了 ...

  5. Nginx 配置指令的执行顺序(十)

    运行在 post-rewrite 阶段之后的是所谓的 preaccess 阶段.该阶段在 access 阶段之前执行,故名preaccess. 标准模块 ngx_limit_req 和 ngx_lim ...

  6. Nginx 配置指令的执行顺序(八)

    前面我们详细讨论了 rewrite.access 和 content 这三个最为常见的 Nginx 请求处理阶段,在此过程中,也顺便介绍了运行在这三个阶段的众多 Nginx 模块及其配置指令.同时可以 ...

  7. Nginx 配置指令的执行顺序(五)

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

  8. Nginx 配置指令的执行顺序(三)

    如前文所述,除非像 ngx_set_misc 模块那样使用特殊技术,其他模块的配置指令即使是在 rewrite 阶段运行,也不能和 ngx_rewrite 模块的指令混合使用.不妨来看几个这样的例子. ...

  9. Nginx 配置指令的执行顺序(二)

    我们前面已经知道,当 set 指令用在 location 配置块中时,都是在当前请求的 rewrite 阶段运行的.事实上,在此上下文中,ngx_rewrite 模块中的几乎全部指令,都运行在 rew ...

随机推荐

  1. dubbo常见问题解答FAQ

    常见问题解答 1. 如果服务注册不上怎么办? 2. 出现RpcException: No provider available for remote service异常怎么办? 3. 出现调用超时co ...

  2. 4.【nuxt起步】-具体练习一个h5实例

    目标地址:https://www.vyuan8.com/vyuan/plugin.php?id=vyuan_fangchan&module=fangchan&pid=10079& ...

  3. Neutron网络入门

    Neutron是OpenStack核心项目之中的一个,提供云计算环境下的虚拟网络功能.Neutron的功能日益强大,并在Horizon面板中已经集成该模块.作为Neutron的核心开发人员之中的一个. ...

  4. ARC forbids Objective-C objects in structs or unions

    解决方法有二种: 1.在出错的地方加入__unsafe_unretained 2.关闭系统ARC.1.点击project   2.点击Build Setting    3.找到其以下的Objetive ...

  5. muduo::Connector、TcpClient分析

    Connector TcpClient Connector Connector用来发起连接. 在非堵塞网络中,主动发起连接比被动接收连接更为复杂,由于要考虑错误处理,还要考虑重试. 主要难点在于 1. ...

  6. kubernetes之创建基于名称空间的内存和cpu限额示例

    系列目录 首先我们创建一个名称空间 kubectl create namespace quota-mem-cpu-example 创建资源配额 apiVersion: v1 kind: Resourc ...

  7. 生产制造追溯系统-IQC来料检验

    前言 相信大家都知道,任何一家工厂都有自己的仓库,用来存储采购回来的物料,那么在供应商将我们采购的物料送到工厂之后,我们都需要一个检验动作,也就是今天要说的===>IQC来料检验,这个检验动作是 ...

  8. STL algorihtm算法iter_swap(29)

    iter_swap原型: std::iter_swap template <class ForwardIterator1, class ForwardIterator2> void ite ...

  9. windows 打开文件夹

    @echo off rem 建立链接 net use \\192.168.2.3\share /user:username password rem 打开共享文件夹 explorer \\192.16 ...

  10. 获取本地IP V4 出现::1

    获取本地IP V4 竟然得到  ::1 和 192.168.x.xxx 多出来一个::1???? 终于在网络找到答案,原来是禁用了IP V6 导致,重新勾选IP V6,或者卸载IP V6 都可以解决问 ...