Nginx version "helloworld"

Source: Internet
Author: User
: This article mainly introduces the Nginx version "helloworld". if you are interested in the PHP Tutorial, refer to it. 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_sddHTTP_MODULES="$HTTP_MODULES ngx_http_hello_world_module"NGX_ADD>"$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 
  
   #include
   
    #include
    
     staticchar *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);}staticchar *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 ".

Write code analysis in the next article

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above introduces the Nginx version "helloworld", including some content, and hope to be helpful to friends who are interested in the PHP Tutorial.

Related Article

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.