Hello World HTTP模

1.构造config

ngx_addon_name=ngx_http_mytest_module

HTTP_MODULES="$HTTP_MODULESngx_http_mytest_module"

NGX_ADDON_SRCS="$NGX_ADDON_SRCS$ngx_addon_dir/ngx_http_mytest_module.c"

2.在ngx_http_mytest_module.c中定义mytest模块

#include<ngx_config.h>

#include<ngx_core.h>

#include<ngx_http.h>

static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r);

static char* ngx_http_mytest(ngx_conf_t* cf, ngx_command_t* cmd, void* conf);

 //用于定义模块的配置文件參数

static ngx_command_t ngx_http_mytest_commonds[] = {

         { 

                   ngx_string("mytest"),

                   NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF |                      NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,

                   ngx_http_mytest,

                   NGX_HTTP_LOC_CONF_OFFSET,

                   0,

                   NULL

         }

         ngx_null_command

};

//指向ngx_http_module接口(HTTP框架要求)
static ngx_http_module ngx_http_mytest_module_ctx = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL }; //定义mytest模块
ngx_module_t ngx_http_mytest_module = { ngx_MODULE_v1, &ngx_http_mytest_module_ctx, ngx_http_mytest_commands, NGX_HTTP_MODULE, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NGX_MODULE_V1_PADDING }; <span style="font-family:Calibri;">//当在某个配置块中出现mytest配置项时。nginx会调用ngx_http_mytest方法
static char* ngx_http_mytest(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_mytest_handler; return NGX_CONF_OK; }
</span>
//在这里处理用户请求,并发送响应
static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t* r) { if(!(r->method & (NGX_HTTP_GET | NGX_HTTP_HEAD))){ return NGX_HTTP_NOT_ALLOWED; } ngx_int_t rc = ngx_http_discard_request_body(r); if(rc != NGX_OK){ return rc; } ngx_str_t type = ngx_string("text/plain"); ngx_str+t response = ngx_string("Hello World!"); r->headers_out.status= NGX_HTTP_OK; r->headers_out.content_length_n = response.len; r->headerS_out.content_type = type; rc = ngx_http_send_header(r); if(rc == NGX_ERROR || rc->NGX_OK || r->header_only){ return rc; } ngx_buf_t* b; b = ngx_create_temp_buf(r->pool, response.len); if(b == NULL){ return NGX_HTTP_INTERNAL_SERVER_ERROR; } ngx_memcpy(b->pos, response.data, response.len); b->last = b->pos + response.len; b->last_buf = 1; ngx_chain_t out; out.buf = b; out.next = NULL; return ngx_http_output_filter(r, &out); }

3.在ngx.conf中http里面默认的server中增加例如以下配置

location /test{

mytest;

}

4.编译安装nginx

./configure --add-module=/home/chen123/nginx/exp2 黄色区域为config和ngx_http_mytest_module.c的安装文件夹)

make

sudo make install

5.启动nginx

sudo /usr/local/nginx/sbin/nginx

6.显示结果例如以下:

版权声明:本文博主原创文章,博客,未经同意不得转载。

Nginx得知——Hello World模的更多相关文章

  1. Nginx得知——流程模型(worker流程)

    流程模型 worker流程 master进程模型核心函数ngx_master_process_cycle()中调用了创建子进程函数ngx_start_worker_processes(),该函数源代码 ...

  2. Apache与Nginx的优缺点比较

    1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下ngin ...

  3. web服务器之nginx与apache

    最近准备架设php的web服务器,以下内容可供参考. 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞 ...

  4. nginx&apache比较

    1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apache 则是阻塞型的,在高并发下nginx ...

  5. 转:Apache与Nginx的优缺点比较

    Apache与Nginx的优缺点比较 http://weilei0528.blog.163.com/blog/static/206807046201321810834431/ 1.nginx相对于ap ...

  6. nginx和apache的优缺点比较

    简单的说apache httpd和nginx都是web服务器,但两者适应的场景不同,也就是两者专注于解决不同的问题.apache httpd:稳定.对动态请求处理强,但同时高并发时性能较弱,耗费资源多 ...

  7. Apache与Nginx优缺点比较

    本文来源:收集.整理自互联网 1.nginx相对于apache的优点:  轻量级,同样起web 服务,比apache 占用更少的内存及资源  抗并发,nginx 处理请求是异步非阻塞的,而apache ...

  8. nginx+tomcat负载均衡策略

    測试环境均为本地,測试软件为: nginx-1.6.0,apache-tomcat-7.0.42-1.apache-tomcat-7.0.42-2.apache-tomcat-7.0.42-3 利用n ...

  9. Apache和Nginx的对比

    Apache与Nginx的优缺点比较 1.nginx相对于apache的优点: 轻量级,同样起web 服务,比apache 占用更少的内存及资源 抗并发,nginx 处理请求是异步非阻塞的,而apac ...

随机推荐

  1. Android中&lt;meta-data&gt;的使用

    在AndroidManifest.xml中.<meta-data>元素能够作为子元素,被包括在<activity>.<application> .<servi ...

  2. 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers(转)

    接我前面一篇文章关于RestAssured测试Restful web service的, RestAssured还有一个功能, 使用RestAssuredMockMvc 单元测试你的Spring MV ...

  3. uva 1434 - YAPTCHA(数论)

    题目链接:uva 1434 - YAPTCHA 题目大意:给定n和k,求题目中给定的式子S(n). 解题思路:威尔逊定理,x为素数时有,((x−1)!+1)%x==0,所以对于本题.假设3*k+7为素 ...

  4. tolua 有些功能可以用(经过测试)

    tolua 提供几个 C++ 与 Lua 进行数据交换的工具函数. ~~ tolua.type 返回一个 C++ 对象的类型描写叙述字符串. local node = display.newNode( ...

  5. webapi Task

    webapi+Task并行请求不同接口实例 标题的名称定义不知道是否准确,不过我想表达的意思就是使用Task特性来同时请求多个不同的接口,然后合并数据:我想这种场景的开发对于对接过其他公司接口的人不会 ...

  6. asp.net中用FileStream类实现下载文件功能,自定义下载路径,像IE下载一样

    方法一: //这个值可以从配置文件读取,或者放个textbox让用户填 string path = "你的路径";FileStream outputStream = new Fil ...

  7. WASP_百度百科

    WASP_百度百科 WASP

  8. 开放源代码的微微信.NET 0.8 版公布了

    微微信.NET 0.8 版公布了     A.源代码应用范围:         未认证的和经过认证的微信订阅号.微信服务号均可使用,本源代码的每个模块都提供全然的 ASP.NET C#源代码,绝对不含 ...

  9. android maven eclipse里面新建mavenprojectThe desired archetype does not exist

    这个问题头疼死我了 又一次配置下你看我的教程 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvY2hlbmFpbmkxMTk=/font/5a6L5L2T/f ...

  10. pygame系列_mouse鼠标事件

    pygame.mouse提供了一些方法获取鼠标设备当前的状态 ''' pygame.mouse.get_pressed - get the state of the mouse buttons get ...