nginx原始碼分析--ngx_http_optimize_servers()函數

來源:互聯網
上載者:User

標籤:

這個函數做了連部分工作:1)以port為進入點 將實用的資訊存放到hash表內 2)調用ngx_http_init_listening()函數 對port進行監聽

1、 在ngx_http_core_main_conf_t結構體中有一個欄位為ports,是一個數組,數組記憶體放的全是ngx_http_conf_port_t;對於每個連接埠資訊(ngx_http_conf_port_t),調用

ngx_http_server_names函數,同一時候也調用ngx_http_init_listening函數,這裡先分析ngx_http_server_names函數。

2)對於每個連接埠資訊(ngx_http_conf_port_t),裡面有一個欄位為addrs,這個欄位是一個數組,這個數組記憶體放的全是地址資訊(ngx_http_conf_addr_t),一個地址資訊

(ngx_http_conf_addr_t)相應著多個server{}配置塊(ngx_http_core_srv_conf_t)3

3)對於每個server{}配置塊資訊(ngx_http_core_srv_conf_t)相應著非常多個sever_name.所以,ngx_http_core_srv_conf_t結構體中有一個數組server_names,成員是ngx_http_server_name_t,這樣對每個server_name(ngx_http_server_name_t)進行操作,存放到hash表內

這樣就完畢了以連接埠(ngx_http_conf_port_t)為進入點 對全部的地址都進行遍曆,將全部的server_name都存放到hash表內


二、ngx_http_init_listening()函數調用

      在ngx_http_core_main_conf_t結構體中存放著一個Port資訊的數組,對於每個port進行ngx_http_init_listening 的操作,對每個ngx_http_conf_addr_t資訊調用

ngx_http_add_listening()函數,為何每個port相應非常多個地址呢?也就是說非常多歌IP地址都在這個port上監聽著。

    在ngx_http_add_listening函數調用ngx_create_listening,相應一個地址資訊產生一個ngx_listening_t的結構體,最最重要的是這個監聽通訊端上的回調是ngx_http_init_connection函數,這個一個監聽通訊端上接收到HTTP請求成功後(accept成功後 建立新的串連後,調用accept串連時在函數ngx_event_accept函數中,這個函數最為一個可讀事件上的回調,非常顯然,這個ngx_listening_t結構體上包括的一個事件結構體上的回調,只是這個注冊是在ngx_event_process_init函數,這個函數作為ngx_event_core_module中的init_porcess回調,這個回調是在ngx_worker_process_cycle中被調用,作為worker進程的初始化,也就是說,worker進程初始化的時候才將監聽通訊端上的可讀事件的回調設定為ngx_event_accep,在ngx_event_accept函數中完畢accept後,直接調用監聽通訊端上的handler,也就是ngx_http_init_connection函數)開始的。ngx_http_init_connection函數讓http請求進入到HTTP的11個階段。


nginx原始碼分析--ngx_http_optimize_servers()函數

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.