详细资料:http://tengine.taobao.org/book/chapter_03.html
这里只简单的介绍怎么使用第三方模块。
1.编写第三方模块(也可以在网上下载一些第三方模块)
建立文件夹hello,里面有两个文件:
ngx_http_hello_module.c
1 #include <ngx_config.h> 2 #include <ngx_core.h> 3 #include <ngx_http.h> 4 5 6 typedef struct 7 { 8 ngx_str_t hello_string; 9 ngx_int_t hello_counter; 10 }ngx_http_hello_loc_conf_t; 11 12 static ngx_int_t ngx_http_hello_init(ngx_conf_t *cf); 13 14 static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf); 15 16 static char *ngx_http_hello_string(ngx_conf_t *cf, ngx_command_t *cmd, 17 void *conf); 18 static char *ngx_http_hello_counter(ngx_conf_t *cf, ngx_command_t *cmd, 19 void *conf); 20 21 static ngx_command_t ngx_http_hello_commands[] = { 22 { 23 ngx_string("hello_string"), 24 NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1, 25 ngx_http_hello_string, 26 NGX_HTTP_LOC_CONF_OFFSET, 27 offsetof(ngx_http_hello_loc_conf_t, hello_string), 28 NULL }, 29 30 { 31 ngx_string("hello_counter"), 32 NGX_HTTP_LOC_CONF|NGX_CONF_FLAG, 33 ngx_http_hello_counter, 34 NGX_HTTP_LOC_CONF_OFFSET, 35 offsetof(ngx_http_hello_loc_conf_t, hello_counter), 36 NULL }, 37 38 ngx_null_command 39 }; 40 41 42 /* 43 static u_char ngx_hello_default_string[] = "Default String: Hello, world!"; 44 */ 45 static int ngx_hello_visited_times = 0; 46 47 static ngx_http_module_t ngx_http_hello_module_ctx = { 48 NULL, /* preconfiguration */ 49 ngx_http_hello_init, /* postconfiguration */ 50 51 NULL, /* create main configuration */ 52 NULL, /* init main configuration */ 53 54 NULL, /* create server configuration */ 55 NULL, /* merge server configuration */ 56 57 ngx_http_hello_create_loc_conf, /* create location configuration */ 58 NULL /* merge location configuration */ 59 }; 60 61 62 ngx_module_t ngx_http_hello_module = { 63 NGX_MODULE_V1, 64 &ngx_http_hello_module_ctx, /* module context */ 65 ngx_http_hello_commands, /* module directives */ 66 NGX_HTTP_MODULE, /* module type */ 67 NULL, /* init master */ 68 NULL, /* init module */ 69 NULL, /* init process */ 70 NULL, /* init thread */ 71 NULL, /* exit thread */ 72 NULL, /* exit process */ 73 NULL, /* exit master */ 74 NGX_MODULE_V1_PADDING 75 }; 76 77 78 static ngx_int_t 79 ngx_http_hello_handler(ngx_http_request_t *r) 80 { 81 ngx_int_t rc; 82 ngx_buf_t *b; 83 ngx_chain_t out; 84 ngx_http_hello_loc_conf_t* my_conf; 85 u_char ngx_hello_string[1024] = {0}; 86 ngx_uint_t content_length = 0; 87 88 ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "ngx_http_hello_handler is called!"); 89 90 my_conf = ngx_http_get_module_loc_conf(r, ngx_http_hello_module); 91 if (my_conf->hello_string.len == 0 ) 92 { 93 ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "hello_string is empty!"); 94 return NGX_DECLINED; 95 } 96 97 98 if (my_conf->hello_counter == NGX_CONF_UNSET 99 || my_conf->hello_counter == 0) 100 { 101 ngx_sprintf(ngx_hello_string, "%s", my_conf->hello_string.data); 102 } 103 else 104 { 105 ngx_sprintf(ngx_hello_string, "%s Visited Times:%d", my_conf->hello_string.data, 106 ++ngx_hello_visited_times); 107 } 108 ngx_log_error(NGX_LOG_EMERG, r->connection->log, 0, "hello_string:%s", ngx_hello_string); 109 content_length = ngx_strlen(ngx_hello_string); 110 111 /* we response to ‘GET‘ and ‘HEAD‘ requests only */ 112 if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) { 113 return NGX_HTTP_NOT_ALLOWED; 114 } 115 116 /* discard request body, since we don‘t need it here */ 117 rc = ngx_http_discard_request_body(r); 118 119 if (rc != NGX_OK) { 120 return rc; 121 } 122 123 /* set the ‘Content-type‘ header */ 124 /* 125 *r->headers_out.content_type.len = sizeof("text/html") - 1; 126 *r->headers_out.content_type.data = (u_char *)"text/html"; 127 */ 128 ngx_str_set(&r->headers_out.content_type, "text/html"); 129 130 131 /* send the header only, if the request type is http ‘HEAD‘ */ 132 if (r->method == NGX_HTTP_HEAD) { 133 r->headers_out.status = NGX_HTTP_OK; 134 r->headers_out.content_length_n = content_length; 135 136 return ngx_http_send_header(r); 137 } 138 139 /* allocate a buffer for your response body */ 140 b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); 141 if (b == NULL) { 142 return NGX_HTTP_INTERNAL_SERVER_ERROR; 143 } 144 145 /* attach this buffer to the buffer chain */ 146 out.buf = b; 147 out.next = NULL; 148 149 /* adjust the pointers of the buffer */ 150 b->pos = ngx_hello_string; 151 b->last = ngx_hello_string + content_length; 152 b->memory = 1; /* this buffer is in memory */ 153 b->last_buf = 1; /* this is the last buffer in the buffer chain */ 154 155 /* set the status line */ 156 r->headers_out.status = NGX_HTTP_OK; 157 r->headers_out.content_length_n = content_length; 158 159 /* send the headers of your response */ 160 rc = ngx_http_send_header(r); 161 162 if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) { 163 return rc; 164 } 165 166 /* send the buffer chain of your response */ 167 return ngx_http_output_filter(r, &out); 168 } 169 170 static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf) 171 { 172 ngx_http_hello_loc_conf_t* local_conf = NULL; 173 local_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_loc_conf_t)); 174 if (local_conf == NULL) 175 { 176 return NULL; 177 } 178 179 ngx_str_null(&local_conf->hello_string); 180 local_conf->hello_counter = NGX_CONF_UNSET; 181 182 return local_conf; 183 } 184 185 /* 186 static char *ngx_http_hello_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child) 187 { 188 ngx_http_hello_loc_conf_t* prev = parent; 189 ngx_http_hello_loc_conf_t* conf = child; 190 191 ngx_conf_merge_str_value(conf->hello_string, prev->hello_string, ngx_hello_default_string); 192 ngx_conf_merge_value(conf->hello_counter, prev->hello_counter, 0); 193 194 return NGX_CONF_OK; 195 }*/ 196 197 static char * 198 ngx_http_hello_string(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) 199 { 200 201 ngx_http_hello_loc_conf_t* local_conf; 202 203 204 local_conf = conf; 205 char* rv = ngx_conf_set_str_slot(cf, cmd, conf); 206 207 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "hello_string:%s", local_conf->hello_string.data); 208 209 return rv; 210 } 211 212 213 static char *ngx_http_hello_counter(ngx_conf_t *cf, ngx_command_t *cmd, 214 void *conf) 215 { 216 ngx_http_hello_loc_conf_t* local_conf; 217 218 local_conf = conf; 219 220 char* rv = NULL; 221 222 rv = ngx_conf_set_flag_slot(cf, cmd, conf); 223 224 225 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "hello_counter:%d", local_conf->hello_counter); 226 return rv; 227 } 228 229 static ngx_int_t 230 ngx_http_hello_init(ngx_conf_t *cf) 231 { 232 ngx_http_handler_pt *h; 233 ngx_http_core_main_conf_t *cmcf; 234 235 cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module); 236 237 h = ngx_array_push(&cmcf->phases[NGX_HTTP_CONTENT_PHASE].handlers); 238 if (h == NULL) { 239 return NGX_ERROR; 240 } 241 242 *h = ngx_http_hello_handler; 243 244 return NGX_OK; 245 }
config
1 ngx_addon_name=ngx_http_hello_module 2 HTTP_MODULES="$HTTP_MODULES ngx_http_hello_module" 3 NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_hello_module.c"
2.编译第三方模块(nginx源码目录下)
在未安装nginx的情况下安装第三方模块
# ./configure --prefix=/usr/local/nginx --add-module=/第三方模块目录
在已安装nginx情况下安装第三方模块
# ./configure --prefix=/usr/local/nginx --add-module=/第三方模块目录 # make # /usr/local/nginx/sbin/nginx -s stop # cp objs/nginx /usr/local/nginx/sbin/nginx # /usr/local/nginx/sbin/nginx
3.使用第三方模块
使用一个模块需要根据这个模块定义的配置指令来做。比如我们这个简单的hello handler module的时候就很简单。
在nginx.conf文件中在http里面的默认的server里面加入如下配置
location /test {
hello_string somebody;
hello_counter on;
}
当我们访问这个地址时:http://127.0.0.1/test时,就可以看到返回的结果
原文:http://www.cnblogs.com/runnyu/p/4871866.html