[PATCH] Upstream: add use_temp_path=tmp to proxy_cache_path and friends

Piotr Sikora piotr at cloudflare.com
Tue Jan 6 03:15:52 UTC 2015


# HG changeset patch
# User Piotr Sikora <piotr at cloudflare.com>
# Date 1420514028 28800
#      Mon Jan 05 19:13:48 2015 -0800
# Node ID e7596cd5b480c9cbabe583a8f47301e32fdf179a
# Parent  e9effef98874c619326ec11e25b11333225cf797
Upstream: add use_temp_path=tmp to proxy_cache_path and friends.

When set to "tmp", store temporary files in "tmp" directory inside cache
path.

The advantage of this solution is that temporary files are stored inside
cache path (same as with "off"), which makes path suitable to be used in
multi-disks setups, but at the same time they aren't mixed with complete
files, which makes it easier to manage them.

Signed-off-by: Piotr Sikora <piotr at cloudflare.com>

diff -r e9effef98874 -r e7596cd5b480 src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h	Fri Dec 26 16:22:59 2014 +0300
+++ b/src/http/ngx_http_cache.h	Mon Jan 05 19:13:48 2015 -0800
@@ -142,6 +142,7 @@ struct ngx_http_file_cache_s {
     ngx_slab_pool_t                 *shpool;
 
     ngx_path_t                      *path;
+    ngx_path_t                      *temp;
 
     off_t                            max_size;
     size_t                           bsize;
@@ -157,7 +158,7 @@ struct ngx_http_file_cache_s {
     ngx_shm_zone_t                  *shm_zone;
 
     ngx_uint_t                       use_temp_path;
-                                     /* unsigned use_temp_path:1 */
+                                     /* unsigned use_temp_path:2 */
 };
 
 
diff -r e9effef98874 -r e7596cd5b480 src/http/ngx_http_file_cache.c
--- a/src/http/ngx_http_file_cache.c	Fri Dec 26 16:22:59 2014 +0300
+++ b/src/http/ngx_http_file_cache.c	Mon Jan 05 19:13:48 2015 -0800
@@ -2069,6 +2069,7 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
 
     off_t                   max_size;
     u_char                 *last, *p;
+    size_t                  len;
     time_t                  inactive;
     ssize_t                 size;
     ngx_str_t               s, name, *value;
@@ -2158,10 +2159,30 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
             } else if (ngx_strcmp(&value[i].data[14], "off") == 0) {
                 use_temp_path = 0;
 
+            } else if (ngx_strcmp(&value[i].data[14], "tmp") == 0) {
+                use_temp_path = 2;
+
+                len = cache->path->name.len + sizeof("/tmp") - 1;
+
+                p = ngx_pnalloc(cf->pool, len);
+                if (p == NULL) {
+                    return NGX_CONF_ERROR;
+                }
+
+                (void) ngx_sprintf(p, "%V/tmp", &cache->path->name);
+
+                cache->temp = ngx_pcalloc(cf->pool, sizeof(ngx_path_t));
+                if (cache->temp == NULL) {
+                    return NGX_CONF_ERROR;
+                }
+
+                cache->temp->name.data = p;
+                cache->temp->name.len = len;
+
             } else {
                 ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
                                    "invalid use_temp_path value \"%V\", "
-                                   "it must be \"on\" or \"off\"",
+                                   "it must be \"on\", \"off\" or \"tmp\"",
                                    &value[i]);
                 return NGX_CONF_ERROR;
             }
@@ -2291,6 +2312,15 @@ ngx_http_file_cache_set_slot(ngx_conf_t 
         return NGX_CONF_ERROR;
     }
 
+    if (cache->temp) {
+        cache->temp->conf_file = cf->conf_file->file.name.data;
+        cache->temp->line = cf->conf_file->line;
+
+        if (ngx_add_path(cf, &cache->temp) != 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;
diff -r e9effef98874 -r e7596cd5b480 src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c	Fri Dec 26 16:22:59 2014 +0300
+++ b/src/http/ngx_http_upstream.c	Mon Jan 05 19:13:48 2015 -0800
@@ -2678,10 +2678,15 @@ ngx_http_upstream_send_response(ngx_http
         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;
+        if (r->cache) {
+            if (r->cache->file_cache->use_temp_path == 0) {
+                p->temp_file->file.name = r->cache->file.name;
+                p->temp_file->path = r->cache->file_cache->path;
+                p->temp_file->prefix = 1;
+
+            } else if (r->cache->file_cache->use_temp_path == 2) {
+                p->temp_file->path = r->cache->file_cache->temp;
+            }
         }
 #endif
 



More information about the nginx-devel mailing list