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.cnginx
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=spa