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[]数组如下:

  1. ngx_module_t *ngx_modules[] = {
  2. &ngx_core_module,
  3. &ngx_errlog_module,
  4. &ngx_conf_module,
  5. &ngx_events_module,
  6. &ngx_event_core_module,
  7. &ngx_iocp_module,
  8. &ngx_select_module,
  9. &ngx_http_module,
  10. &ngx_http_core_module,
  11. &ngx_http_log_module,
  12. &ngx_http_upstream_module,
  13. &ngx_http_static_module,
  14. &ngx_http_autoindex_module,
  15. &ngx_http_index_module,
  16. &ngx_http_mirror_module,
  17. &ngx_http_try_files_module,
  18. &ngx_http_auth_basic_module,
  19. &ngx_http_access_module,
  20. &ngx_http_limit_conn_module,
  21. &ngx_http_limit_req_module,
  22. &ngx_http_geo_module,
  23. &ngx_http_map_module,
  24. &ngx_http_split_clients_module,
  25. &ngx_http_referer_module,
  26. &ngx_http_proxy_module,
  27. &ngx_http_fastcgi_module,
  28. &ngx_http_uwsgi_module,
  29. &ngx_http_scgi_module,
  30. &ngx_http_memcached_module,
  31. &ngx_http_empty_gif_module,
  32. &ngx_http_browser_module,
  33. &ngx_http_upstream_hash_module,
  34. &ngx_http_upstream_ip_hash_module,
  35. &ngx_http_upstream_least_conn_module,
  36. &ngx_http_upstream_keepalive_module,
  37. &ngx_http_upstream_zone_module,
  38. &ngx_http_write_filter_module,
  39. &ngx_http_header_filter_module,
  40. &ngx_http_chunked_filter_module,
  41. &ngx_http_range_header_filter_module,
  42. &ngx_http_gzip_filter_module,
  43. &ngx_http_postpone_filter_module,
  44. &ngx_http_ssi_filter_module,
  45. &ngx_http_charset_filter_module,
  46. &ngx_http_sub_filter_module,
  47. &ngx_http_gunzip_filter_module,
  48. &ngx_http_userid_filter_module,
  49. &ngx_http_headers_filter_module,
  50. /* 自定义模块地址 */
  51. &ngx_http_copy_filter_module,
  52. &ngx_http_range_body_filter_module,
  53. &ngx_http_not_modified_filter_module,
  54. NULL
  55. };

最优先执行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文件的方式实现。示例如下:

  1. ngx_module_type=HTTP_FILTER
  2. ngx_module_name=ngx_http_mytest_module
  3. ngx_addon_name=$ngx_module_name
  4. ngx_module_srcs="$ngx_addon_dir/ngx_http_mytest_module.c"
  5. . auto/module
  6. next=ngx_http_charset_filter_module
  7. HTTP_FILTER_MODULES=`echo $HTTP_FILTER_MODULES \
  8. | sed "s/$ngx_module_name//" \
  9. | 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. android wifi state and wifi ap state

    /** * Wi-Fi is currently being disabled. The state will change to {@link #WIFI_STATE_DISABLED} if * ...

  2. 算法 binary search

    // ------------------------------------------------------------------------------------------------- ...

  3. cocosStudio中使用PageView,ListView和ScrollView

    晚上吃东西好像吃坏肚子了,.但是技术还要继续研究.最近工作中要使用CocosStudio做界面,好吧,不管对他有什么偏见,学习一下吧.这里主要记录一下三个控件的使用和说明.就是ScrollView,L ...

  4. QQ空间g_tk、bkn加密参数算法

    g_tk是腾讯在QQ空间这一领域使用的密文,有写数据包或者url参数中需要加入你计算出的g_tk才能成功! 下面是通过浏览器抓包工具抓取 访问该js内容找出 QZONE.FrontPage.getAC ...

  5. allegro设置鼠标滚轮放大缩小

    allegro设置鼠标滚轮放大缩小 allegro16版本以增加可以通过鼠标滚轮进行PCB的放大缩小.具体方法如下: 首先在HOME路径下找到PCBENV文件夹,进入该文件夹打开ENV文件. 在ENV ...

  6. oracle sql修改序列为当前序列开始

    declare   v_num integer;  last_value integer;Begin  select SEQ_TBM_ID.NEXTVAL into last_value from d ...

  7. crm操作销售订单实体

    using System;     using Microsoft.Xrm.Sdk;     using Microsoft.Xrm.Sdk.Query;     using Microsoft.Cr ...

  8. Linux 中权限的再讨论( 下 )

    前言 上篇随笔讲述了Linux中权限的大致实现机制以及目录权限的相关规则.本文将讲解Linux中的三种特殊权限:SUID,SGID,Sticky权限.看完这两篇文章,你一定会对Linux的权限有个更深 ...

  9. 透视WPF 应用程序的利器

    当我们看到一些设计新颖的网站时,可以借助浏览器自带的Inspector 工具或插件方便的浏览网站布局结构及逻辑.如果是WPF 应用程序能否看到控件的架构方式呢?本篇将介绍两款工具Snoop 和WPF ...

  10. session自己定义存储,怎样更好地进行session共享;读tomcat7源代码,org.apache.catalina.session.FileStore可知

    session自己定义存储.怎样更好地进行session共享: 读tomcat源代码,org.apache.catalina.session.FileStore可知 一.详见: 方法1 public ...