When you access Sina, some webpages often return blank (but not "this page cannot be displayed"). You can see from the browser information that the server returns a 1 × 1 blank GIF image.
This is actually implemented by nginx. nginx has a module named empty GIF, which is responsible for this work.
Since this module is relatively simple, let's start with it and look at the nginx module implementation.
Module Registration
Empty GIF has only one module File-ngx_http_empty_gif_module.c
This file is relatively simple. Three variables are defined and initialized at the beginning.
Static ngx_command_t ngx_http_empty_gif_commands [] = {...};
Static ngx_http_module_t ngx_http_empty_gif_module_ctx = {...};
Ngx_module_t ngx_http_empty_gif_module = {...};
Only the ngx_http_empty_gif_module is non-static,
The other two variables can be accessed by them.
However, if you continue to view the nginx source code, you will find that there is no reference to ngx_http_empty_gif_module elsewhere,
How does this module register and apply it?
If you are familiar with Apache code, you will find that this is very similar to the module mechanism of Apache 2.0-each module corresponds to a module main structure variable, other contents of this module can be accessed through this main structure variable. All functions of this module are also stored in these structure variables using function pointers.
In addition, Apache does not reference the module main structure variable from any other code. This is because the module is not required and does not exist in a specific compilation version. Therefore, whether a module is valid is not determined by code, but by compiling options.
In the auto directory of nginx code, there is a file named sources. The value of the M4 macro variable http_modules varies depending on the Compilation option (configure parameter:
If the empty GIF module is specified (which is used by default), the value of the M4 macro variable http_modules may be as follows:
Http_modules = "ngx_http_module/
Ngx_http_core_module/
Ngx_http_log_module/
Ngx_http_upstream_module/
Ngx_http_empty_gif_module"
Note: The ngx_http_empty_gif_module string corresponds to the module main structure variable name in the ngx_http_empty_gif_module.c file.
After the configure is compiled, a file named ngx_modules.c is generated under the objs directory. The content of this file is as follows:
# Include <ngx_config.h>
# Include <ngx_core.h>
Extern ngx_module_t ngx_core_module;
Extern ngx_module_t ngx_errlog_module;
Extern ngx_module_t ngx_conf_module;
...
Extern ngx_module_t ngx_http_empty_gif_module;
...
Ngx_module_t * ngx_modules [] = {
& Ngx_core_module,
& Ngx_errlog_module,
& Ngx_conf_module,
...
& Ngx_http_empty_gif_module,
...
Null
};
A reference to the ngx_http_empty_gif_module variable is generated and placed in the ngx_modules table,
Related functions can be used for access.
In this way, the registration process of the empty GIF module is completed during compilation.
Module initialization and Application
Initialization is generally based on the content of the configuration file, but it is different from the general practice of writing programs-nginx does not process all the configurations in a unified place, instead, each module is responsible for processing its own configuration items. If this module is not compiled, the corresponding configuration items cannot be processed. This is another similarity with Apache.
Nginx uses the ngx_command_t structure to describe the configuration items and processing functions of a module.
Take the empty GIF module as an example:
Static ngx_command_t ngx_http_empty_gif_commands [] = {
{Ngx_string ("empty_gif "),
Ngx_http_loc_conf | ngx_conf_noargs,
Ngx_http_empty_gif,
0,
0,
Null },
Ngx_null_command
};
The above definition indicates:
1. The empty GIF module only processes one configuration item-"empty_gif"
2. This configuration is a location-related configuration (ngx_http_loc_conf ),
That is, it takes effect only when processing a URL subset, such as/test _ [0-9] *. GIF.
The actual configuration file may be as follows:
Location ~ /Test_00000-92.16.gif {
Empty_gif;
}
3. This configuration item does not contain parameters (ngx_conf_noargs)
4. The configuration handler is ngx_http_empty_gif.
The implementation of the ngx_http_empty_gif function is simple:
Static char *
Ngx_http_empty_gif (ngx_conf_t * Cf, ngx_command_t * cmd, void * conf)
{
Ngx_http_core_loc_conf_t * clcf;
Clcf = ngx_http_conf_get_module_loc_conf (CF, ngx_http_core_module );
Clcf-> handler = ngx_http_empty_gif_handler;
Return ngx_conf_ OK;
}
Ngx_http_conf_get_module_loc_conf is a macro used to obtain the ngx_http_core_module corresponding to the location-related configuration table Cf. After the configuration is obtained, the empty GIF module mounts its processing function to the handler corresponding to the ngx_http_core_module.
In this way, if nginx finds that its URL matches the location of the empty GIF when processing the HTTP request,
For example, URL (/test_1.gif) matches location (/test_00000-92.16.gif ),
Ngx_http_empty_gif is used as the processing function. This function writes a 1 × 1 blank GIF image to the browser.