国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 網(wǎng)站 > WEB服務(wù) > 正文

nginx HTTP處理流程的淺析

2020-03-22 16:24:53
字體:
供稿:網(wǎng)友
本篇文章給大家?guī)淼膬?nèi)容是關(guān)于nginx HTTP處理流程的淺析,有一定的參考價(jià)值,有需要的朋友可以參考一下,希望對(duì)你有所幫助。

1.初始化服務(wù)器

server指令用于配置virtual server,我們通常會(huì)在一臺(tái)機(jī)器配置多個(gè)virtual server,監(jiān)聽不同端口號(hào),映射到不同文件目錄;nginx解析用戶配置,在所有端口創(chuàng)建socket并啟動(dòng)監(jiān)聽。

nginx解析配置文件是由各個(gè)模塊分擔(dān)處理的,每個(gè)模塊注冊(cè)并處理自己關(guān)心的配置,通過模塊結(jié)構(gòu)體ngx_module_t的字段ngx_command_t *commands實(shí)現(xiàn);

例如ngx_http_module是一個(gè)核心模塊,其commands字段定義如下:

struct ngx_command_s { ngx_str_t name; ngx_uint_t type; char *(*set)(ngx_conf_t *cf, ngx_command_t *cmd, void *conf);html' target='_blank'>static ngx_command_t ngx_http_commands[] = { { ngx_string( http ), NGX_MAIN_CONF|NGX_CONF_BLOCK|NGX_CONF_NOARGS, ngx_http_block,};

name指令名稱,解析配置文件時(shí)按照名稱能匹配查找;

type指令類型,NGX_CONF_NOARGS該配置無參數(shù),NGX_CONF_BLOCK該配置是一個(gè)配置塊,NGX_MAIN_CONF表示配置可以出現(xiàn)在哪些位(NGX_MAIN_CONF、NGX_HTTP_SRV_CONF、NGX_HTTP_LOC_CONF);

set指令處理函數(shù)指針;

可以看到解析http指令的處理函數(shù)為ngx_http_block,實(shí)現(xiàn)如下:

static char * ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) //解析main配置 //解析server配置 //解析location配置 //初始化HTTP處理流程所需的handler //初始化listening if (ngx_http_optimize_servers(cf, cmcf, cmcf- ports) != NGX_OK) { return NGX_CONF_ERROR;}

ngx_http_optimize_servers方法循環(huán)所有配置端口,創(chuàng)建ngx_listening_t對(duì)象,并將其添加到conf- cycle- listening(后續(xù)操作會(huì)遍歷此數(shù)組,創(chuàng)建socket并監(jiān)聽)。方法主要操作如下圖:

625472880-5bc55805f2035_articlex.png

注意到這里設(shè)置了ngx_listening_t的handler為ngx_http_init_connection,當(dāng)接收到socket連接請(qǐng)求時(shí),會(huì)調(diào)用此handler處理。

那么什么時(shí)候啟動(dòng)監(jiān)聽呢?全局搜索關(guān)鍵字cycle- listening可以找到。main方法會(huì)調(diào)用ngx_init_cycle,其完成了服務(wù)器初始化的大部分工作,其中就包括啟動(dòng)監(jiān)聽(ngx_open_listening_sockets)。

假設(shè)nginx使用epoll處理所有socket事件,什么時(shí)候?qū)⒈O(jiān)聽事件添加到epoll呢?全局搜索關(guān)鍵字cycle- listening可以找到。ngx_event_core_module模塊是事件處理核心模塊,初始化此模塊時(shí)會(huì)執(zhí)行ngx_event_process_init函數(shù),其中將監(jiān)聽事件添加到epoll。

static ngx_int_t ngx_event_process_init(ngx_cycle_t *cycle) ls = cycle- listening.elts; for (i = 0; i cycle- listening.nelts; i++) { //設(shè)置讀事件處理handler rev- handler = ngx_event_accept; ngx_add_event(rev, NGX_READ_EVENT, 0);}

注意到接收到客戶端socket連接請(qǐng)求事件的處理函數(shù)是ngx_event_accept。

2.HTTP請(qǐng)求解析

2.1 基礎(chǔ)結(jié)構(gòu)體

結(jié)構(gòu)體ngx_connection_t存儲(chǔ)socket連接相關(guān)信息;nginx預(yù)先創(chuàng)建若干個(gè)ngx_connection_t對(duì)象,存儲(chǔ)在全局變量ngx_cycle- free_connections,稱之為連接池;當(dāng)新生成socket時(shí),會(huì)嘗試從連接池中獲取空閑connection連接,如果獲取失敗,則會(huì)直接關(guān)閉此socket。

指令worker_connections用于配置連接池最大連接數(shù)目,配置在events指令塊中,由ngx_event_core_module解析。

vents { use epoll; worker_connections 60000;}

當(dāng)nginx作為HTTP服務(wù)器時(shí),最大客戶端數(shù)目maxClient=worker_processesworker_connections/2;當(dāng)nginx作為反向代理服務(wù)器時(shí),最大客戶端數(shù)目maxClient=worker_processesworker_connections/4。其worker_processes為用戶配置的worker進(jìn)程數(shù)目。

結(jié)構(gòu)體ngx_connection_t定義如下:

struct ngx_connection_s { //空閑連接池中,data指向下一個(gè)連接,形成鏈表;取出來使用時(shí),data指向請(qǐng)求結(jié)構(gòu)體ngx_http_request_s void *data; //讀寫事件結(jié)構(gòu)體,兩個(gè)關(guān)鍵字段:handler處理函數(shù)、timer定時(shí)器 ngx_event_t *read; ngx_event_t *write; ngx_socket_t fd; //socket fd ngx_recv_pt recv; //socket接收數(shù)據(jù)函數(shù)指針 ngx_send_pt send; //socket發(fā)送數(shù)據(jù)函數(shù)指針 ngx_buf_t *buffer; //輸入緩沖區(qū) struct sockaddr *sockaddr; //客戶端地址 socklen_t socklen; ngx_listening_t *listening; //監(jiān)聽的ngx_listening_t對(duì)象 struct sockaddr *local_sockaddr; //本地地址 socklen_t local_socklen; …………}

結(jié)構(gòu)體ngx_http_request_t存儲(chǔ)整個(gè)HTTP請(qǐng)求處理流程所需的所有信息,字段非常多,這里只進(jìn)行簡(jiǎn)要說明:

struct ngx_http_request_s { ngx_connection_t *connection; //讀寫事件處理handler ngx_http_event_handler_pt read_event_handler; ngx_http_event_handler_pt write_event_handler; //請(qǐng)求頭緩沖區(qū) ngx_buf_t *header_in; //解析后的請(qǐng)求頭 ngx_http_headers_in_t headers_in; //請(qǐng)求體結(jié)構(gòu)體 ngx_http_request_body_t *request_body; //請(qǐng)求行 ngx_str_t request_line; //解析后請(qǐng)求行若干字段 ngx_uint_t method; ngx_uint_t http_version; ngx_str_t uri; ngx_str_t args; …………}

請(qǐng)求行與請(qǐng)求體解析相對(duì)比較簡(jiǎn)單,這里重點(diǎn)講述請(qǐng)求頭的解析,解析后的請(qǐng)求頭信息都存儲(chǔ)在ngx_http_headers_in_t結(jié)構(gòu)體中。

ngx_http_request.c文件中定義了所有的HTTP頭部,存儲(chǔ)在ngx_http_headers_in數(shù)組,數(shù)組的每個(gè)元素是一個(gè)ngx_http_header_t結(jié)構(gòu)體,主要包含三個(gè)字段,頭部名稱、頭部解析后字段存儲(chǔ)在ngx_http_headers_in_t的偏移量,解析頭部的處理函數(shù)。

ngx_http_header_t ngx_http_headers_in[] = { { ngx_string( Host ), offsetof(ngx_http_headers_in_t, host), ngx_http_process_host }, { ngx_string( Connection ), offsetof(ngx_http_headers_in_t, connection), ngx_http_process_connection }, …………typedef struct { ngx_str_t name; ngx_uint_t offset; ngx_http_header_handler_pt handler;} ngx_http_header_t;

解析請(qǐng)求頭時(shí),從ngx_http_headers_in數(shù)組中查找請(qǐng)求頭ngx_http_header_t對(duì)象,調(diào)用處理函數(shù)handler,存儲(chǔ)到r- headers_in對(duì)應(yīng)字段。以解析Connection頭部為例,ngx_http_process_connection實(shí)現(xiàn)如下:

static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h, ngx_uint_t offset) if (ngx_strcasestrn(h- value.data, close , 5 - 1)) { r- headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE; } else if (ngx_strcasestrn(h- value.data, keep-alive , 10 - 1)) { r- headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE; return NGX_OK;}

輸入?yún)?shù)offset在此處并沒有什么作用。注意到第二個(gè)輸入?yún)?shù)ngx_table_elt_t,存儲(chǔ)了當(dāng)前請(qǐng)求頭的鍵值對(duì)信息:

typedef struct { ngx_uint_t hash; //請(qǐng)求頭key的hash值 ngx_str_t key; ngx_str_t value; u_char *lowcase_key; //請(qǐng)求頭key轉(zhuǎn)為小寫字符串(可以看到HTTP請(qǐng)求頭解析時(shí)key不區(qū)分大小寫)} ngx_table_elt_t;

再思考一個(gè)問題,從ngx_http_headers_in數(shù)組中查找請(qǐng)求頭對(duì)應(yīng)ngx_http_header_t對(duì)象時(shí),需要遍歷,每個(gè)元素都需要進(jìn)行字符串比較,效率低下。因此nginx將ngx_http_headers_in數(shù)組轉(zhuǎn)換為哈希表,哈希表的鍵即為請(qǐng)求頭的key,方法ngx_http_init_headers_in_hash實(shí)現(xiàn)了數(shù)組到哈希表的轉(zhuǎn)換,轉(zhuǎn)換后的哈希表存儲(chǔ)在cmcf- headers_in_hash字段。

827829181-5bc5588653a0d_articlex.png

2.2 解析HTTP請(qǐng)求

第1節(jié)提到,在創(chuàng)建socket啟動(dòng)監(jiān)聽時(shí),會(huì)添加可讀事件到epoll,事件處理函數(shù)為ngx_event_accept,用于接收socket連接,分配connection連接,并調(diào)用ngx_listening_t對(duì)象的處理函數(shù)(ngx_http_init_connection)。

void ngx_event_accept(ngx_event_t *ev) s = accept4(lc- fd, (struct sockaddr *) sa, socklen, SOCK_NONBLOCK); //客戶端socket連接成功時(shí),都需要分配connection連接,如果分配失敗則會(huì)直接關(guān)閉此socket。 //而每個(gè)worker進(jìn)程連接池的最大連接數(shù)目是固定的,當(dāng)不存在空閑連接時(shí),此worker進(jìn)程accept的所有socket都會(huì)被拒絕; //多個(gè)worker進(jìn)程通過競(jìng)爭(zhēng)執(zhí)行epoll_wait;而當(dāng)ngx_accept_disabled大于0時(shí),會(huì)直接放棄此次競(jìng)爭(zhēng),同時(shí)ngx_accept_disabled減1。 //以此實(shí)現(xiàn),當(dāng)worker進(jìn)程的空閑連接過少時(shí),減少其競(jìng)爭(zhēng)epoll_wait次數(shù) ngx_accept_disabled = ngx_cycle- connection_n / 8 - ngx_cycle- free_connection_n; c = ngx_get_connection(s, ev- log); ls- handler(c);}

socket連接成功后,nginx會(huì)等待客戶端發(fā)送HTTP請(qǐng)求,默認(rèn)會(huì)有60秒的超時(shí)時(shí)間,即60秒內(nèi)沒有接收到客戶端請(qǐng)求時(shí),斷開此連接,打印錯(cuò)誤日志。函數(shù)ngx_http_init_connection用于設(shè)置讀事件處理函數(shù),以及超時(shí)定時(shí)器。

void ngx_http_init_connection(ngx_connection_t *c) c- read = ngx_http_wait_request_handler; c- write- handler = ngx_http_empty_handler; ngx_add_timer(rev, c- listening- post_accept_timeout);}

全局搜索post_accept_timeout字段,可以查找到設(shè)置此超時(shí)時(shí)間的配置指令,client_header_timeout,其可以在http、server指令塊中配置。

函數(shù)ngx_http_wait_request_handler為解析HTTP請(qǐng)求的入口函數(shù),實(shí)現(xiàn)如下:

static void ngx_http_wait_request_handler(ngx_event_t *rev) //讀事件已經(jīng)超時(shí) if (rev- timedout) { ngx_log_error(NGX_LOG_INFO, c- log, NGX_ETIMEDOUT, client timed out  ngx_http_close_connection(c); return; size = cscf- client_header_buffer_size; //client_header_buffer_size指令用于配置接收請(qǐng)求頭緩沖區(qū)大小 b = c- buffer; n = c- recv(c, b- last, size); //創(chuàng)建請(qǐng)求對(duì)象ngx_http_request_t,HTTP請(qǐng)求整個(gè)處理過程都有用; c- data = ngx_http_create_request(c); rev- handler = ngx_http_process_request_line; //設(shè)置讀事件處理函數(shù)(此次請(qǐng)求行可能沒有讀取完) ngx_http_process_request_line(rev);}

函數(shù)ngx_http_create_request創(chuàng)建并初始化ngx_http_request_t對(duì)象,注意這賦值語句r- header_in =c- buffer。

解析請(qǐng)求行與請(qǐng)求頭的代碼較為繁瑣,終點(diǎn)在于讀取socket數(shù)據(jù),解析字符串,這里不做詳述。HTTP請(qǐng)求解析過程主要函數(shù)調(diào)用如下圖所示:

3000860485-5bc55954dbd75_articlex.png

注意,解析完成請(qǐng)求行與請(qǐng)求頭,nginx就開始處理HTTP請(qǐng)求,并沒有等到解析完請(qǐng)求體再處理。處理請(qǐng)求入口為ngx_http_process_request。

3.處理HTTP請(qǐng)求

3.1 HTTP請(qǐng)求處理的11個(gè)階段

nginx將HTTP請(qǐng)求處理流程分為11個(gè)階段,絕大多數(shù)HTTP模塊都會(huì)將自己的handler添加到某個(gè)階段(將handler添加到全局唯一的數(shù)組phases中),注意其中有4個(gè)階段不能添加自定義handler,nginx處理HTTP請(qǐng)求時(shí)會(huì)挨個(gè)調(diào)用每個(gè)階段的handler;

typedef enum { NGX_HTTP_POST_READ_PHASE = 0, //第一個(gè)階段,目前只有realip模塊會(huì)注冊(cè)handler,但是該模塊默認(rèn)不會(huì)運(yùn)行(nginx作為代理服務(wù)器時(shí)有用,后端以此獲取客戶端原始ip) NGX_HTTP_SERVER_REWRITE_PHASE, //server塊中配置了rewrite指令,重寫url NGX_HTTP_FIND_CONFIG_PHASE, //查找匹配的location配置;不能自定義handler; NGX_HTTP_REWRITE_PHASE, //location塊中配置了rewrite指令,重寫url NGX_HTTP_POST_REWRITE_PHASE, //檢查是否發(fā)生了url重寫,如果有,重新回到FIND_CONFIG階段;不能自定義handler; NGX_HTTP_PREACCESS_PHASE, //訪問控制,比如限流模塊會(huì)注冊(cè)handler到此階段 NGX_HTTP_ACCESS_PHASE, //訪問權(quán)限控制,比如基于ip黑白名單的權(quán)限控制,基于用戶名密碼的權(quán)限控制等 NGX_HTTP_POST_ACCESS_PHASE, //根據(jù)訪問權(quán)限控制階段做相應(yīng)處理;不能自定義handler; NGX_HTTP_TRY_FILES_PHASE, //只有配置了try_files指令,才會(huì)有此階段;不能自定義handler; NGX_HTTP_CONTENT_PHASE, //內(nèi)容產(chǎn)生階段,返回響應(yīng)給客戶端 NGX_HTTP_LOG_PHASE //日志記錄} ngx_http_phases;

nginx使用結(jié)構(gòu)體ngx_module_s表示一個(gè)模塊,其中字段ctx,是一個(gè)指向模塊上下文結(jié)構(gòu)體的指針(上下文結(jié)構(gòu)體的字段都是一些函數(shù)指針);nginx的HTTP模塊上下文結(jié)構(gòu)體大多都有字段postconfiguration,負(fù)責(zé)注冊(cè)本模塊的handler到某個(gè)處理階段。11個(gè)階段在解析完成http配置塊指令后初始化。

static char * ngx_http_block(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) //解析http配置塊 //初始化11個(gè)階段的phases數(shù)組,注意多個(gè)模塊可能注冊(cè)到同一個(gè)階段,因此phases是一個(gè)二維數(shù)組 if (ngx_http_init_phases(cf, cmcf) != NGX_OK) { return NGX_CONF_ERROR; //遍歷索引HTTP模塊,注冊(cè)handler for (m = 0; ngx_modules[m]; m++) { if (ngx_modules[m]- type != NGX_HTTP_MODULE) { continue; module = ngx_modules[m]-  if (module- postconfiguration) { if (module- postconfiguration(cf) != NGX_OK) { return NGX_CONF_ERROR; //將二維數(shù)組轉(zhuǎn)換為一維數(shù)組,從而遍歷執(zhí)行數(shù)組所有handler if (ngx_http_init_phase_handlers(cf, cmcf) != NGX_OK) { return NGX_CONF_ERROR;}

以限流模塊ngx_http_limit_req_module模塊為例,postconfiguration方法簡(jiǎn)單實(shí)現(xiàn)如下:

static ngx_int_t ngx_http_limit_req_init(ngx_conf_t *cf) h = ngx_array_push( cmcf- phases[NGX_HTTP_PREACCESS_PHASE].handlers); *h = ngx_http_limit_req_handler; //ngx_http_limit_req_module模塊的限流方法;nginx處理HTTP請(qǐng)求時(shí),都會(huì)調(diào)用此方法判斷應(yīng)該繼續(xù)執(zhí)行還是拒絕請(qǐng)求 return NGX_OK;}

GDB調(diào)試,斷點(diǎn)到ngx_http_block方法執(zhí)行所有HTTP模塊注冊(cè)handler之后,打印phases數(shù)組

p cmcf- phases[*].handlersp *(ngx_http_handler_pt*)cmcf- phases[*].handlers.elts

11個(gè)階段注冊(cè)的handler如下圖所示:

1853153573-5bc559b613ab8_articlex.png

3.2 11個(gè)階段初始化

上面提到HTTP的11個(gè)處理階段handler存儲(chǔ)在phases數(shù)組,但由于多個(gè)模塊可能注冊(cè)handler到同一個(gè)階段,使得phases是一個(gè)二維數(shù)組,因此需要轉(zhuǎn)換為一維數(shù)組,轉(zhuǎn)換后存儲(chǔ)在cmcf- phase_engine字段,phase_engine的類型為ngx_http_phase_engine_t,定義如下:

typedef struct { ngx_http_phase_handler_t *handlers; //一維數(shù)組,存儲(chǔ)所有handler ngx_uint_t server_rewrite_index; //記錄NGX_HTTP_SERVER_REWRITE_PHASE階段handler的索引值 ngx_uint_t location_rewrite_index; //記錄NGX_HTTP_REWRITE_PHASE階段handler的索引值} ngx_http_phase_engine_t;struct ngx_http_phase_handler_t { ngx_http_phase_handler_pt checker; //執(zhí)行handler之前的校驗(yàn)函數(shù) ngx_http_handler_pt handler; ngx_uint_t next; //下一個(gè)待執(zhí)行handler的索引(通過next實(shí)現(xiàn)handler跳轉(zhuǎn)執(zhí)行)//cheker函數(shù)指針類型定義typedef ngx_int_t (*ngx_http_phase_handler_pt)(ngx_http_request_t *r, ngx_http_phase_handler_t *ph);//handler函數(shù)指針類型定義typedef ngx_int_t (*ngx_http_handler_pt)(ngx_http_request_t *r);

數(shù)組轉(zhuǎn)換函數(shù)ngx_http_init_phase_handlers實(shí)現(xiàn)如下:

static ngx_int_t ngx_http_init_phase_handlers(ngx_conf_t *cf, ngx_http_core_main_conf_t *cmcf) use_rewrite = cmcf- phases[NGX_HTTP_REWRITE_PHASE].handlers.nelts ? 1 : 0; use_access = cmcf- phases[NGX_HTTP_ACCESS_PHASE].handlers.nelts ? 1 : 0; n = use_rewrite + use_access + cmcf- try_files + 1 /* find config phase */; //至少有4個(gè)階段,這4個(gè)階段是上面說的不能注冊(cè)handler的4個(gè)階段 //計(jì)算handler數(shù)目,分配空間 for (i = 0; i NGX_HTTP_LOG_PHASE; i++) { n += cmcf- phases[i].handlers.nelts; ph = ngx_pcalloc(cf- pool, n * sizeof(ngx_http_phase_handler_t) + sizeof(void *)); //遍歷二維數(shù)組 for (i = 0; i NGX_HTTP_LOG_PHASE; i++) { h = cmcf- phases[i].handlers.elts; switch (i) { case NGX_HTTP_SERVER_REWRITE_PHASE: if (cmcf- phase_engine.server_rewrite_index == (ngx_uint_t) -1) { cmcf- phase_engine.server_rewrite_index = n; //記錄NGX_HTTP_SERVER_REWRITE_PHASE階段handler的索引值 checker = ngx_http_core_rewrite_phase; break; case NGX_HTTP_FIND_CONFIG_PHASE: find_config_index = n; //記錄NGX_HTTP_FIND_CONFIG_PHASE階段的索引,NGX_HTTP_POST_REWRITE_PHASE階段可能會(huì)跳轉(zhuǎn)回此階段 ph- checker = ngx_http_core_find_config_phase; n++; ph++; continue; //進(jìn)入下一個(gè)階段NGX_HTTP_REWRITE_PHASE case NGX_HTTP_REWRITE_PHASE: if (cmcf- phase_engine.location_rewrite_index == (ngx_uint_t) -1) { cmcf- phase_engine.location_rewrite_index = n; //記錄NGX_HTTP_REWRITE_PHASE階段handler的索引值 checker = ngx_http_core_rewrite_phase;  break; case NGX_HTTP_POST_REWRITE_PHASE: if (use_rewrite) { ph- checker = ngx_http_core_post_rewrite_phase; ph- next = find_config_index; n++; ph++; continue; //進(jìn)入下一個(gè)階段NGX_HTTP_ACCESS_PHASE case NGX_HTTP_ACCESS_PHASE: checker = ngx_http_core_access_phase; n++; break; case NGX_HTTP_POST_ACCESS_PHASE: if (use_access) { ph- checker = ngx_http_core_post_access_phase; ph- next = n; ph++; continue; //進(jìn)入下一個(gè)階段 case NGX_HTTP_TRY_FILES_PHASE: if (cmcf- try_files) { ph- checker = ngx_http_core_try_files_phase; n++; ph++; continue; case NGX_HTTP_CONTENT_PHASE: checker = ngx_http_core_content_phase; break; default: checker = ngx_http_core_generic_phase; //n為下一個(gè)階段第一個(gè)handler的索引 n += cmcf- phases[i].handlers.nelts; //遍歷當(dāng)前階段的所有handler for (j = cmcf- phases[i].handlers.nelts - 1; j j--) { ph- checker = checker; ph- handler = h[j]; ph- next = n; ph++;}

GDB打印出轉(zhuǎn)換后的數(shù)組如下圖所示,第一列是cheker字段,第二列是handler字段,箭頭表示next跳轉(zhuǎn);圖中有個(gè)返回的箭頭,即NGX_HTTP_POST_REWRITE_PHASE階段可能返回到NGX_HTTP_FIND_CONFIG_PHASE;原因在于只要NGX_HTTP_REWRITE_PHASE階段產(chǎn)生了url重寫,就需要重新查找匹配location。

2115972663-5bc55a0499017_articlex.png

3.3 處理HTTP請(qǐng)求

2.2節(jié)提到HTTP請(qǐng)求的處理入口函數(shù)是ngx_http_process_request,其主要調(diào)用ngx_http_core_run_phases實(shí)現(xiàn)11個(gè)階段的執(zhí)行流程;

ngx_http_core_run_phases遍歷預(yù)先設(shè)置好的cmcf- phase_engine.handlers數(shù)組,調(diào)用其checker函數(shù),邏輯如下:

void ngx_http_core_run_phases(ngx_http_request_t *r) ph = cmcf- phase_engine.handlers; //phase_handler初始為0,表示待處理handler的索引;cheker內(nèi)部會(huì)根據(jù)ph- next字段修改phase_handler while (ph[r- phase_handler].checker) { rc = ph[r- phase_handler].checker(r, ph[r- phase_handler]); if (rc == NGX_OK) { return;}

checker內(nèi)部就是調(diào)用handler,并設(shè)置下一步要執(zhí)行handler的索引;比如說ngx_http_core_generic_phase實(shí)現(xiàn)如下:

ngx_int_t ngx_http_core_generic_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph) ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r- connection- log, 0, rewrite phase: %ui , r- phase_handler); rc = ph- handler(r); if (rc == NGX_OK) { r- phase_handler = ph- next; return NGX_AGAIN;}

3.4 內(nèi)容產(chǎn)生階段

內(nèi)容產(chǎn)生階段NGX_HTTP_CONTENT_PHASE是HTTP請(qǐng)求處理的第10個(gè)階段,一般情況有3個(gè)模塊注冊(cè)handler到此階段:ngx_http_static_module、ngx_http_autoindex_module和ngx_http_index_module。

但是當(dāng)我們配置了proxy_pass和fastcgi_pass時(shí),情況會(huì)有所不同;

使用proxy_pass配置上游時(shí),ngx_http_proxy_module模塊會(huì)設(shè)置其處理函數(shù)到配置類conf;使用fastcgi_pass配置時(shí),ngx_http_fastcgi_module會(huì)設(shè)置其處理函數(shù)到配置類conf。例如:

static char * ngx_http_fastcgi_pass(ngx_conf_t *cf, ngx_command_t *cmd, void *conf) ngx_http_core_loc_conf_t *clcf; clcf = ngx_http_conf_get_module_loc_conf(cf, ngx_http_core_module); clcf- handler = ngx_http_fastcgi_handler;}

階段NGX_HTTP_FIND_CONFIG_PHASE查找匹配的location,并獲取此ngx_http_core_loc_conf_t對(duì)象,將其handler賦值給ngx_http_request_t對(duì)象的content_handler字段(內(nèi)容產(chǎn)生處理函數(shù))。

而在執(zhí)行內(nèi)容產(chǎn)生階段的checker函數(shù)時(shí),會(huì)執(zhí)行content_handler指向的函數(shù);查看ngx_http_core_content_phase函數(shù)實(shí)現(xiàn)(內(nèi)容產(chǎn)生階段的checker函數(shù)):

ngx_int_t ngx_http_core_content_phase(ngx_http_request_t *r, ngx_http_phase_handler_t *ph) if (r- content_handler) { //如果請(qǐng)求對(duì)象的content_handler字段不為空,則調(diào)用 r- write_event_handler = ngx_http_request_empty_handler; ngx_http_finalize_request(r, r- content_handler(r)); return NGX_OK; ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r- connection- log, 0, content phase: %ui , r- phase_handler); rc = ph- handler(r); //否則執(zhí)行內(nèi)容產(chǎn)生階段handler}

總結(jié)

nginx處理HTTP請(qǐng)求的流程較為復(fù)雜,因此本文只是簡(jiǎn)單提供了一條線索:分析了nginx服務(wù)器啟動(dòng)監(jiān)聽的過程,HTTP請(qǐng)求的解析過程,11個(gè)階段的初始化與調(diào)用過程。至于HTTP解析處理的詳細(xì)流程,還需要讀者去探索。

以上就是nginx HTTP處理流程的淺析的詳細(xì)內(nèi)容,PHP教程

鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 鞍山市| 东源县| 上高县| 阿拉善右旗| 恩施市| 若尔盖县| 深圳市| 延川县| 龙江县| 文成县| 雅安市| 上林县| 瓮安县| 石阡县| 喜德县| 连城县| 财经| 唐海县| 通渭县| 白玉县| 洞头县| 德钦县| 泸溪县| 淮滨县| 六盘水市| 达拉特旗| 改则县| 江川县| 八宿县| 凤庆县| 麻城市| 嘉鱼县| 农安县| 平泉县| 石景山区| 盘山县| 广水市| 龙里县| 麻栗坡县| 高碑店市| 霸州市|