Nginx version "helloworld"
Nginx module Overview
Nginx modules cannot be dynamically added as Apache does. All modules must be pre-compiled into Nginx binary executable files.
The Nginx module has three roles:
(1) Handlers (Processing Module)-used to process HTTP requests and output content.
(2) Filters-used to filter the content output by Headler.
(3) Load-balancers (Server Load balancer module)-When multiple servers are available for selection, select a backend server and forward HTTP requests to the server.
Writing and installing the hello world module
(1) execute the following command to write our Nginx module in this directory:
Mkdir-p/opt/nginx_hello_world
Cd/opt/nginx_hello_world
(2) start to create the configuration file (named config) required by the nginx module)
Vim/opt/nginx_hello_world
Then enter the following content 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 module c program file for Nginx (in the format of "ngx_http _ module name_module. c", in this example: 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) refer to my nginx installation blog, which is slightly different in this step.
**./Configure-prefix =/usr/local/nginx-add-module =/opt/nginx_hello_world
Make & make install **
(5) Configure nginx. conf (/usr/local/nginx/conf/nginx. conf) and add the following content to the server section:
** Location =/hello {
Hello_world;
}**
(6) Start Nginx (Nginx startup) and access http: // localhost/hello in a browser. Then you can see the text "Hello World" output by the Nginx hello world module ".
For more Nginx tutorials, see the following:
Deployment of Nginx + MySQL + PHP in CentOS 6.2
Build a WEB server using Nginx
Build a Web server based on Linux6.3 + Nginx1.2 + PHP5 + MySQL5.5
Performance Tuning for Nginx in CentOS 6.3
Configure Nginx to load the ngx_pagespeed module in CentOS 6.3
Install and configure Nginx + Pcre + php-fpm in CentOS 6.4
Nginx installation and configuration instructions
Nginx log filtering using ngx_log_if does not record specific logs
Nginx details: click here
Nginx: click here
This article permanently updates the link address: