读书人

nginx自定义模块获取远路ip

发布时间: 2012-11-16 14:12:15 作者: rapoo

nginx自定义模块获取远程ip

#include <ngx_config.h>#include <ngx_core.h>#include <ngx_http.h>//http://www.162cm.com/p/ngx_ext.html// http://blog.csdn.net/marcky/article/details/6539387// http://zhwen.org/xlog/?p=554static char* ngx_echo_readconf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);static void* ngx_echo_create_loc_conf(ngx_conf_t *cf);static char* ngx_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);typedef struct {    ngx_str_t ecdata; ///echo模块只有一个参数 比如 echo "hello"    ngx_flag_t           enable;} ngx_echo_loc_conf_t;static ngx_command_t  ngx_echo_commands[] = {    { ngx_string("echo"), ////命令名字      NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, ////代表是local配置,带一个参数      ngx_echo_readconf, ////组装模块配置结构      NGX_HTTP_LOC_CONF_OFFSET, ////上面的组装模块配置获取完参数后存放到哪里?使用这个和下面的offset参数来进行定位      offsetof(ngx_echo_loc_conf_t, ecdata),      NULL },      ngx_null_command};static ngx_http_module_t  ngx_echo_module_ctx = {    NULL,                          /* preconfiguration */    NULL,           /* postconfiguration */    NULL,                          /* create main configuration */    NULL,                          /* init main configuration */    NULL,                          /* create server configuration */    NULL,                          /* merge server configuration */    ngx_echo_create_loc_conf,  /* create location configuration */    ngx_echo_merge_loc_conf /* merge location configuration */};ngx_module_t  ngx_module_echo = {    NGX_MODULE_V1,    &ngx_echo_module_ctx, /* module context */    ngx_echo_commands,   /* module directives */    NGX_HTTP_MODULE,               /* module type */    NULL,                          /* init master */    NULL,                          /* init module */    NULL,                          /* init process */    NULL,                          /* init thread */    NULL,                          /* exit thread */    NULL,                          /* exit process */    NULL,                          /* exit master */    NGX_MODULE_V1_PADDING};static ngx_int_tngx_echo_handler(ngx_http_request_t *r){    ngx_int_t    rc;    ngx_buf_t   *b;    ngx_chain_t  out;    //填充HTTP头    /* set the ‘Content-type’ header */    r->headers_out.content_type.len = sizeof("text/html") - 1;    r->headers_out.content_type.data = (u_char *) "text/html";    //分配输出内存空间    /* allocate a buffer */    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t));    if (b == NULL) {    return NGX_HTTP_INTERNAL_SERVER_ERROR;    }    //输出缓存附加到输出链表上    /* attach buffer to the buffer chain */    out.buf = b;    out.next = NULL;    ngx_connection_local_sockaddr(r->connection, NULL, 0);    struct sockaddr_in *sin = NULL;    /*type of r is ngx_http_request_t* */    sin = (struct sockaddr_in *)(r->connection->local_sockaddr);    u_char *addr_string = (u_char *)inet_ntoa(sin->sin_addr);    //填写输出缓存内容    /* adjust the pointers of the buffer */    b->pos = addr_string;  /* the begin offset of the buffer */    b->last = addr_string + sizeof(addr_string) - 1; /* the end offset of the buffer */    b->memory = 1;    /* this buffer is in memory */    b->last_buf = 1;  /* this is the last buffer in the buffer chain */    /* 设置http返回码 */    r->headers_out.status = NGX_HTTP_OK;    r->headers_out.content_length_n = sizeof(addr_string) - 1;    //发送HTTP报头    /* send the headers of your response */    rc = ngx_http_send_header(r);    if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {    return rc;    }    //输出内容    /* send the buffer chain of your response */    return ngx_http_output_filter(r, &out);}static char *ngx_echo_readconf(ngx_conf_t *cf, ngx_command_t *cmd, void *conf){    printf("called:ngx_echo_readconf\n");    ngx_http_core_loc_conf_t  *clcf;    clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);    clcf->handler = ngx_echo_handler;    ngx_conf_set_str_slot(cf,cmd,conf);    return NGX_CONF_OK;}static void *ngx_echo_create_loc_conf(ngx_conf_t *cf){    printf("called:ngx_echo_create_loc_conf\n");    ngx_echo_loc_conf_t  *conf;    conf = ngx_pcalloc(cf->pool, sizeof(ngx_echo_loc_conf_t));    if (conf == NULL) {        return NGX_CONF_ERROR;    }    conf->ecdata.len=0;    conf->ecdata.data=NULL;    conf->enable = NGX_CONF_UNSET;    return conf;}static char *ngx_echo_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child){    printf("called:ngx_echo_merge_loc_conf\n");    ngx_echo_loc_conf_t *prev = parent;    ngx_echo_loc_conf_t *conf = child;    ngx_conf_merge_str_value(conf->ecdata, prev->ecdata, 10);    ngx_conf_merge_value(conf->enable, prev->enable, 0);/** *     if(conf->enable) *             ngx_echo_init(conf); *                     */    return NGX_CONF_OK;    return NGX_CONF_OK;}
?

读书人网 >软件架构设计

热点推荐