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. linux删除空行操作:awk、grep、tr、sed

    如下:如何删除空行 shen\nshen\\n sen seh sehe she she 真正删除空行,可以使用vim: 通过命令模式删除空行.vim在命令模式下(在vim里输入英文字符:进入命令模式 ...

  2. cache数据库之表的存储结构

    1.我们已经建了一个person类,接下来就是表的存储结构 2.打开Inspector,先输入rowid名字为p_RowID,选class->Storage 3.新建一个Storage,选择Ca ...

  3. [Hibernate开发之路](3)基础配置

    一 JDBC链接 通常你希望SessionFactory来为你创建和缓存(pool)JDBC连接. 假设你採用这样的方式, 仅仅须要例如以下例所看到的那样,打开一个Session: Session s ...

  4. trigger_error

    PHP trigger_error 自定义错误级别抛出错误. //如果设置成notice或warning则程序继续向下执行,如果是致命级别的错误则终止执行 $num1 = 1; if (is_numb ...

  5. laravel 配置了自己的域名以后, localhost 无法访问 404 not found 的解决方法

    这是后盾网视频教程的方法,应该是配置虚拟主机,此方法要改动,apache服务器里的conf文件夹里的httpd.conf文件 和conf/extral里面的httpd-vhost文件 具体改动为,co ...

  6. Matlab princomp函数浅析

    matlab中的princomp函数主要是实现主成分分析的功能,有1一个输入参数,4个返回参数,形式如下: [coef, score, latent, t2] = princomp(X) 输入: X为 ...

  7. strupr和strlwr字符串函数的使用

    strupr 功能:将小写字母转换为大写字母 strlwr 功能:将大写字母转换为小写字母 在VS2013里面使用的时候要这样的格式 _strlwr_s _strupr_s #include<s ...

  8. MGTemplateEngine 模版发动机简单使用

    https://github.com/nxtbgthng/MGTemplateEngine MGTemplateEngine 模版引擎 MGTemplateEngine比較象 PHP 中的 Smart ...

  9. docker&k8s填坑记

    本篇主要用于记录在实施docker和kubenetes过程中遇到的一个问题和解决办法. 本节部分内容摘自互联网,有些部分为自己在测试环境中遇到到实际问题,后面还会根据实际情况不断分享关于docker/ ...

  10. erlang防止反编译

    前面提到了erlang的反编译,下面说下防止反编译: 1)建立~/.erlang.crypt 在编译的用户名的home目录中建立一个加密方法的文件.erlang.crypt,内容如下: [{debug ...