Chinaunix首页 | 论坛 | 博客
  • 博客访问: 79079
  • 博文数量: 21
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 218
  • 用 户 组: 普通用户
  • 注册时间: 2016-02-25 10:02
文章分类
文章存档

2017年(1)

2016年(20)

我的朋友

分类: LINUX

2016-08-30 13:43:52

主要的配置文件如下
user root;    #如果nginx没有对应的权限会报段错误
location /mytest{
    mytest;
}

点击(此处)折叠或打开

  1. #include <ngx_core.h>
  2. #include <ngx_http.h>
  3. #include <nginx.h>

  4. static void *ngx_http_mytest_create_loc_conf(ngx_conf_t *cf);
  5. static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);
  6. static ngx_int_t ngx_http_mytest_handler(ngx_http_request_t *r);
  7. static char *ngx_http_mytest_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child);

  8. typedef struct{
  9.     ngx_http_upstream_conf_t upstream;
  10. }ngx_http_mytest_conf_t;


  11. static ngx_str_t ngx_http_proxy_hide_headers[] =
  12. {
  13.     ngx_string("Date"),
  14.     ngx_string("Server"),
  15.     ngx_string("X-Pad"),
  16.     ngx_string("X-Accel-Expires"),
  17.     ngx_string("X-Accel-Redirect"),
  18.     ngx_string("X-Accel-Limit-Rate"),
  19.     ngx_string("X-Accel-Buffering"),
  20.     ngx_string("X-Accel-Charset"),
  21.     ngx_null_string
  22. };

  23. typedef struct
  24. {
  25.     ngx_http_status_t status;
  26.     ngx_str_t                    backendServer;
  27. } ngx_http_mytest_ctx_t;

  28. static ngx_command_t ngx_http_mytest_commands[] = {
  29.     {
  30.         ngx_string("mytest"),
  31.         NGX_HTTP_MAIN_CONF | NGX_HTTP_SRV_CONF | NGX_HTTP_LOC_CONF | NGX_HTTP_LMT_CONF | NGX_CONF_NOARGS,
  32.         ngx_http_mytest,
  33.         NGX_HTTP_LOC_CONF_OFFSET,
  34.         0,
  35.         NULL},
  36.     ngx_null_command
  37. };

  38. static void *ngx_http_mytest_create_loc_conf(ngx_conf_t *cf){
  39.     ngx_http_mytest_conf_t *mycf;

  40.     mycf = (ngx_http_mytest_conf_t *) ngx_pcalloc(cf->pool,sizeof(ngx_http_mytest_conf_t));
  41.     if( mycf == NULL){
  42.         return NULL;
  43.     }

  44.     mycf->upstream.connect_timeout = 60000;
  45.     mycf->upstream.send_timeout = 60000;
  46.     mycf->upstream.read_timeout = 60000;
  47.     mycf->upstream.store_access = 0600;
  48.     mycf->upstream.buffering = 0;
  49.     mycf->upstream.bufs.num = 8;
  50.     mycf->upstream.bufs.size = ngx_pagesize;
  51.     mycf->upstream.buffer_size = ngx_pagesize;


  52.     mycf->upstream.busy_buffers_size = 2 * ngx_pagesize;
  53.     mycf->upstream.temp_file_write_size = 2 * ngx_pagesize;


  54.     mycf->upstream.max_temp_file_size = 1024 * 1024 * 1024;

  55.     mycf->upstream.hide_headers = NGX_CONF_UNSET_PTR;
  56.     mycf->upstream.pass_headers = NGX_CONF_UNSET_PTR;

  57.     return mycf;
  58. }

  59. static char *ngx_http_mytest_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child){
  60.     ngx_http_mytest_conf_t *prev = (ngx_http_mytest_conf_t *) parent;
  61.     ngx_http_mytest_conf_t *conf = (ngx_http_mytest_conf_t *) child;

  62.     ngx_hash_init_t hash;
  63.     hash.max_size = 100;
  64.     hash.bucket_size = 1024;
  65.     hash.name = "proxy_headers_hash";


  66.     if(ngx_http_upstream_hide_headers_hash(cf,&conf->upstream,
  67.                 &prev->upstream,
  68.                 ngx_http_proxy_hide_headers,
  69.                 &hash)!=NGX_OK){

  70.         return NGX_CONF_ERROR;
  71.     }
  72.     return NGX_CONF_OK;
  73. }

  74. static ngx_http_module_t ngx_http_mytest_module_ctx =
  75. {
  76.     NULL, /* preconfiguration */
  77.     NULL,                        /* postconfiguration */
  78.     NULL, /* create main configuration */
  79.     NULL, /* init main configuration */
  80.     NULL, /* create server configuration */
  81.     NULL, /* merge server configuration */
  82.     ngx_http_mytest_create_loc_conf,                /* create location configuration */
  83.     ngx_http_mytest_merge_loc_conf                    /* merge location configuration */
  84. };

  85. ngx_module_t ngx_http_mytest_module =
  86. {
  87.     NGX_MODULE_V1,
  88.     &ngx_http_mytest_module_ctx, /* module context */
  89.     ngx_http_mytest_commands, /* module directives */
  90.     NGX_HTTP_MODULE, /* module type */
  91.     NULL, /* init master */
  92.     NULL, /* init module */
  93.     NULL, /* init process */
  94.     NULL, /* init thread */
  95.     NULL, /* exit thread */
  96.     NULL, /* exit process */
  97.     NULL, /* exit master */
  98.     NGX_MODULE_V1_PADDING
  99. };

  100. static char *ngx_http_mytest(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
  101. {
  102.     ngx_http_core_loc_conf_t *clcf;

  103.     clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module);

  104.     clcf->handler = ngx_http_mytest_handler;

  105.     return NGX_CONF_OK;
  106. }

  107. static ngx_int_t mytest_upstream_create_request(ngx_http_request_t *r){
  108.     //XXXX 为后端请求的IP
  109.     static ngx_str_t backendQueryLine=ngx_string("GET / HTTP/1.1\r\nHost: XXXX\r\nUser-Agent: curl\r\n\r\n");

  110.     ngx_int_t queryLinelen = backendQueryLine.len;
  111.     ngx_buf_t *b = ngx_create_temp_buf(r->pool,queryLinelen);

  112.     if(b == NULL){
  113.         return NGX_ERROR;
  114.     }
  115.     
  116.     ngx_memcpy(b->pos ,backendQueryLine.data ,backendQueryLine.len);
  117.     b->last = b->pos+queryLinelen;

  118.     r->upstream->request_bufs=ngx_alloc_chain_link(r->pool);
  119.     if(r->upstream->request_bufs == NULL){
  120.         return NGX_ERROR;
  121.     }
  122.     r->upstream->request_bufs->buf = b;
  123.     r->upstream->request_bufs->next = NULL;

  124.     r->upstream->request_sent = 0;
  125.     r->upstream->header_sent = 0;

  126.     r->header_hash = 1;
  127.     
  128.     return NGX_OK;
  129. }


  130. static ngx_int_t mytest_upstream_process_header(ngx_http_request_t *r){
  131.     ngx_int_t rc;
  132.     ngx_table_elt_t *h;
  133.     ngx_http_upstream_header_t *hh;
  134.     ngx_http_upstream_main_conf_t *umcf;

  135.     umcf = ngx_http_get_module_main_conf(r, ngx_http_upstream_module);
  136.     for( ; ; ){
  137.         rc = ngx_http_parse_header_line(r,&r->upstream->buffer,1);
  138.         if(rc == NGX_OK){
  139.             h = ngx_list_push(&r->upstream->headers_in.headers);
  140.             if(h == NULL){
  141.                 return NGX_ERROR;
  142.             }
  143.             h->hash = r->header_hash;
  144.             h->key.len = r->header_name_end - r->header_name_start;
  145.             h->value.len = r->header_end - r->header_start;


  146.             h->key.data = ngx_pnalloc(r->pool,h->key.len + 1 + h->value.len + 1 + h->key.len);
  147.             if(h->key.data == NULL){
  148.                 return NGX_ERROR;
  149.             }
  150.             h->value.data = h->key.data + h->key.len +1;
  151.             h->lowcase_key = h->key.data + h->key.len + 1 + h->value.len + 1;


  152.             ngx_memcpy(h->key.data,r->header_name_start,h->key.len);
  153.             h->key.data[h->key.len] = '\0';


  154.             ngx_memcpy(h->value.data,r->header_start,h->value.len);
  155.             h->key.data[h->value.len] = '\0';


  156.             if(h->key.len == r->lowcase_index){
  157.                 ngx_memcpy(h->lowcase_key,r->lowcase_header,h->key.len);
  158.             }else{
  159.                 ngx_strlow(h->lowcase_key,h->key.data,h->key.len);
  160.             }

  161.             hh = ngx_hash_find(&umcf->headers_in_hash,h->hash,h->lowcase_key,h->key.len);


  162.             if(hh && hh->handler(r,h,hh->offset)!=NGX_OK){
  163.                 return NGX_ERROR;
  164.             }
  165.             continue;
  166.         }
  167.         if(rc == NGX_HTTP_PARSE_HEADER_DONE){
  168.             if(r->upstream->headers_in.server == NULL){
  169.                 h = ngx_list_push(&r->upstream->headers_in.headers);
  170.                 if(h == NULL){
  171.                     return NGX_ERROR;
  172.                 }
  173.                 h->hash = ngx_hash(ngx_hash(ngx_hash(ngx_hash(ngx_hash('s', 'e'), 'r'), 'v'), 'e'), 'r');
  174.                 ngx_str_set( &h->key,"Server");
  175.                 ngx_str_null( &h->value);
  176.                 h->lowcase_key = (u_char *)"server";
  177.             }
  178.             if(r->upstream->headers_in.date == NULL){
  179.                 h = ngx_list_push(&r->upstream->headers_in.headers);
  180.                 if(h == NULL){
  181.                     return NGX_ERROR;
  182.                 }
  183.                 h->hash = ngx_hash(ngx_hash(ngx_hash('d','a'),'t'),'e');
  184.                 ngx_str_set(&h->key,"Date");
  185.                 ngx_str_null(&h->value);
  186.                 h->lowcase_key = (u_char *)"date";
  187.             }
  188.             return NGX_OK;
  189.         }
  190.         if(rc == NGX_AGAIN){
  191.             return NGX_AGAIN;
  192.         }
  193.         ngx_log_error(NGX_LOG_ERR,r->connection->log,0,"upstream sent invalid header");


  194.         return NGX_HTTP_UPSTREAM_INVALID_HEADER;
  195.     }
  196. }
  197. static ngx_int_t mytest_process_status_line(ngx_http_request_t *r){
  198.     size_t len;
  199.     ngx_int_t rc;
  200.     ngx_http_upstream_t *u;

  201.     ngx_http_mytest_ctx_t *ctx = ngx_http_get_module_ctx(r,ngx_http_mytest_module);
  202.     if(ctx == NULL){
  203.         return NGX_ERROR;
  204.     }
  205.     
  206.     u = r->upstream;

  207.     rc = ngx_http_parse_status_line(r, &u->buffer, &ctx->status);

  208.     if(rc == NGX_AGAIN){
  209.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,"Don't get full response header");
  210.         return rc;
  211.     }


  212.     if(rc == NGX_ERROR) {
  213.         ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,"upstream sent no valid HTTP/1.0 header");
  214.         r->http_version = NGX_HTTP_VERSION_9;
  215.         u->state->status = NGX_HTTP_OK;

  216.         return NGX_OK;
  217.     }

  218.     if(u->state){
  219.         u->state->status = ctx->status.code;
  220.     }

  221.     u->headers_in.status_n = ctx->status.code;

  222.     len = ctx->status.end - ctx->status.start;
  223.     u->headers_in.status_line.len = len;

  224.     u->headers_in.status_line.data = ngx_palloc(r->pool,len)
阅读(1607) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~