[nginx] Upstream: use_temp_path parameter of proxy_cache_path an...

Valentin Bartenev vbart at nginx.com
Fri Dec 26 13:24:01 UTC 2014


details:   http://hg.nginx.org/nginx/rev/e9effef98874
branches:  
changeset: 5960:e9effef98874
user:      Valentin Bartenev <vbart at nginx.com>
date:      Fri Dec 26 16:22:59 2014 +0300
description:
Upstream: use_temp_path parameter of proxy_cache_path and friends.

When set to "off", temporary files for cacheable responses will be stored
inside cache directory.

diffstat:

 src/http/ngx_http_cache.h      |   3 +++
 src/http/ngx_http_file_cache.c |  36 +++++++++++++++++++++++++++++++++++-
 src/http/ngx_http_upstream.c   |   8 ++++++++
 3 files changed, 46 insertions(+), 1 deletions(-)

diffs (105 lines):

diff -r f7584d7c0ccb -r e9effef98874 src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h	Fri Dec 26 16:22:56 2014 +0300
+++ b/src/http/ngx_http_cache.h	Fri Dec 26 16:22:59 2014 +0300
@@ -155,6 +155,9 @@ struct ngx_http_file_cache_s {
     ngx_msec_t                       loader_threshold;
 
     ngx_shm_zone_t                  *shm_zone;
+
+    ngx_uint_t                       use_temp_path;
+                                     /* unsigned use_temp_path:1 */
 };
 
 
diff -r f7584d7c0ccb -r e9effef98874 src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Fri Dec 26 16:22:56 2014 +0300
+++ b/src/http/ngx_http_file_cache.c	Fri Dec 26 16:22:59 2014 +0300
@@ -1935,6 +1935,17 @@ ngx_http_file_cache_add_file(ngx_tree_ct
         return NGX_ERROR;
     }
 
+    /*
+     * Temporary files in cache have a suffix consisting of a dot
+     * followed by 10 digits.
+     */
+
+    if (name->len >= 2 * NGX_HTTP_CACHE_KEY_LEN + 1 + 10
+        && name->data[name->len - 10 - 1] == '.')
+    {
+        return NGX_OK;
+    }
+
     if (ctx->size < (off_t) sizeof(ngx_http_file_cache_header_t)) {
         ngx_log_error(NGX_LOG_CRIT, ctx->log, 0,
                       "cache file \"%s\" is too small", name->data);
@@ -2063,7 +2074,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
     ngx_str_t               s, name, *value;
     ngx_int_t               loader_files;
     ngx_msec_t              loader_sleep, loader_threshold;
-    ngx_uint_t              i, n;
+    ngx_uint_t              i, n, use_temp_path;
     ngx_array_t            *caches;
     ngx_http_file_cache_t  *cache, **ce;
 
@@ -2077,6 +2088,8 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
         return NGX_CONF_ERROR;
     }
 
+    use_temp_path = 1;
+
     inactive = 600;
     loader_files = 100;
     loader_sleep = 50;
@@ -2137,6 +2150,25 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
             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;
@@ -2274,6 +2306,8 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
     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;
 
diff -r f7584d7c0ccb -r e9effef98874 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Fri Dec 26 16:22:56 2014 +0300
+++ b/src/http/ngx_http_upstream.c	Fri Dec 26 16:22:59 2014 +0300
@@ -2677,6 +2677,14 @@ ngx_http_upstream_send_response(ngx_http
     if (p->cacheable) {
         p->temp_file->persistent = 1;
 
+#if (NGX_HTTP_CACHE)
+        if (r->cache && !r->cache->file_cache->use_temp_path) {
+            p->temp_file->file.name = r->cache->file.name;
+            p->temp_file->path = r->cache->file_cache->path;
+            p->temp_file->prefix = 1;
+        }
+#endif
+
     } else {
         p->temp_file->log_level = NGX_LOG_WARN;
         p->temp_file->warn = "an upstream response is buffered "



More information about the nginx-devel mailing list