The 1.Nginx configuration file consists mainly of:
The instructions in this section of main (global configuration) will affect all other parts.
Server (Virtual host configuration) This part of the instruction is primarily used to specify the virtual host domain name, IP, and port.
Upstream (mainly reverse proxy, load balancer related configuration) This part of the instruction is used to set up the reverse proxy and backend services
Load balancing of the device.
Location (directory matching configuration) This part of the instruction is used to match the page position (for example, root directory "/", "/images", etc.
, etc.).
The location section inherits the instructions from the Server section, and the server part inherits the instructions from the main section. Upstream
Neither inheriting the instruction nor affecting the other parts. It has its own special instructions and does not need to be applied elsewhere.
2.Nginx module Overview.
Nginx modules can not be added dynamically like Apache, all the modules are pre-compiled into the nginx binary can be
Execute the file. Nginx module has three kinds of characters.
(1) Handlers (processing module)--for processing HTTP requests and outputting content.
(2) Filters (Filter module)--for filtering the contents of the handler output.
(3) Load-balancers (Load balancer module)-When you have more than one backend server to choose from, select one
The back-end server and forwards the HTTP request to the server.
Process flow for 3.Nginx modules:
When Nginx sends a file or requests to another server, it can use the handlers processing module to serve it: when Nginx is required
When you compress the output or add something to the server, you can use the Filters filter module. Nginx Core Module Main tube
The network layer and application layer protocols, as well as launching a series of candidate modules for specific applications.
The client sends an HTTP request to the Nginx server
|
Nginx selects a suitable processing module based on the location in the configuration file
|
Load Balancer Module Select a back-end server (reverse proxy case)
|
Processing module for processing and placing the output buffer on the first filter module
|
The first filter module is processed and output to a second filter module
|
And then the second filter module goes to the third filter module.
|
Nth Filter Module
|
Finally send the processing results to the client
4. Location of module execution
The module is equivalent to a hook, can be hung in the nginx location, at some point to perform certain functions.
(1) Before the service reads the configuration file.
(2) When reading every configuration instruction in the location and Server section or any other part.
(3) When Nginx initializes the configuration of the global section.
(4) When Nginx initializes the configuration of the host part (such as host/port).
(5) When Nginx merges the configuration of the global part with the configuration of the host part.
(6) When nginx initializes the matching location portion of the configuration.
(7) When Nginx merges its upper host configuration with the Location section configuration.
(8) When the Nginx master process (master) starts.
(9) When a new work process (worker) starts.
(10) When a worker process exits.
(11) When the main process exits.
(12) When processing an HTTP request.
(13) When the header of the HTTP reply is filtered.
(14) When filtering the main body of an HTTP reply.
(15) When selecting a back-end server.
(16) When the request is initialized to the backend server.
(17) When re-initializing the request to the backend server.
(18) When processing replies from the backend server.
(19) When you are finished interacting with the backend server.
5.Nginx Module Writing Example
(1) Execute the following command to create a directory in which our Nginx module will be written:
Mkdir-p/opt/nginx_hello_world
Cd/opt/nginx_hello_world
(2) Start creating the desired configuration file for Nginx module (name config)
Vi/opt/nginx_hello_world/config
Enter the following and save the exit.
Ngx_addon_name=ngx_http_hello_world_module
http_modules= "$HTTP _modules ngx_http_hello_world_module"
ngx_addon_srcs= "$NGX _addon_srcs $ngx _addon_dir/ngx_http_hello_world_module.c"
core_libs= "$CORE _libs-lpcre"
(3) Create the Nginx module C program file (the name format is "NGX_HTTP_ module name _module.c", in this example
, the file name is called ngx_http_hello_world_module.c)
Vi/opt/nginx_hello_world/ngx_http_hello_world_module.c
(4) Download the Nginx source package and compile the Hello World module into it. As shown below
wget ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/pcre-7.9.tar.gz
Tar zxvf pcre-7.9.tar.gz
CD pcre-7.9/
./configure
Make && make install
Cd.. /
wget http://sysoev.ru/nginx/nginx-0.8.14.tar.gz
Tar zxvf nginx-0.8.14.tar.gz
CD nginx-0.8.14/
./configure--prefix=/usr/local/nginx--add-module=/opt/nginx_hello_world
Make
Make install
(5) Configure nginx.conf, add the following in the server section:
Location =/hello {
Hello_world;
}
(6) Start Nginx, Access Http://localhost/hello with the browser, you can see the written nginx Hello
World module Output text "Hello World"
6.Hello World Module Analysis:
In the NGX_HTTP_HELLO_WORLD_MODULE.C code, the ngx_command_t function is used to define the command containing the module
Static array ngx_http_hello_world_commands, the code is as follows
Static ngx_command_t ngx_http_hello_world_commands[] =
{
{
Ngx_string ("Hello_world"),
Ngx_http_loc_conf| Ngx_conf_noargs,
Ngx_http_hello_world,
0,
0,
Null
},
Ngx_null_command
}
(1) in the array, the first parameter ngx_string ("Hello_world") is the instruction name string and cannot contain spaces,
The data type is ngx_str_t and is often used for string instantiation. The ngx_str_t struct consists of a string-containing
The data member and the Len member that represents the length of the string. Nginx uses this data type to hold the string.
(2) The second parameter is used to set which part of the configuration file location The instruction is used to be valid. The optional values are as follows, multiple
Option to "|" Separated:
The ngx_http_main_conf--directive appears to be legal in the global configuration section.
The ngx_http_srv_conf--directive appears to be legal in the server Host configuration section.
The ngx_http_loc_conf--directive appears to be legal in the Location configuration section.
The ngx_http_ups_conf--directive appears to be legal in the upstream configuration section.
(3) The third parameter, Ngx_http_hello_world, is a callback function.
(4) The fourth parameter is used to tell Nginx to put the value to be kept in the Global configuration section, the Server Host configuration section also
Is the Location configuration section (using Ngx_http_main_conf_offset,
Ngx_http_srv_conf_offset,ngx_http_loc_conf_offset)
(5) The fifth parameter is used to set where the value of the instruction is stored in the structure
(6) The six parameter is generally null.
After the six parameters are set, the array stops after reading the Ngx_null_command.
Nginx Learning Note six Nginx module development