根据网上的代码开始学习,如下:
网上代码为:http://www.codinglabs.org/html/intro-of-nginx-module-development.html
后来我想自己输出数据:修改代码为:
ngx_http_echo_handler(ngx_http_request_t *r)
{
ngx_int_t rc;
ngx_buf_t *b;
ngx_chain_t out;
ngx_http_echo_loc_conf_t *elcf;
elcf = ngx_http_get_module_loc_conf(r, ngx_http_echo_module);
if(!(r->method & (NGX_HTTP_HEAD|NGX_HTTP_GET|NGX_HTTP_POST)))
{
return NGX_HTTP_NOT_ALLOWED;
}
r->headers_out.content_type.len = sizeof("text/html") - 1;
r->headers_out.content_type.data = (u_char *) "text/html";
r->headers_out.status = NGX_HTTP_OK;
r->headers_out.content_length_n = elcf->ed.len;
if(r->method == NGX_HTTP_HEAD)
{
rc = ngx_http_send_header(r);
if(rc != NGX_OK)
{
return rc;
}
}
b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));
if(b == NULL)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0, "Failed to allocate response buffer.");
return NGX_HTTP_INTERNAL_SERVER_ERROR;
}
out.buf = b;
out.next = NULL;
// b->pos = elcf->ed.data;
b->pos = (u_char *) "gy";
b->last = elcf->ed.data + (elcf->ed.len);
//b->last = b->pos + sizeof("ffgy") - 1;
//ngx_buf_t *c;
// c = ngx_calloc_buf(r->pool);
//if (c == NULL) {
// return NGX_ERROR;
//}
//c->pos = (u_char *) "";
//c->last = c->pos + sizeof("") - 1;
// ngx_chain_t added_link;
// added_link.buf = c;
//added_link.next = NULL;
//out.next = added_link;
b->memory = 1;
b->last_buf = 1;
//added_link->buf->last_buf = 1;
rc = ngx_http_send_header(r);
if(rc != NGX_OK)
{
return rc;
}
return ngx_http_output_filter(r, &out);
}
输出为gy。
每次修改一次源c文件的时候都要 config和make&make install 一次nginx源码,生成新的nginx