Nginx version of "HelloWorld"

Source: Internet
Author: User

Nginx Module Overview

Nginx modules cannot be added dynamically like Apache, and all modules are pre-compiled into Nginx binaries.
The Nginx module has three roles:
(1) Handlers (processing module) – Used to process HTTP requests and output content.
(2) Filters (Filter module) – Used to filter the contents of the Headler output.
(3) load-balancers (Load balancer module) – When multiple servers are available, select a back-end server and forward the HTTP request to the server.

Hello World Module Authoring and installation

(1) Execute the following command to write our Nginx module within the directory:
mkdir-p/opt/nginx_hello_world
Cd/opt/nginx_hello_world
(2) The configuration file required to create the Nginx module (named config) is started
Vim/opt/nginx_hello_world
Then enter the following to save and exit:

ngx_sddon_name=nginx_http_hello_world_moduleHTTP_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 Nginx module C program file (in the 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);Staticngx_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};StaticU_char ngx_hello_world[]="Hello World";Staticngx_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};Staticngx_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)); O Ut. 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);returnNgx_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;returnNGX_CONF_OK;}

(4) Refer to my Nginx installation that a Nginx installation blog in this step slightly different
**./configure–prefix=/usr/local/nginx–add-module=/opt/nginx_hello_world
Make&&make install**
(5) Configure nginx.conf (/usr/local/nginx/conf/nginx.conf) to add the following in the server section:
**location =/hello{
Hello_world;
}**
(6) Start Nginx, (Nginx start), with a browser access to Http://localhost/hello, you can see the written nginx Hello World module output text "Hello World".

Next Write Code Analysis

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Nginx version of "HelloWorld"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.