当初我要是这么学习Nginx就好了!(多图详解)
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; (编辑:晋中站长网) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |