iT邦幫忙

0

nginx - 處理http請求

  • 分享至 

  • xImage
  •  

每個配置項都有這個結構:

struct ngx_http_core_loc_conf_s {
    ...
    ngx_http_handler_pt  handler;
    ...
};
typedef ngx_int_t (*ngx_http_handler_pt)(ngx_http_request_t *r);

需要先define http request的return value (NGX_HTTP_OK... /src/http/ngx_http_request.h)
也能返回nginx global error code:

#define  NGX_OK          0      //繼續執行
#define  NGX_ERROR      -1      
#define  NGX_AGAIN      -2      //結束目前handler處理
#define  NGX_BUSY       -3
#define  NGX_DONE       -4      //框架不再執行
#define  NGX_DECLINED   -5      //在同phase繼續找對該請求有興趣的module
#define  NGX_ABORT      -6
#define  NGX_NEXT       -7

ngx_http_request_s

  • method: NGX_HTTP_GET(0x0002), NGX_HTTP_POST(0x0008)...用&判斷flag
  • uri
  • http_version
  • header_in: 未經解析的http header

接收http header
ngx_http_request_t -> header_in存放接收到的header

接收http body

ngx_int_t ngx_http_read_client_request_body(ngx_http_request_t*r, ngx_http_client_body_handler_pt post_handler)

異步, 調用時表示要求nginx開始接收request body, 接收完調用post_handler.

發送http response header

ngx_int_t ngx_http_send_header(ngx_http_request_t *r);
static ngx_int_t ngx_http_header_filter(ngx_http_request_t *r)
//ngx_http_request_t -> header_out存放response header

例子:
https://ithelp.ithome.com.tw/upload/images/20180329/20109268bHjYNiPMSC.png

發送http response body

ngx_int_t ngx_http_output_filter(ngx_http_request_t *r, ngx_chain_t *in)

ngx_chain_t *in 用於容納ngx_buf_t
例子:

ngx_buf_t *b = ngx_create_temp_buf(r->pool, 128);

ngx_chain_t out;
out.buf = b;
out.next = NULL;
return ngx_http_output_filter(r, &out);     //發送response body
ngx_buf_t *ngx_create_temp_buf(ngx_pool_t *pool, size_t size)
//nginx做的封裝,創造暫存區

https://ithelp.ithome.com.tw/upload/images/20180329/201092686GleAvfZ49.png


圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言