lig 发表于 2018-11-12 11:04:56

nginx模块开发入门

#include   
#include
  
#include
  

  

  
typedef struct
  
{
  
      ngx_str_t hello_string;
  
}ngx_http_hello_loc_conf_t;
  

  
static ngx_int_t ngx_http_hello_init(ngx_conf_t *cf);
  

  
static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf);
  

  
static char *ngx_http_hello_string(ngx_conf_t *cf, ngx_command_t *cmd,
  
      void *conf);
  

  

  
static ngx_command_t ngx_http_hello_commands[] = {
  
   {
  
      ngx_string("hello_string"),
  
      NGX_HTTP_LOC_CONF|NGX_CONF_NOARGS|NGX_CONF_TAKE1,
  
      ngx_http_hello_string,
  
      NGX_HTTP_LOC_CONF_OFFSET,
  
      offsetof(ngx_http_hello_loc_conf_t, hello_string),
  
      NULL
  
    },
  
    ngx_null_command
  
};
  

  

  
static ngx_http_module_t ngx_http_hello_module_ctx = {
  
      NULL,                        /* preconfiguration */
  
      ngx_http_hello_init,         /* postconfiguration */
  

  
      NULL,                        /* create main configuration */
  
      NULL,                        /* init main configuration */
  

  
      NULL,                        /* create server configuration */
  
      NULL,                        /* merge server configuration */
  

  
      ngx_http_hello_create_loc_conf, /* create location configuration */
  
      NULL                            /* merge location configuration */
  
};
  

  

  
ngx_module_t ngx_http_hello_module = {
  
      NGX_MODULE_V1,
  
      &ngx_http_hello_module_ctx,    /* module context */
  
      ngx_http_hello_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_t
  
ngx_http_hello_handler(ngx_http_request_t *r)
  
{
  
      ngx_http_discard_request_body(r);
  

  
      ngx_str_t response = ngx_string("Hello world");
  

  
      ngx_buf_t* b;
  
      b = ngx_create_temp_buf(r->pool, response.len);
  
      ngx_memcpy(b->pos, response.data, response.len);
  
      b->last = b->pos + response.len;
  
      b->last_buf = 1;
  
      r->headers_out.status = NGX_HTTP_OK;
  
      r->headers_out.content_length_n = response.len;
  
      ngx_http_send_header(r);
  

  
      ngx_chain_t out;
  
      out.buf = b;
  
      out.next = NULL;
  

  
      return ngx_http_output_filter(r, &out);
  
}
  

  

  
static void *ngx_http_hello_create_loc_conf(ngx_conf_t *cf)
  
{
  
      ngx_http_hello_loc_conf_t* local_conf = NULL;
  
      local_conf = ngx_pcalloc(cf->pool, sizeof(ngx_http_hello_loc_conf_t));
  
      if (local_conf == NULL)
  
      {
  
                return NULL;
  
      }
  

  
      ngx_str_null(&local_conf->hello_string);
  

  
      return local_conf;
  
}
  

  
static char *
  
ngx_http_hello_string(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  
{
  
      ngx_http_hello_loc_conf_t* local_conf;
  

  
      local_conf = conf;
  
      char* rv = ngx_conf_set_str_slot(cf, cmd, conf);
  

  
      ngx_conf_log_error(NGX_LOG_EMERG, cf, 0, "hello_string:%s", local_conf->hello_string.data);
  

  
      return rv;
  
}
  

  
static ngx_int_t
  
ngx_http_hello_init(ngx_conf_t *cf)
  
{
  
      ngx_http_handler_pt      *h;
  
      ngx_http_core_main_conf_t*cmcf;
  

  
      cmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_core_module);
  

  
      h = ngx_array_push(&cmcf->phases.handlers);
  
      if (h == NULL) {
  
                return NGX_ERROR;
  
      }
  

  
      *h = ngx_http_hello_handler;
  

  
      return NGX_OK;
  
}


页: [1]
查看完整版本: nginx模块开发入门