nginx开发_Filter模块执行顺序
Filter模块执行顺序
Filter模块的执行顺序是在执行configure文件时决定的,configure文件执行完成后生成objs/ngx_modules.c
,文件中定义了一个数组ngx_module_t *ngx_modules[]
,数组的顺序就是模块执行顺序的逆序。
一般而言,在模块的config文件中配置ngx_module_type
为HTTP_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
andHTTP_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 isngx_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仅对动态模块有效,对静态模块无效。