Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Introduction to the Nginx filter module


May 23, 2021 Nginx Getting started


Table of contents


Introduction to the filter module

Execution time and content

The filter module is a module that filters response heads and content, which can handle the headers and content of replies. I ts processing time is before the response is sent to the user after the reply has been obtained. Its processing is divided into two stages, filtering the head and body of the HTTP reply, in which the head and body can be modified separately.

There are similar functions in the code:

ngx_http_top_header_filter(r);
ngx_http_top_body_filter(r, in);

is a function that filters the head and body, respectively. Both interfaces must be called for the response content of all modules to be returned to the client.

The order of execution

The calls to the filter module are sequential, and their order is determined at compile time. T he script that controls compilation is located in auto/modules, and when you have compiled Nginx, you can see a file under the objs directory ngx_modules.c database. Open this file with a similar code:

        ngx_module_t *ngx_modules[] = {
            ...
            &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
        };

From write_filter to not_modified_filter, the order in which modules are executed is reversed. T hat is, the earliest execution not_modified_filter, and then the modules execute in turn. In general, the config file of a third-party filtering module appends the module name to variable HTTP_AUX_FILTER_MODULES, at which point the module can only be added between the copy_filter and headers_filter modules.

How do Nginx execute individual filter modules in order? I t takes a very subtle approach, that is, through local global variables. For example, in each filter module, it is likely that you will see the following code:

        static ngx_http_output_header_filter_pt  ngx_http_next_header_filter;
        static ngx_http_output_body_filter_pt    ngx_http_next_body_filter;

        ...

        ngx_http_next_header_filter = ngx_http_top_header_filter;
        ngx_http_top_header_filter = ngx_http_example_header_filter;

        ngx_http_next_body_filter = ngx_http_top_body_filter;
        ngx_http_top_body_filter = ngx_http_example_body_filter;

ngx_http_top_header_filter is a global variable. W hen a filter module is compiled, it is assigned to the handler of the current filter module. T he ngx_http_next_header_filter is a local global variable that holds the handler of the previous filter module before compilation. So overall, it's like a one-way list of global variables.

Each module wants to execute the next filter function, just call the ngx_http_next_header_filter this local variable. A nd the entry point of the entire filter module chain needs to ngx_http_top_header_filter this global variable. ngx_http_top_body_filter behavior is similar to header fitler.

The response header and response body filter functions are executed in the following order:

Introduction to the Nginx filter module

This diagram represents only the order in which execution is performed between head_filter and body_filter, and there may be other execution code between the header_filter and body_filter handler and between the body_filter handler.

The module is compiled

Nginx can easily join third-party filtering modules. In the directory of the filter module, you first need to include a config file, which reads as follows:

ngx_addon_name=ngx_http_example_filter_module
HTTP_AUX_FILTER_MODULES="$HTTP_AUX_FILTER_MODULES ngx_http_example_filter_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_example_filter_module.c"

The description adds ngx_http_example_filter_module module called a filter ngx_http_example_filter_module.c the source code of the module.

Note HTTP_AUX_FILTER_MODULES this variable is different from the general content processing module.