Nginx缓存清理

1、相关配置

1
2
3

proxy_cache_path /dev/sda1/data inactive=60m max_size=10G;

proxy_cache_path指令的inactive参数和max_size参数用来维护缓存队列大小

2、源码解析

  1. 首先来看看配置解析函数
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    char *
    ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
    {
    char *confp = conf;

    off_t max_size;
    u_char *last, *p;
    time_t inactive;
    ssize_t size;
    ngx_str_t s, name, *value;
    ngx_int_t loader_files, manager_files;
    ngx_msec_t loader_sleep, manager_sleep, loader_threshold,
    manager_threshold;
    ngx_uint_t i, n, use_temp_path;
    ngx_array_t *caches;
    ngx_http_file_cache_t *cache, **ce;

    cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));
    if (cache == NULL) {
    return NGX_CONF_ERROR;
    }

    cache->path = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
    if (cache->path == NULL) {
    return NGX_CONF_ERROR;
    }

    use_temp_path = 1;

    inactive = 600;

    loader_files = 100;
    loader_sleep = 50;
    loader_threshold = 200;

    manager_files = 100;
    manager_sleep = 50;
    manager_threshold = 200;

    name.len = 0;
    size = 0;
    max_size = NGX_MAX_OFF_T_VALUE;

    value = cf->args->elts;
    //指定路径名称
    cache->path->name = value[1];

    if (cache->path->name.data[cache->path->name.len - 1] == '/') {
    cache->path->name.len--;
    }

    if (ngx_conf_full_name(cf->cycle, &cache->path->name, 0) != NGX_OK) {
    return NGX_CONF_ERROR;
    }

    //解析proxy_cache_path指令后的参数
    for (i = 2; i < cf->args->nelts; i++) {
    //解析目录层级参数
    if (ngx_strncmp(value[i].data, "levels=", 7) == 0) {

    p = value[i].data + 7;
    last = value[i].data + value[i].len;

    for (n = 0; n < NGX_MAX_PATH_LEVEL && p < last; n++) {

    if (*p > '0' && *p < '3') {

    cache->path->level[n] = *p++ - '0';
    cache->path->len += cache->path->level[n] + 1;

    if (p == last) {
    break;
    }

    if (*p++ == ':' && n < NGX_MAX_PATH_LEVEL - 1 && p < last) {
    continue;
    }

    goto invalid_levels;
    }

    goto invalid_levels;
    }

    if (cache->path->len < 10 + NGX_MAX_PATH_LEVEL) {
    continue;
    }

    invalid_levels:

    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid \"levels\" \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    if (ngx_strncmp(value[i].data, "use_temp_path=", 14) == 0) {

    if (ngx_strcmp(&value[i].data[14], "on") == 0) {
    use_temp_path = 1;

    } else if (ngx_strcmp(&value[i].data[14], "off") == 0) {
    use_temp_path = 0;

    } else {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid use_temp_path value \"%V\", "
    "it must be \"on\" or \"off\"",
    &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }
    //解析共享内存名称参数
    if (ngx_strncmp(value[i].data, "keys_zone=", 10) == 0) {

    name.data = value[i].data + 10;

    p = (u_char *) ngx_strchr(name.data, ':');

    if (p == NULL) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid keys zone size \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    name.len = p - name.data;

    s.data = p + 1;
    s.len = value[i].data + value[i].len - s.data;

    size = ngx_parse_size(&s);

    if (size == NGX_ERROR) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid keys zone size \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    if (size < (ssize_t) (2 * ngx_pagesize)) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "keys zone \"%V\" is too small", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }
    //解析缓存分片活跃时间
    if (ngx_strncmp(value[i].data, "inactive=", 9) == 0) {

    s.len = value[i].len - 9;
    s.data = value[i].data + 9;

    inactive = ngx_parse_time(&s, 1);
    if (inactive == (time_t) NGX_ERROR) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid inactive value \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }
    //解析配置的目录最大能保存多少缓存分片
    if (ngx_strncmp(value[i].data, "max_size=", 9) == 0) {

    s.len = value[i].len - 9;
    s.data = value[i].data + 9;

    max_size = ngx_parse_offset(&s);
    if (max_size < 0) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid max_size value \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }
    //解析缓存加载进程暂停阈值
    if (ngx_strncmp(value[i].data, "loader_files=", 13) == 0) {

    loader_files = ngx_atoi(value[i].data + 13, value[i].len - 13);
    if (loader_files == NGX_ERROR) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid loader_files value \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }
    //解析缓存加载进程暂停时间
    if (ngx_strncmp(value[i].data, "loader_sleep=", 13) == 0) {

    s.len = value[i].len - 13;
    s.data = value[i].data + 13;

    loader_sleep = ngx_parse_time(&s, 0);
    if (loader_sleep == (ngx_msec_t) NGX_ERROR) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid loader_sleep value \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }

    if (ngx_strncmp(value[i].data, "loader_threshold=", 17) == 0) {

    s.len = value[i].len - 17;
    s.data = value[i].data + 17;

    loader_threshold = ngx_parse_time(&s, 0);
    if (loader_threshold == (ngx_msec_t) NGX_ERROR) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid loader_threshold value \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }
    //解析缓存管理进程暂停阈值
    if (ngx_strncmp(value[i].data, "manager_files=", 14) == 0) {

    manager_files = ngx_atoi(value[i].data + 14, value[i].len - 14);
    if (manager_files == NGX_ERROR) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid manager_files value \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }

    //manager进程达到阈值后的睡眠时间
    if (ngx_strncmp(value[i].data, "manager_sleep=", 14) == 0) {

    s.len = value[i].len - 14;
    s.data = value[i].data + 14;

    manager_sleep = ngx_parse_time(&s, 0);
    if (manager_sleep == (ngx_msec_t) NGX_ERROR) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid manager_sleep value \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }

    if (ngx_strncmp(value[i].data, "manager_threshold=", 18) == 0) {

    s.len = value[i].len - 18;
    s.data = value[i].data + 18;

    manager_threshold = ngx_parse_time(&s, 0);
    if (manager_threshold == (ngx_msec_t) NGX_ERROR) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid manager_threshold value \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    continue;
    }

    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "invalid parameter \"%V\"", &value[i]);
    return NGX_CONF_ERROR;
    }

    if (name.len == 0 || size == 0) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "\"%V\" must have \"keys_zone\" parameter",
    &cmd->name);
    return NGX_CONF_ERROR;
    }

    //设置loader跟manager进程的回调函数
    cache->path->manager = ngx_http_file_cache_manager;
    cache->path->loader = ngx_http_file_cache_loader;
    cache->path->data = cache;
    cache->path->conf_file = cf->conf_file->file.name.data;
    cache->path->line = cf->conf_file->line;
    cache->loader_files = loader_files;
    cache->loader_sleep = loader_sleep;
    cache->loader_threshold = loader_threshold;
    cache->manager_files = manager_files;
    cache->manager_sleep = manager_sleep;
    cache->manager_threshold = manager_threshold;
    //将缓存路径添加到全局管理
    if (ngx_add_path(cf, &cache->path) != NGX_OK) {
    return NGX_CONF_ERROR;
    }
    //添加共享内存名称
    cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
    if (cache->shm_zone == NULL) {
    return NGX_CONF_ERROR;
    }

    if (cache->shm_zone->data) {
    ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
    "duplicate zone \"%V\"", &name);
    return NGX_CONF_ERROR;
    }

    //设置共享内存初始化函数指针
    cache->shm_zone->init = ngx_http_file_cache_init;
    cache->shm_zone->data = cache;

    cache->use_temp_path = use_temp_path;

    cache->inactive = inactive;
    cache->max_size = max_size;

    caches = (ngx_array_t *) (confp + cmd->offset);
    //保存cache结构体
    ce = ngx_array_push(caches);
    if (ce == NULL) {
    return NGX_CONF_ERROR;
    }

    *ce = cache;

    return NGX_CONF_OK;
    }
  1. 上述代码中我们只关注inactive,max_size参数解析、cache->path相关成员赋值以及loader和manager相关的赋值代码,如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46

cache->path->manager = ngx_http_file_cache_manager;
cache->path->loader = ngx_http_file_cache_loader;
cache->path->data = cache;
cache->path->conf_file = cf->conf_file->file.name.data;
cache->path->line = cf->conf_file->line;
cache->loader_files = loader_files;
cache->loader_sleep = loader_sleep;
cache->loader_threshold = loader_threshold;
cache->manager_files = manager_files;
cache->manager_sleep = manager_sleep;
cache->manager_threshold = manager_threshold;

if (ngx_add_path(cf, &cache->path) != NGX_OK) {
return NGX_CONF_ERROR;
}
//添加共享内存名称
cache->shm_zone = ngx_shared_memory_add(cf, &name, size, cmd->post);
if (cache->shm_zone == NULL) {
return NGX_CONF_ERROR;
}

if (cache->shm_zone->data) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"duplicate zone \"%V\"", &name);
return NGX_CONF_ERROR;
}

//设置共享内存初始化函数
cache->shm_zone->init = ngx_http_file_cache_init;
cache->shm_zone->data = cache;

cache->use_temp_path = use_temp_path;

cache->inactive = inactive;
cache->max_size = max_size;
//保存cache
caches = (ngx_array_t *) (confp + cmd->offset);

ce = ngx_array_push(caches);
if (ce == NULL) {
return NGX_CONF_ERROR;
}

*ce = cache;

  1. path结构体初始化完成之后,ngx_add_path函数将配置的路径添加到nginx全局路径管理器后,配置解析完成之后会统一管理,接下来我们来看看几个跟目录相关的回调函数。
1
2
3
4
5

ngx_http_file_cache_manager
//加载本地已有缓存函数
ngx_http_file_cache_loader

  1. 首先来看看ngx_http_file_cache_manager函数的具体实现
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76

static ngx_msec_t
ngx_http_file_cache_manager(void *data)
{
ngx_http_file_cache_t *cache = data;

off_t size;
time_t wait;
ngx_msec_t elapsed, next;
ngx_uint_t count, watermark;

cache->last = ngx_current_msec;
cache->files = 0;
//查找过期缓存结点
next = (ngx_msec_t) ngx_http_file_cache_expire(cache) * 1000;

if (next == 0) {
next = cache->manager_sleep;
goto done;
}

for ( ;; ) {
ngx_shmtx_lock(&cache->shpool->mutex);

size = cache->sh->size;
count = cache->sh->count;
watermark = cache->sh->watermark;

ngx_shmtx_unlock(&cache->shpool->mutex);

ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"http file cache size: %O c:%ui w:%i",
size, count, (ngx_int_t) watermark);

if (size < cache->max_size && count < watermark) {
break;
}
//处理缓存目录已经满了的情况
wait = ngx_http_file_cache_forced_expire(cache);

if (wait > 0) {
next = (ngx_msec_t) wait * 1000;
break;
}

if (ngx_quit || ngx_terminate) {
break;
}

if (++cache->files >= cache->manager_files) {
next = cache->manager_sleep;
break;
}

ngx_time_update();

elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));

if (elapsed >= cache->manager_threshold) {
next = cache->manager_sleep;
break;
}
}

done:

elapsed = ngx_abs((ngx_msec_int_t) (ngx_current_msec - cache->last));

ngx_log_debug3(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
"http file cache manager: %ui e:%M n:%M",
cache->files, elapsed, next);

return next;
}


  1. 这个函数主要的功能就是遍历缓存结点数,找出过期结点,并删除,接下来看看ngx_http_file_cache_loader函数实现
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
     
    static void
    ngx_http_file_cache_loader(void *data)
    {
    ngx_http_file_cache_t *cache = data;

    ngx_tree_ctx_t tree;

    if (!cache->sh->cold || cache->sh->loading) {
    return;
    }

    if (!ngx_atomic_cmp_set(&cache->sh->loading, 0, ngx_pid)) {
    return;
    }

    ngx_log_debug0(NGX_LOG_DEBUG_HTTP, ngx_cycle->log, 0,
    "http file cache loader");
    //设置遍历目录时所需的函数指针
    tree.init_handler = NULL;
    //遍历目录时,遇到文件处理函数
    tree.file_handler = ngx_http_file_cache_manage_file;
    //遍历目录时,遇到文件处理函数
    tree.pre_tree_handler = ngx_http_file_cache_manage_directory;
    tree.post_tree_handler = ngx_http_file_cache_noop;
    //缓存目录中有不符合规则的缓存文件,会删除文件
    tree.spec_handler = ngx_http_file_cache_delete_file;
    tree.data = cache;
    tree.alloc = 0;
    tree.log = ngx_cycle->log;

    cache->last = ngx_current_msec;
    cache->files = 0;
    //遍历配置路径下所有文件
    if (ngx_walk_tree(&tree, &cache->path->name) == NGX_ABORT) {
    cache->sh->loading = 0;
    return;
    }

    cache->sh->cold = 0;
    cache->sh->loading = 0;

    ngx_log_error(NGX_LOG_NOTICE, ngx_cycle->log, 0,
    "http file cache: %V %.3fM, bsize: %uz",
    &cache->path->name,
    ((double) cache->sh->size * cache->bsize) / (1024 * 1024),
    cache->bsize);
    }

    >6. 此函数功能就是绑定回调函数,然后遍历配置的目录,根据目录中不同的对象调用不同的回调函数

3、总结

以上就是nginx缓存管理的主要代码,总的来说就是在解析配置时设置ngx_path_t结构体的回调函数,cache_manager进程定时启动扫描LRU队列,删除过期结点