nginx中生成cache_key的过程

来源:互联网 发布:个人展示网站源码 编辑:程序博客网 时间:2024/06/06 03:07
(gdb) bt#0  ngx_http_file_cache_create_key (r=0x93bb1f8) at src/http/ngx_http_file_cache.c:246#1  0x0809c7a4 in ngx_http_upstream_cache (r=0x93bb1f8, u=0x93bb9cc) at src/http/ngx_http_upstream.c:799#2  0x0809bf9a in ngx_http_upstream_init_request (r=0x93bb1f8) at src/http/ngx_http_upstream.c:536#3  0x0809bf45 in ngx_http_upstream_init (r=0x93bb1f8) at src/http/ngx_http_upstream.c:509#4  0x08093478 in ngx_http_read_client_request_body (r=0x93bb1f8, post_handler=0x809beb6 <ngx_http_upstream_init>)    at src/http/ngx_http_request_body.c:89#5  0x080c67d6 in ngx_http_proxy_handler (r=0x93bb1f8) at src/http/modules/ngx_http_proxy_module.c:913#6  0x08080f5e in ngx_http_core_content_phase (r=0x93bb1f8, ph=0x93ddd74) at src/http/ngx_http_core_module.c:1363#7  0x08080155 in ngx_http_core_run_phases (r=0x93bb1f8) at src/http/ngx_http_core_module.c:840#8  0x080800e7 in ngx_http_handler (r=0x93bb1f8) at src/http/ngx_http_core_module.c:823#9  0x0808b640 in ngx_http_process_request (r=0x93bb1f8) at src/http/ngx_http_request.c:1911#10 0x0808a6eb in ngx_http_process_request_headers (rev=0x78af0068) at src/http/ngx_http_request.c:1342#11 0x08089e25 in ngx_http_process_request_line (rev=0x78af0068) at src/http/ngx_http_request.c:1022#12 0x0808d1a7 in ngx_http_keepalive_handler (rev=0x78af0068) at src/http/ngx_http_request.c:3196#13 0x0807c055 in ngx_epoll_process_events (cycle=0x93bd618, timer=65000, flags=1) at src/event/modules/ngx_epoll_module.c:822#14 0x08071cf4 in ngx_process_events_and_timers (cycle=0x93bd618) at src/event/ngx_event.c:242#15 0x0807a6f6 in ngx_worker_process_cycle (cycle=0x93bd618, data=0x0) at src/os/unix/ngx_process_cycle.c:753#16 0x080780dd in ngx_spawn_process (cycle=0x93bd618, proc=0x807a65c <ngx_worker_process_cycle>, data=0x0, name=0x80f13e8 "worker process",     respawn=-4) at src/os/unix/ngx_process.c:198#17 0x08079cad in ngx_start_worker_processes (cycle=0x93bd618, n=1, type=-4) at src/os/unix/ngx_process_cycle.c:358#18 0x08079925 in ngx_master_process_cycle (cycle=0x93bd618) at src/os/unix/ngx_process_cycle.c:243#19 0x08051516 in main (argc=1, argv=0xbfc0e9b4) at src/core/nginx.c:367



下面看下cache_key是怎么生成的:


ngx_http_file_cache.c:


voidngx_http_file_cache_create_key(ngx_http_request_t *r){    size_t             len;    ngx_str_t         *key;    ngx_uint_t         i;    ngx_md5_t          md5;    ngx_http_cache_t  *c;    c = r->cache;    len = 0;    ngx_crc32_init(c->crc32);    ngx_md5_init(&md5);    key = c->keys.elts;    for (i = 0; i < c->keys.nelts; i++) {        ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,                       "http cache key: \"%V\"", &key[i]);        len += key[i].len;        ngx_crc32_update(&c->crc32, key[i].data, key[i].len);        ngx_md5_update(&md5, key[i].data, key[i].len);    }    c->header_start = sizeof(ngx_http_file_cache_header_t)                      + sizeof(ngx_http_file_cache_key) + len + 1;    ngx_crc32_final(c->crc32);    ngx_md5_final(c->key, &md5);    ngx_memcpy(c->main, c->key, NGX_HTTP_CACHE_KEY_LEN);}

调试下key中存储的内容


(gdb) p key[0]$54 = {  len = 18,  data = 0x93d84df "http://domain_name"}


(gdb) p key[1]$55 = {  len = 32,  data = 0x93c172c "/view/1/services.html?name=James HTTP/1.1\r\nHost"}

(gdb) p c->keys.nelts$56 = 2

注意,此cache使用的是poxy_cache, 而非memcached, 在key中,存储了两个字符串的数组,一个是域名<http://domain_name>,一个是根目录之后的访问目录以及参数</view/1/services.html?name=James HTTP/1.1\r\nHost>




0 0