设为首页 - 加入收藏 ASP站长网(Aspzz.Cn)- 科技、建站、经验、云计算、5G、大数据,站长网!
热搜: 创业者 手机 数据
当前位置: 首页 > 服务器 > 搭建环境 > Unix > 正文

当初我要是这么学习Nginx就好了!(多图详解)(4)

发布时间:2020-03-23 12:31 所属栏目:119 来源:站长网
导读:ngx_buf_t*b; ngx_chain_tout; ngx_http_test_conf_t*lrcf; ngx_str_tngx_test_string=ngx_string(hellotest); lrcf=ngx_http_get_module_loc_conf(r,ngx_http_test_module); if(lrcf-test_counter==0){ returnNGX_

    ngx_buf_t                               *b; 

    ngx_chain_t                             out; 

    ngx_http_test_conf_t                    *lrcf; 

    ngx_str_t                               ngx_test_string = ngx_string("hello test"); 

 

    lrcf = ngx_http_get_module_loc_conf(r, ngx_http_test_module); 

    if ( lrcf->test_counter == 0 ) { 

        return NGX_DECLINED; 

    } 

 

    /* we response to 'GET' and 'HEAD' requests only */ 

    if ( !(r->method & (NGX_HTTP_GET|NGX_HTTP_HEAD)) ) { 

            return NGX_HTTP_NOT_ALLOWED; 

    } 

 

    /* discard request body, since we don't need it here */ 

    rc = ngx_http_discard_request_body(r); 

 

    if ( rc != NGX_OK ) { 

        return rc; 

    } 

 

    /* 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"; 

    */ 

    ngx_str_set(&r->headers_out.content_type, "text/html"); 

 

    /* send the header only, if the request type is http 'HEAD' */ 

    if ( r->method == NGX_HTTP_HEAD ) { 

        r->headers_out.status = NGX_HTTP_OK; 

        r->headers_out.content_length_n = ngx_test_string.len; 

 

        return ngx_http_send_header(r); 

    } 

 

    /* set the status line */ 

    r->headers_out.status = NGX_HTTP_OK; 

    r->headers_out.content_length_n =  ngx_test_string.len; 

 

    /* send the headers of your response */ 

    rc = ngx_http_send_header(r); 

    if ( rc == NGX_ERROR || rc > NGX_OK || r->header_only ) { 

        return rc; 

    } 

 

    /* allocate a buffer for your response body */ 

    b = ngx_pcalloc(r->pool, sizeof(ngx_buf_t)); 

    if ( b == NULL ) { 

        return NGX_HTTP_INTERNAL_SERVER_ERROR; 

    } 

 

    /* attach this buffer to the buffer chain */ 

    out.buf = b; 

    out.next = NULL; 

 

    /* adjust the pointers of the buffer */ 

    b->pos = ngx_test_string.data; 

    b->last = ngx_test_string.data + ngx_test_string.len; 

(编辑:ASP站长网)

网友评论
推荐文章
    热点阅读