May 23, 2021 Nginx Getting started
After the function of the module is developed, the use of the module also needs to be compiled to be able to execute, let's look at the module compilation and use.
For developing a module, we need to organize the C code of this module into a directory and write a config file. T he content of this config file is to tell Nginx's compilation script how to compile it. Let's take a look at the contents of hello handler module's config file, and then explain.
ngx_addon_name=ngx_http_hello_module
HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module"
NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c"
In fact, the file is very simple, almost no need to do anything to explain. E veryone understands it at a glance. The only thing to note is that if the implementation of this module has multiple source files, then you can write it in NGX_ADDON_SRCS variable in turn.
Nginx does not provide a separate compilation tool for module compilation, as apache does, and can compile the code of a module separately without apache source code. N ginx must go to Nginx's source code directory and compile them using the parameters of the configuration instruction. Here's a look at hello module's configuration instructions:
./configure --prefix=/usr/local/nginx-1.3.1 --add-module=/home/jizhao/open_source/book_module
The code and config files for this sample module that I wrote
/home/jizhao/open_source/book_module
of the sample module.
So everything is clear, and there's nothing to say.
Using a module needs to be done according to the configuration instructions defined by the module. F or example, our simple hello handler module is very simple to use. In the configuration file of my test server, the following configuration is added to the default server inside http:
location /test {
hello_string jizhao;
hello_counter on;
}
When we visit this address,
lynx http://127.0.0.1/test
can see the results returned.
jizhao Visited Times:1
Of course you visit many times, this number will increase.