1. 程式人生 > >nginx開發筆記_Filter模塊執行順序

nginx開發筆記_Filter模塊執行順序

this cgi ioc 實現 autoindex down sgi orm 如果

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模塊執行順序