Hatena::Groupxn--272ax3f

nginx輪講#5

nginx輪講#5

プレゼンテーション

nginx輪講#5

(gdb) bt

#0 ngx_http_close_request (r=0x6b3340, rc=0) at src/http/ngx_http_request.c:2980

#1 0x000000000042e9f0 in ngx_http_finalize_connection (r=0x6b3340) at src/http/ngx_http_request.c:2185

#2 0x000000000042faed in ngx_http_finalize_request (r=<value optimized out>, rc=<value optimized out>) at src/http/ngx_http_request.c:2102

#3 0x000000000042a843 in ngx_http_core_content_phase (r=0x6b3340, ph=0x6bb2b8) at src/http/ngx_http_core_module.c:1397

#4 0x0000000000425e53 in ngx_http_core_run_phases (r=0x6b3340) at src/http/ngx_http_core_module.c:877

#5 0x0000000000425f50 in ngx_http_handler (r=0x6b3340) at src/http/ngx_http_core_module.c:860

#6 0x00000000004297ca in ngx_http_internal_redirect (r=0x6b3340, uri=<value optimized out>, args=<value optimized out>) at src/http/ngx_http_core_module.c:2536

#7 0x0000000000442c47 in ngx_http_index_handler (r=0x6b3340) at src/http/modules/ngx_http_index_module.c:277

#8 0x000000000042a832 in ngx_http_core_content_phase (r=0x6b3340, ph=0x6bb288) at src/http/ngx_http_core_module.c:1394

#9 0x0000000000425e53 in ngx_http_core_run_phases (r=0x6b3340) at src/http/ngx_http_core_module.c:877

#10 0x0000000000425f50 in ngx_http_handler (r=0x6b3340) at src/http/ngx_http_core_module.c:860

#11 0x00000000004301ea in ngx_http_process_request (r=0x6b3340) at src/http/ngx_http_request.c:1668

#12 0x0000000000430aba in ngx_http_process_request_headers (rev=0x6a4ae0) at src/http/ngx_http_request.c:1114

#13 0x0000000000431090 in ngx_http_process_request_line (rev=0x6bd238) at src/http/ngx_http_request.c:914

#14 0x000000000042da2c in ngx_http_init_request (rev=0x6bd238) at src/http/ngx_http_request.c:519

#15 0x0000000000422c56 in ngx_epoll_process_events (cycle=0x69ed10, timer=<value optimized out>, flags=<value optimized out>)

at src/event/modules/ngx_epoll_module.c:679

#16 0x000000000041a4a2 in ngx_process_events_and_timers (cycle=0x69ed10) at src/event/ngx_event.c:246

#17 0x000000000042062a in ngx_single_process_cycle (cycle=0x69ed10) at src/os/unix/ngx_process_cycle.c:316

#18 0x00000000004043cb in main (argc=47, argv=0x69e0c0) at src/core/nginx.c:407

今日のきも

#11 ngx_http_process_request at src/http/ngx_http_request.c:1668

#12 ngx_http_process_request_headers at src/http/ngx_http_request.c:1114

#13 ngx_http_process_request_line src/http/ngx_http_request.c:914

#14 ngx_http_init_request at src/http/ngx_http_request.c:519


ngx_http_process_request_line src/http/ngx_http_request.c:914

  • http リクエストを一行ずつ読んでループしてるぽい
  • parse に成功すると rc == NGX_OK になる
  • まだ続く場合は rc == NGX_AGAIN
  • src/http/ngx_http_request.c:731〜
    /* ssize_t                    n;
       ngx_int_t                  rc, rv; */
    rc = NGX_AGAIN;

    for ( ;; ) {
        if (rc == NGX_AGAIN) {
            n = ngx_http_read_request_header(r);
            if (n == NGX_AGAIN || n == NGX_ERROR) {
                return;
            }
        }
        rc = ngx_http_parse_request_line(r, r->header_in);

ngx_http_read_request_header(r);

ngx_http_read_request_header(ngx_http_request_t *r)
{
    ssize_t                    n;

    n = r->header_in->last - r->header_in->pos;

    if (n > 0) {
        return n;
    }
  • header読み残しを返すだけ

ngx_http_parse_request_line(r, r->header_in);

  • ngx_http_parse.c
    return NGX_AGAIN;
done:
    ...
    return NGX_OK;
header_done:
    ...
    return NGX_HTTP_PARSE_HEADER_DONE;

ngx_http_request.c 745 以下

  • リクエストの構造化は終わってる
  • 構造化データを見ながら条件振り分け
    • win32
    • complex_uri quoted_uri
    • HTTP 0.9
      • ngx_http_process_request(r);
  • 問題なければ ngx_http_process_request_headers(rev);

ngx_http_process_request_headers(rev);

  • ngx_http_request.c 同じファイル
    rc = NGX_AGAIN;
    for ( ;; ) {
        if (rc == NGX_AGAIN) {
            ...
            n = ngx_http_read_request_header(r);
            if (n == NGX_AGAIN || n == NGX_ERROR) {
                return;
            }
        }
        rc = ngx_http_parse_header_line(r, r->header_in,
                                        cscf->underscores_in_headers);
        if (rc == NGX_OK) {
            ...
            また、構造化したり...
            continue;
        }

        if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
            ....
            ngx_http_process_request(r);

ngx_http_process_request(r);

  • ngx_http_request.c 1595
  • HTTPS の 処理を場合分け
    • 今回は飛ばす
  • ngx_http_handler(r);
  • ngx_http_run_posted_requests(c);

ngx_http_handler(r);

  • ngx_http_core_module.c 821
  • keepalive かチェック
  • ngx_http_core_run_phases(r);

ngx_http_core_run_phases(r);

  • ngx_http_core_module.c 865
    cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
    ph = cmcf->phase_engine.handlers;
    while (ph[r->phase_handler].checker) {
        rc = ph[r->phase_handler].checker(r, &ph[r->phase_handler]);

phase で色々チェック & 返却

  • ngx_http_core_module.c
    • 各faseの実装あり

ngx_http_process_request(r);

  • ngx_http_request.c 1595
    • ngx_http_run_posted_requests(c);
    • ngx_http_request.c 1856