Test,Nginx Hello World Module

1 ngx_addon_name=ngx_http_mytest_module
2 HTTP_MODULES="$HTTP_MODULES ngx_http_mytest_module"
3 NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_mytest_module.c"

ngx_http_mytest_module.c

 

 1 #include <ngx_core.h>
 2 #include <ngx_http.h>
 3 #include <nginx.h>
 4 
 5 
 6 static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
 7 static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
 8 
 9 static ngx_command_t ngx_http_mytest_commands[] = {
10     {ngx_string("mytest"),
11         NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
12         ngx_http_mytest,
13         NGX_HTTP_LOC_CONF_OFFSET,
14         0,
15         NULL},
16 
17     ngx_null_command
18 };
19 
20 static ngx_http_module_t ngx_http_mytest_module_ctx = {
21 NULL,
22 NULL,
23 NULL,
24 NULL,
25 NULL,
26 NULL,
27 NULL,
28 NULL
29 };
30 
31 ngx_module_t ngx_http_mytest_module = {
32 NGX_MODULE_V1,
33 &ngx_http_mytest_module_ctx,
34 ngx_http_mytest_commands,
35 NGX_HTTP_MODULE,
36 NULL,
37 NULL,
38 NULL,
39 NULL,
40 NULL,
41 NULL,
42 NULL,
43 NGX_MODULE_V1_PADDING
44 };
45 
46 
47 static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
48 {
49     ngx_http_core_loc_conf_t *clcf;
50     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);
51     clcf->handler = ngx_http_mytest_handler;
52 
53     return NGX_CONF_OK;
54 }
55 
56 static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r)
57 {
58     if(!(r->method & NGX_HTTP_GET))
59         return NGX_HTTP_NOT_ALLOWED;
60 
61     ngx_int_t rc = ngx_http_discard_request_body(r);
62     if(rc != NGX_OK)
63         return rc;
64 
65     ngx_str_t type = ngx_string("text/plain");
66     ngx_str_t response = ngx_string("Hello World!");
67     r->headers_out.status = NGX_HTTP_OK;
68     r->headers_out.content_length_n = response.len;
69     r->headers_out.content_type = type;
70 
71     rc = ngx_http_send_header(r);
72     if(rc == NGX_ERROR || rc > NGX_OK || r->header_only)
73         return rc;
74 
75     ngx_buf_t *b;
76     b = ngx_create_temp_buf(r->pool, response.len);
77     if(b == NULL)
78         return NGX_HTTP_INTERNAL_SERVER_ERROR;
79 
80     ngx_memcpy(b->pos, response.data, response.len);
81     b->last = b->pos + response.len;
82     b->last_buf = 1;
83 
84     ngx_chain_t out;
85     out.buf = b;
86     out.next = NULL;
87 
88     return ngx_http_output_filter(r, &out);
89 }

 

 

./configure --add-module=

posted on 2013-10-24 20:49  Zirconi 阅读( ...) 评论( ...) 编辑 收藏

转载于:https://www.cnblogs.com/Zirx/p/3386781.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值