>  기사  >  백엔드 개발  >  기사 19: Nginx의 http 요청 또는 응답에 헤더 필드 추가하기

기사 19: Nginx의 http 요청 또는 응답에 헤더 필드 추가하기

WBOY
WBOY원래의
2016-08-08 09:19:122205검색

재인쇄 및 재인쇄를 환영합니다. 출처를 표시해 주세요. http://blog.csdn.net/yankai0219/article/details/8270219

0.序1.upload模块中添加头部字段

2.Nginx对于ngx_http_headers_out_t  headers_out.headers成员变量的处理

            1)请求:

            2)响应:

3.向请求或者响应中添加头部字段

            1)程序 在请求中添加头部字段 helloHeaders 内容123344

0. 서문 1. 업로드 모듈에 헤더 필드 추가2.ngx_http_headers_out_t headers_out. 헤더 멤버 변수 처리 헤더 필드 helloHeaders 콘텐츠 123344

0. 머리말

프로젝트 요구에 따라 파일의 md5 헤더 필드를 추가해야 합니다. http 응답. 우리 모두 알고 있듯이 Nginx 응답의 헤더 필드는 모두 ngx_http_headers_out_t 구조에 있습니다. 필드에 md5 헤더가 없습니다. 그러면 http 프로토콜이 md5 헤더 필드를 지원하지 않는다는 뜻인가요?

기사를 확인하면 http 프로토콜은 md5 헤더 필드를 지원하지만 nginx는 ngx_http_headers_out_t 이 헤더 필드는 구조에 추가되지 않습니다. nginx 작성자 설명 http://mailman.nginx.org/pipermail/nginx/2008-July/006251.html

그렇다면 Nginx는 실제로 md5 헤더 필드를 지원하지 않는 걸까요? 확인 결과 업로드 모듈이 content-md5 헤더 필드를 지원하는 것으로 나타났습니다. 그러면 nginx가 content-md5를 지원한다는 뜻인데 어떻게 구현해야 할까요?

요약: 1) http 프로토콜은 md5 헤더 필드를 지원합니다. 2) Nginx는 content-md5를 지원합니다

1. 업로드 모듈에 헤더 필드 추가

nginx 업로드 모듈에서

ngx_http_upload_add_header를 통해 구성 파일의 헤더 필드를 구문 분석하고 ulcf->header_templates에 저장한 후 추가합니다. ngx_http_upload_add_headers 함수를 호출하여 헤더 필드를 응답 헤더에 추가합니다. 다음으로 응답 헤더에 어떻게 추가되는지 분석합니다.

Nginx 업로드 모듈 소스코드에서 ngx_http_upload_add_headers 함수를 볼 수 있습니다.

프로세스는 다음과 같습니다. 1)

ulcf->header_templates

에서 헤더 필드를 구문 분석합니다. 2)

에서ngx_http_headers_out_t headers_out의 ngx_list_t 헤더에 요소를 추가합니다.

                  3) 새로 추가된 요소의 값을 입력합니다.

정적 ngx_int_t ngx_http_upload_add_headers(ngx_http_request_t *r, ngx_http_upload_loc_conf_t *ulcf) { /* // 🎜> ngx_list_push(

&

r->headers_out. 헤더); //인ngx_http_headers_out_t headers_out의 ngx_list_t                                                        🎜>h >->해시 = 1;

->

.len = 이름.len

;    h->.데이터 = 이름.데이터

;

               h->.len =.len;

              h->.데이터 = .데이터;

                ..................................

} /* }}} */

2.Nginx for ngx_http_headers_out_t headers_out.headers 멤버 변수 처리

1) 요청:

ngx_http_proxy_create_request 함수에서 http 요청의 구성을 볼 수 있습니다( 세 부분(요청 라인, 요청 헤더, 요청 본문)을 포함하여 요청 헤더 처리만 살펴보면 됩니다.

if (plcf->upstream.pass_request_headers) {
부분 = &r->headers_in.headers.part;
헤더 = 부분>elts;
i = 0; > 만약(i >= 부분->넬트) {    if
(부분->다음 == NULL) {      break
; }
부분 = 부분->다음; 헤더 = 부분->elts; i = 0;
}
> (ngx_hash_find(&plcf->headers_set_hash, header[i].hash,
     >
                                                                                    🎜 > ;
                                                                                                             b->마지막, 헤더[i].key.data, 헤더[i].key.len);
*b->last++ = ':' ; *b->last++ =
' ' ;
b->last = ngx_copy(b->last, header[i].value.data,
header[i].value. len); *b->last++ = CR; *b->last++ = LF;
...
}
}

2) 응답:

헤더 처리는 ngx_http_header_filter_module 모듈의 ngx_http_header_filter입니다. 이 프로세스는 요청의 헤더 처리와 매우 유사합니다.

3. 요청 또는 응답에 헤더 필드를 추가합니다.

ngx_http_request_t 멤버 변수 headers_in 및 headers_out에는 ngx_list_t 헤더 멤버 변수가 있습니다. 그 역할은 헤더 필드를 저장하는 것입니다. 이 함수에서는 헤더 필드를 응답에 넣는 ngx_list_push(&r->headers_out.headers) 함수를 볼 수 있습니다.

비유적으로 ngx_list_push(&r->headers_in.headers) 함수를 사용하여 헤더 필드를 요청에 넣을 수 있습니다.

1) 프로그램 헤더 필드 helloHeaders를 요청 Content 123344에 추가합니다

이 함수는 ngx_http_proxy_module.c의 ngx_http_proxy_handler에 있습니다

무효 yk_add_headers_in( ngx_http_request_t *r) { /* {{{ */
                                             printf("시작 yk_add_headers_in" ); 
 ngx_table_el t_t *h;    
ngx_str_t 이름 = ngx_string("helloHeaders" );
ngx_str_t 값 = ngx_string("123344" ); h = ngx_list_push(&r->
headers_in.headers ); >(h ==
NULL){                                                   🎜>반환 ;
} h- > 해시
= 1;
h-> .
= 이름.len; h-> .데이터
= 이름.데이터; h-> .len
= 값.len;
h-> .데이터 = 값.데이터;
}
#endif

프록시를 설정하는 중이므로 모두 모든 요청은 ngx_http_proxy_handler 함수에 들어가므로 ngx_http_proxy_handler 함수 시작 부분에 yk_add_headers_in( 을 추가하세요. r) 그러면 모든 요청 헤더에 helloHeaders 헤더 필드가 포함됩니다. 실제로 패킷 캡처를 통해 이러한 경우를 확인할 수 있습니다.

                                       위는 기사 19: Nginx의 http 요청 또는 응답에 헤더 필드 추가 관련 내용을 포함하여 PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.