最新消息:

nginx 缓存文件添加后缀名

nginx admin 3908浏览 0评论

修改nginx代码在proxy_cache_path 添加suffix=$var 支持缓存文件后缀名。

配置实例:

proxy_cachepath ./cache levels=1:2 suffix=$server_name keys_zone=test:10m max_size=1g;

测试:

./cache/m/co/d0431adbaa72e1b38e0884b71c9d30be_www.test.com
diff --git a/src/http/ngx_http_cache.h b/src/http/ngx_http_cache.h
index f9e96640..1a1a6420 100644
--- a/src/http/ngx_http_cache.h
+++ b/src/http/ngx_http_cache.h
@@ -160,6 +160,8 @@ struct ngx_http_file_cache_s {

     ngx_path_t                      *path;

+    ngx_http_complex_value_t         suffix;
+
     off_t                            max_size;
     size_t                           bsize;

diff --git a/src/http/ngx_http_file_cache.c b/src/http/ngx_http_file_cache.c
index 3b2b68a2..808092de 100644
--- a/src/http/ngx_http_file_cache.c
+++ b/src/http/ngx_http_file_cache.c
@@ -933,6 +933,7 @@ static ngx_int_t
 ngx_http_file_cache_name(ngx_http_request_t *r, ngx_path_t *path)
 {
     u_char            *p;
+    ngx_str_t          suffix;
     ngx_http_cache_t  *c;

     c = r->cache;
@@ -941,8 +942,17 @@ ngx_http_file_cache_name(ngx_http_request_t *r, ngx_path_t *path)
         return NGX_OK;
     }

+    suffix.len = 0;
+    if (c->file_cache->suffix.value.len > 0) {
+        if (ngx_http_complex_value(r, &c->file_cache->suffix, &suffix)
+            != NGX_OK)
+        {
+            return NGX_ERROR;
+        }
+    }
+
     c->file.name.len = path->name.len + 1 + path->len
-                       + 2 * NGX_HTTP_CACHE_KEY_LEN;
+                       + 2 * NGX_HTTP_CACHE_KEY_LEN + suffix.len;

     c->file.name.data = ngx_pnalloc(r->pool, c->file.name.len + 1);
     if (c->file.name.data == NULL) {
@@ -953,6 +963,12 @@ ngx_http_file_cache_name(ngx_http_request_t *r, ngx_path_t *path)

     p = c->file.name.data + path->name.len + 1 + path->len;
     p = ngx_hex_dump(p, c->key, NGX_HTTP_CACHE_KEY_LEN);
+
+    if (suffix.len > 0) {
+        ngx_memcpy(p, suffix.data, suffix.len);
+        p += suffix.len;
+    }
+
     *p = '\0';

     ngx_create_hashed_filename(path, c->file.name.data, c->file.name.len);
@@ -2308,7 +2324,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
     u_char                 *last, *p;
     time_t                  inactive;
     ssize_t                 size;
-    ngx_str_t               s, name, *value;
+    ngx_str_t               s, suffix, name, *value;
     ngx_int_t               loader_files, manager_files;
     ngx_msec_t              loader_sleep, manager_sleep, loader_threshold,
                             manager_threshold;
@@ -2316,6 +2332,8 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
     ngx_array_t            *caches;
     ngx_http_file_cache_t  *cache, **ce;

+    ngx_http_compile_complex_value_t   ccv;
+
     cache = ngx_pcalloc(cf->pool, sizeof(ngx_http_file_cache_t));
     if (cache == NULL) {
         return NGX_CONF_ERROR;
@@ -2338,6 +2356,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
     manager_sleep = 50;
     manager_threshold = 200;

+    suffix.len = 0;
     name.len = 0;
     size = 0;
     max_size = NGX_MAX_OFF_T_VALUE;
@@ -2393,6 +2412,14 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
             return NGX_CONF_ERROR;
         }

+        if (ngx_strncmp(value[i].data, "suffix=", 7) == 0) {
+
+            suffix.data = value[i].data + 7;
+            suffix.len = value[i].len - 7;
+
+            continue;
+        }
+
         if (ngx_strncmp(value[i].data, "use_temp_path=", 14) == 0) {

             if (ngx_strcmp(&value[i].data[14], "on") == 0) {
@@ -2599,6 +2626,21 @@ ngx_http_file_cache_set_slot(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
     cache->inactive = inactive;
     cache->max_size = max_size;

+    // suffix
+    if (suffix.len > 0) {
+        ngx_memzero(&ccv, sizeof(ngx_http_compile_complex_value_t));
+
+        ccv.cf = cf;
+        ccv.value = &suffix;
+        ccv.complex_value = &cache->suffix;
+
+        if (ngx_http_compile_complex_value(&ccv) != NGX_OK) {
+            ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "error suffix \"%V\"", &suffix);
+            return NGX_CONF_ERROR;
+        }
+    }
+
     caches = (ngx_array_t *) (confp + cmd->offset);

     ce = ngx_array_push(caches);

https://github.com/vislee/leevis.com/issues/162

转载请注明:爱开源 » nginx 缓存文件添加后缀名

您必须 登录 才能发表评论!