nginx 모듈 구성

8554 단어 nginxngx
모듈 을 작성 하려 면 아래 5 개 부분 을 실현 해 야 합 니 다.
  • 정의 ngxmodule_t 모듈 구조 체
  • 정의 commands
  • 정의 cxn
  • commands 에 대응 하 는 실현 함수 실현
  • handler 함수 실현 (command 실현 함수 의존 handler 함수)  (이것 이 야 말로 진짜 일 하 는 것 이다)
  • 각 함수 부분의 의 미 를 설명 하 다.
  • commands
  • eg:
    
    static ngx_command_t  ngx_echo_commands[] = {
    {
            ngx_string("echo"),  //    
            NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1, //                 
            ngx_echo_readconf,            //       .                ,     handler
            NGX_HTTP_LOC_CONF_OFFSET,    //      loc conf  
            offsetof(ngx_echo_loc_conf_t, ecdata), //             
            NULL  //    
    },
    ngx_null_command  //command    
    };
    

    nginx 는 몇 가지 기본 변환 함 수 를 제공 합 니 다:
    ngx_conf_set_flag_slot: "on" 과 "off" 를 1 과 0 으로 바 꿉 니 다.
    ngx_conf_set_str_slot: 문자열 을 ngx 로 포맷 합 니 다.str_t 형식
    ngx_conf_set_num_slot: 숫자 를 분석 하고 정형 으로 변환 합 니 다.
    ngx_conf_set_size_slot: 크기 를 나타 내 는 값 ("8k", "1m" 등) 을 해석 하고 size 로 포맷 합 니 다.t 형식
  • commands 실현 함수 (command 의 리 셋 함수)
  • 이 함 수 는 일반적으로 설정 파일 에 있 는 명령 의 매개 변 수 를 필요 한 형식 으로 바 꾸 고 설정 구조 체 에 저장 합 니 다
  • .
  • 이 함 수 는 핵심 모듈 설정 (즉 이 location 설정) 을 수정 하여 handler 를 우리 가 작성 한 handler 로 바 꿉 니 다.
  • 이렇게 하면 이 location 의 기본 handler 를 차단 하고 ngx 를 사용 합 니 다.http_echo_handler 에서 HTTP 응답 을 생 성 합 니 다.
    
    static char *                                                           
    ngx_http_circle_gif(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)     
    {/*{{{*/                                                                
        ngx_http_core_loc_conf_t  *clcf;                                    
        ngx_http_circle_gif_loc_conf_t *cglcf = conf;                       
        clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); 
        clcf->handler = ngx_http_circle_gif_handler;                                                                                                
        cglcf->enable = 1;                                                  
        return NGX_CONF_OK;                                                 
    }/*}}}*/                                                                
    
  • context
  • 이 기 구 는 주로 각 hook 함 수 를 정의 합 니 다.
    
    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                */
    };
    

    create_loc_conf 는 구조 체 를 설정 하기 위 한 메모리 할당 등 설정 구조 체 를 초기 화 하 는 데 사 용 됩 니 다.
    merge_loc_conf 는 부모 block 의 설정 정 보 를 이 구조 체 에 통합 하 는 데 사 용 됩 니 다. 즉, 설정 의 계승 을 실현 하 는 것 입 니 다.
  • handler
  • 이 함수 의 직책:
  • 모듈 설정 읽 기
  • 처리 기능 업무
  • HTTP 헤더 생 성
  • HTTP body 생 성
  • handler 는 ngx 를 받 습 니 다.http_request_t 포인터 형식의 매개 변 수 는 ngx 를 가리 키 고 있 습 니 다.http_request_t 구조 체, 이 구조 체 는 이번 HTTP 요청 에 대한 정 보 를 저장 합 니 다.
    
    static ngx_int_t ngx_echo_handler(ngx_http_request_t *r)                                         
    {                                                                                                
        ngx_int_t     rc;                                                                            
        ngx_buf_t    *b;                                                                             
        ngx_chain_t   out;                                                                           
        ngx_echo_loc_conf_t  *cglcf;                                                                 
        cglcf = ngx_http_get_module_loc_conf(r, ngx_module_echo);                                    
        if (!(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD))) {                                           
            return NGX_HTTP_NOT_ALLOWED;                                                             
        }                                                                                            
        if (r->headers_in.if_modified_since) {                                                       
            return NGX_HTTP_NOT_MODIFIED;                                                            
        }                                                                                            
        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 = cglcf->ecdata.len;                                         
        if (r->method == NGX_HTTP_HEAD) {                                                            
            rc = ngx_http_send_header(r);                                                            
            if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {                                  
                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 = cglcf->ecdata.data;                                                                 
        b->last = cglcf->ecdata.data+(cglcf->ecdata.len);                                            
        b->memory = 1;                                                                               
        b->last_buf = 1;                                                                             
        rc = ngx_http_send_header(r);                                                                
        if (rc == NGX_ERROR || rc > NGX_OK || r->header_only) {                                      
            return rc;                                                                               
        }                                                                                            
        return ngx_http_output_filter(r, &out);                                                      
    }                                                                                                
    
  •   ngx-module_t
  • 조합 Nginx Module
    
    ngx_module_t  ngx_http_circle_gif_module = {                
        NGX_MODULE_V1,                                          
        &ngx_http_circle_gif_module_ctx, /* module context */   
        ngx_http_circle_gif_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                                   
    };                                                          
    

    좋은 웹페이지 즐겨찾기