nginx开发笔记_Filter模块执行顺序

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仅对动态模块有效,对静态模块无效。

时间: 2024-08-29 05:32:04

nginx开发笔记_Filter模块执行顺序的相关文章

Junit4学习笔记--方法的执行顺序

package com.lt.Demo.TestDemo; import java.util.Arrays; import java.util.Collection; import org.junit.After; import org.junit.AfterClass; import org.junit.Before; import org.junit.BeforeClass; import org.junit.Test; import org.junit.runner.RunWith; im

静态初始化模块执行顺序

测试静态初始化模块执行顺序的程序: 1 class Root 2 { 3 static{ 4 System.out.println("Root的静态初始化块"); 5 } 6 { 7 System.out.println("Root的普通初始化块"); 8 } 9 public Root() 10 { 11 System.out.println("Root的无参数的构造器"); 12 } 13 } 14 class Mid extends Roo

nginx开发笔记_ngx_palloc源码解析

功能简介 ngx_pool_t是nginx开发中最经常使用到的内存容器.对动态内存的封装,由框架进行创建与释放,模块开发过程中仅需要进行内存申请,不需要关注何时释放.常见的pool对象有: 1. ngx_init_cycle()创建的cycle->pool.常见的cf->pool也是指向cycle->pool,这个pool的生命周期很长,直到进程退出ngx_worker_process_exit()/ngx_master_process_exit()才释放. 2.ngx_http_cre

算法笔记_017:递归执行顺序的探讨(Java)

目录 1 问题描述 2 解决方案 2.1 问题化简 2.2 定位输出测试 2.3 回顾总结 1 问题描述 最近两天在思考如何使用蛮力法解决旅行商问题(此问题,说白了就是如何求解n个不同字母的所有不同排序的序列问题,即共有n!次不同排序). 为此,我认真看了一篇出自CSDN上的博客文章,其中有一段核心代码就是在for循环里面添加一句递归调用语句,来实现n!次排序.因此,我对文章中的那段核心代码苦苦不得其解--其执行顺序究竟是咋样的呢? 附其简要代码: public int count = 0; p

nginx 学习五 filter模块简介和实现一个简单的filter模块

1 nginx过滤模块简介 过滤(filter)模块是过滤响应头和内容的模块,可以对回复的头和内容进行处理.它的处理时间在获取回复内容之后, 向用户发送响应之前.它的处理过程分为两个阶段,过滤HTTP回复的头部和主体,在这两个阶段可以分别对头部和主体 进行修改. 2 过滤模块执行顺序 2.1 ngx_http_output_(head, body)_filter_pt 先看一下nginx常用的过滤模块,在ngx_moudles.c中有一下代码: ngx_module_t *ngx_modules

ionic2+ 开发笔记

开发笔记 ionic 模块定义 ionic2+ 环境下运行已有的ionic1项目 将ionic.project 重命名为 ionic.config.json 引用外部js 文件 将外部文件放到www文件下 在src/index.html 引用外部文件 <script src="./jquery-3.2.1.min.js"></script> ` 注意路径写法 (./为当前路径,../为上级目录) 安装camera插件报错 安装camera插件报错(https:/

Nginx学习笔记六Nginx的模块开发

1.Nginx配置文件主要组成:main(全局配置)这部分的指令将影响其他所有部分.server(虚拟主机配置)这部分指令主要用于指定虚拟主机域名,IP和端口.upstream(主要为反向代理,负载均衡相关配置)这部分指令用于设置反向代理及后端服务 器的负载均衡.location(目录匹配配置)这部分指令用于匹配网页位置(例如,根目录"/","/images",等 等). location部分会继承server部分的指令,而server部分会继承main部分的指令.

nginx与Lua执行顺序

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读取并解析完请求头之后就立即

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

大多数 Nginx 新手都会频繁遇到这样一个困惑,那就是当同一个 location 配置块使用了多个 Nginx 模块的配置指令时,这些指令的执行顺序很可能会跟它们的书写顺序大相径庭.于是许多人选择了“试错法”,然后他们的配置文件就时常被改得一片狼藉.这个系列的教程就旨在帮助读者逐步地理解这些配置指令背后的执行时间和先后顺序的奥秘. 现在就来看这样一个令人困惑的例子:     ? location /test {    ?     set $a 32;    ?     echo $a;