ngx_modules数组中 回调函数的调用顺序是遍历ngx_modules数组,然后逐一调用postconfiguration函数,那么filter模块在ngx_modules数组中的顺序就决定了其filter函数在filter链中位置。
ngx_module_t *ngx_modules[] = {
...
&ngx_http_mytest_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_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_write_filter_module和ngx_http_header_filter_module两个模块是最靠前的两个filter模块,所以它们处于filter链的最末尾,也就是最后执行。这两个模块就是用来输出header和body的