Nginx source code analysis 1 -------- compiling Nginx extension module, 1 -------- nginx
Recently, I applied to pass the CSDN quasi-expert. In order to successfully upgrade to an certified expert six months later and be worthy of this medal, I feel that I cannot relax the blog's update frequency and quality. C/C ++ development in windows is a relatively large part of my work. For Linux server development and so on, it is also a great place to go home. Recently, I have been studying distributed storage, it involves Nginx extension development and configuration. I checked a lot of information and found that Nginx configuration is quite easy to deploy, but the source code is really obscure. We often think about it, therefore, it is not easy to expand and develop the Nginx module. I will start from the Nginx extension module and analyze the Nginx source code step by step,
~~ It's too violent.
Based on some open-source code implementations, we found that the Nginx code seems to be rational. In Nginx, we can write about three types of extensions, I only want to write a simple http extension ~ But this is enough to lead the entry.
1. http extension
As shown in, the extensions are included in Nginx. Below I plan to copy these extensions, which are similar to God.
2. Filter
What this guy can do is big ~ We can mount our Filter to the Nginx Filter to Filter data streams. We will not explain the specific application here.
3,Server Load balancer
Have you ever complained that Nginx's polling mechanism IP_HASH and server weight methods cannot achieve the expected results? Since Nginx cannot meet your needs, you need to meet your own needs, you can expand your own load
.
Needless to say, it's useless ~~~~ Check the code first.
The English Ubuntu sogou has a problem. Qt cannot provide Chinese characters, and the comments are written in English. Not standard English either
/*************************************** * ************ Nginx Extension Module By YDW ********************* *********************************** // Definition A bunch of macros and differentiate different operating systems based on pre-compilation options to load different configuration files # include
After coding, let's compile and compile the configuration file config to copy an open-source extension format. Just modify it.
ngx_addon_name=ngx_http_hello_ydw_moduleHTTP_MODULES="$HTTP_MODULES ngx_http_hello_ydw_module"NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_ydw_module.c"
The code configuration file has been written. Now we have created a folder named ngx_http_hello_ydw_module to store the config and ngx_http_hello_ydw_module.c. The structure is as follows. Our module has been compiled ~~~~~
Compile and install nginx. Nginx_1.62 is used. Download nginx source code and decompress it.
Sequential
./Configure -- add-module = ../nginx_http_ydw_module/-- prefix =/usr/local/nginx2 -- conf-path =/etc/nginx2/nginx. conf
Make & make install
The final ngx_http_hello_ydw_module.o of the following structure has been compiled, which indicates that our extension compilation is successful.
Wait for make install to end the installation.
After compiling and installing nginx, first modify the Nginx configuration file nginx. conf.
Add the previous line
Location/hello_ydw
{
# Handle the hello_ydw command, which we declared in the code.
Hello_ydw ExtensionSuccessful;
}
Start Nginx now
/Usr/local/nginx2/sbin/nginx
Now, let's test the results. Now, it's successful. This step means you can write your own Nginx Extension Program.