Nginx源碼分析之開篇
作者:那一劍的風情
Nginx源碼構思精巧,每每閱讀頗有收獲,寫此文與各碼農分享。閱讀從main開始,流程非常清晰。
Nginx源碼構思精巧,每每閱讀頗有收獲,寫此文與各碼農分享。
閱讀從main開始,流程非常清晰。
- /* 系統錯誤初始化,將構建ngx_sys_errlist */
- ngx_strerror_init();
- /* 選項處理 */
- ngx_get_options(argc, argv);
- /* 時間初始化, 當前時間:ngx_current_msec */
- ngx_time_init();
- /* 日志初始化 */
- log = ngx_log_init(ngx_prefix);
- /* 選項處理 */
- ngx_save_argv(..., argc, argv);
- ngx_process_options(...);
- /* 操作系統初始化處理 */
- ngx_os_init(log);
- /* 模塊點名, ngx_modules代表所有模塊,是個數組 */
- ngx_max_module = 0;
- for (i = 0; ngx_modules[i]; i++) {
- ngx_modules[i]->index = ngx_max_module++;
- }
- /*
- * 系統初始化,這里將發生配置文件解析,模塊上下文注冊鉤子調用,模塊初始化
- * module : 模塊
- * commands : 模塊指令集,負責解析配置文件的選項,一個指令對應一個配置選項
- * conf : 模塊配置結構體,指令解析后的值就是存儲在這個里面,每個模塊都有自已的一個conf
- * ctx : 模塊上下文,有四種,core, event, http, mail,有注冊鉤子功能。比如 create conf, init conf
- */
- cycle = ngx_init_cycle(&init_cycle);
- /* 創建進程id文件 */
- ngx_create_pidfile(&ccf->pid, cycle->log);
- /*
- * 進程處理
- * 主進程(master)產生多個工作進程(worker)
- * 這里將做各模塊進程初始化,監聽,接受,請求處理,還有信號等
- */
- ngx_master_process_cycle(cycle) {
- ngx_start_worker_processes(cycle, ccf->worker_processes,
- NGX_PROCESS_RESPAWN) {
- for ( ... ) {
- ngx_spawn_process(cycle, ngx_worker_process_cycle, NULL,
- "worker process", type);
- }
- }
- }
- goto: 繼續
- /* 生產進程都在這個函數里處理 */
- ngx_worker_process_cycle {
- /* 各模塊進程注冊鉤子調用 */
- ngx_worker_process_init();
- for ( ;; ) {
- ngx_process_events_and_timers(cycle);
- }
- }
- goto: 繼續
- ngx_process_events_and_timers(cycle) {
- /*
- * 定時器,用紅黑樹實現,這里找出某個event(事件)的超時時間
- * 每個事件在紅黑樹里的key的值為:ngx_currnet_msec + 超時時間(默認60s)
- * timer的值為 -1 (如果沒有事件),或 ngx_current_msec - key
- */
- timer = ngx_event_find_timer();
- /* epoll 機制,這里將做 epoll_wait(..., timer); */
- ngx_process_events(cycle, timer, flags);
- /* 先處理所有可能超時的事件,如果超時,將event的timedout設為1,并且馬上event->handler(ev); */
- ngx_event_expire_timers();
- /* 事件存隊列方式,開始遍歷,調用 */
- ngx_event_process_posted(cycle, &ngx_posted_events);
- }
- /*
- * 幾個重要的結構體
- * ngx_listening_t : 監聽套接字的結構體,比如地址,端口等
- * ngx_connection_t : 每個socket將對應一個connection,意為連接,里面存著 fd,read(讀事件),
- * write(寫事件) 等。
- * ngx_event_t : 事件結構體,有個重要的函數指針handler,fd事件被驅動時,將調用這個函數。
- * 它有幾個重要成員
- * timer_set : 每個event在epoll_wait前,要先進入定時器紅黑樹,這個標記就是
- * 標記是否在定時器里, 超時處理用的.
- * active : 當ngx_add_event里(添加或更新事件) 進入epoll時,會置為1.
- * ready : 進入事件隊列里,將置為1,只有為1,它對應的socket fd才可以讀
- * timedout : 此事件對應的socket fd將視為超時
- */
- /*
- * http處理
- * 當 listen fd 有連接過來時,它將調用函數 ngx_http_init_connection
- * 當 accept fd 有傳送東東時,它將調用函數 ngx_http_init_request,所以的處理都將從這函數開始
- */
- ngx_http_init_request {
- ngx_http_process_request_line {
- /* 讀請求頭 */
- ngx_http_read_request_header(...);
- /* 解析請求行 */
- ngx_http_parse_request_line(...);
- /* 處理請求頭部信息 */
- ngx_http_process_request_headers(...) {
- for ( ;; ) {
- /* 解析每一行 */
- ngx_http_parse_header_line(...);
- }
- /* 解析之后對所有行的處理 */
- ngx_http_process_request_header(...);
- /* 真正開始處理請求 */
- ngx_http_process_request(r) {
- ngx_http_handler(r) {
- /* 非常巧妙的設計處理即將開始 */
- ngx_http_core_run_phases(r);
- }
- }
- }
- }
- }
- /*
- * 精巧的設計函數:責任鏈模式
- * http的每個請求可以分為好幾個階段
- * 規則重寫(rewrite)
- * 處理配置(不同url有不同的配置)
- * 權限訪問處理
- * 核心內容處理(是走fastcgi,還是直接輸出或從緩存獲取等)
- * 日志處理
- *
- * 每個階段都可以由好幾個模塊處理,這些模塊組成一個鏈,
- * 這是設計模式里的一種,責任鏈模式
- */
- ngx_http_core_run_phases(r) {
- while (ph[r->phase_handler].checker) {
- rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);
- if (rc == NGX_OK) {
- return;
- }
- }
- }
責任編輯:林師授
來源:
OSCHINA