|
Nginx Module Overview
Nginx modules that can not be dynamically added as Apache, all modules must be pre-compiled into Nginx binary executable file.
Nginx modules have three roles:
(1) Handlers (processing module) - for handling HTTP requests and output.
(2) Filters (filter modules) - for filtering content Headler output.
(3) Load-balancers (load balancing module) - When more than one server available, select a back-end server and forwards the request to the HTTP server.
hello world module prepared and Installation
(1) Execute the following commands to prepare our Nginx modules in that directory:
mkdir -p / opt / nginx_hello_world
cd / opt / nginx_hello_world
(2) needed to start creating nginx module configuration file (named config)
vim / opt / nginx_hello_world
Then enter the following to save and exit:
ngx_sddon_name = nginx_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 a Nginx module c program file (format "ngx_http_ module name _module.c", in this case: ngx_http_hello_world_module.c)
vim /opt/nginx_hello_world/ngx_http_hello_world_module.c
#include < ngx_config.h>
#include < ngx_core.h>
#include < ngx_http.h>
static char * ngx_http_hello_world (ngx_conf_t * cf, ngx_command_t * cmd, void * conf);
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
};
static u_char ngx_hello_world [] = "hello world";
static ngx_http_module_t ngx_http_hello_world_module_ctx = {
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL
};
ngx_module_t ngx_http_hello_world_module = {
NGX_MODULE_V1,
& Ngx_http_hello_world_module_ctx,
ngx_http_hello_world_commands,
NGX_HTTP_MODULE,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NULL,
NGX_MODULE_V1_PADDING
};
static ngx_int_t ngx_http_hello_world_handler (ngx_http_request_t * r)
{
ngx_buf_t * b;
ngx_chain_t out;
r-> headers_out.content_type.len = sizeof ( "text / plain") - 1;
r-> headers_out.content_type.data = (u_char *) "text / plain";
b = ngx_pcalloc (r-> pool, sizeof (ngx_buf_t));
out.buf = b;
out.next = NULL;
b-> pos = ngx_hello_world;
b-> last = ngx_hello_world + sizeof (ngx_hello_world);
b-> memory = 1;
b-> last_buf = 1;
r-> headers_out.status = NGX_HTTP_OK;
r-> headers_out.content_length_n = sizeof (ngx_hello_world);
ngx_http_send_header (r);
return ngx_http_output_filter (r, & out);
}
static char * ngx_http_hello_world (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_hello_world_handler;
return NGX_CONF_OK;
}
(4) that refer to my nginx install Nginx install a blog in this step is slightly different
**. / Configure -prefix = / usr / local / nginx -add-module = / opt / nginx_hello_world
make && make install **
(5) Configuration nginx.conf (/usr/local/nginx/conf/nginx.conf), part of the increase in the server the following:
** Location = / hello {
hello_world;
} **
(6) start Nginx, (Nginx start), the browser to http: // localhost / hello, you can see written Nginx Hello World module outputs the text "hello world". |
|
|
|