[PATCH 06 of 15] Configuration for tempfiles serving

Jiří Setnička jiri.setnicka at cdn77.com
Fri Jan 28 16:31:58 UTC 2022


# HG changeset patch
# User Jiří Setnička  <jiri.setnicka at cdn77.com>
# Date 1643385660 -3600
#      Fri Jan 28 17:01:00 2022 +0100
# Node ID 5e3013a56643a9f8d26ea0e5882a1aa986c51903
# Parent  76c1a836b1de47cb16b636b585526e25574e0f58
Configuration for tempfiles serving
New directives:
* proxy_cache_tempfile on/off      -- activate the whole tempfile serving logic
* proxy_cache_tempfile_timeout 5s; -- how long to wait for tempfile before 504
* proxy_cache_tempfile_loop 50ms;  -- loop time for check tempiles

* fastcgi_cache_tempfile on/off      -- activate the whole tempfile serving logic
* fastcgi_cache_tempfile_timeout 5s; -- how long to wait for tempfile before 504
* fastcgi_cache_tempfile_loop 50ms;  -- loop time for check tempiles

* scgi_cache_tempfile on/off      -- activate the whole tempfile serving logic
* scgi_cache_tempfile_timeout 5s; -- how long to wait for tempfile before 504
* scgi_cache_tempfile_loop 50ms;  -- loop time for check tempiles

* uwsgi_cache_tempfile on/off      -- activate the whole tempfile serving logic
* uwsgi_cache_tempfile_timeout 5s; -- how long to wait for tempfile before 504
* uwsgi_cache_tempfile_loop 50ms;  -- loop time for check tempiles

diff --git a/src/http/modules/ngx_http_fastcgi_module.c b/src/http/modules/ngx_http_fastcgi_module.c
--- a/src/http/modules/ngx_http_fastcgi_module.c
+++ b/src/http/modules/ngx_http_fastcgi_module.c
@@ -473,6 +473,27 @@ static ngx_command_t  ngx_http_fastcgi_c
       offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_lock_age),
       NULL },
 
+    { ngx_string("fastcgi_cache_tempfile"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_tempfile),
+      NULL },
+
+    { ngx_string("fastcgi_cache_tempfile_timeout"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_tempfile_timeout),
+      NULL },
+
+    { ngx_string("fastcgi_cache_tempfile_loop"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_tempfile_loop),
+      NULL },
+
     { ngx_string("fastcgi_cache_revalidate"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -2866,6 +2887,9 @@ ngx_http_fastcgi_create_loc_conf(ngx_con
     conf->upstream.cache_lock = NGX_CONF_UNSET;
     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_tempfile = NGX_CONF_UNSET;
+    conf->upstream.cache_tempfile_timeout = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_tempfile_loop = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
     conf->upstream.cache_background_update = NGX_CONF_UNSET;
 #endif
@@ -3160,6 +3184,22 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf
     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
                               prev->upstream.cache_lock_age, 5000);
 
+    ngx_conf_merge_value(conf->upstream.cache_tempfile,
+                              prev->upstream.cache_tempfile, 0);
+
+    ngx_conf_merge_msec_value(conf->upstream.cache_tempfile_timeout,
+                              prev->upstream.cache_tempfile_timeout, 5000);
+
+    ngx_conf_merge_msec_value(conf->upstream.cache_tempfile_loop,
+                              prev->upstream.cache_tempfile_loop, 50);
+
+    if (conf->upstream.cache_lock && conf->upstream.cache_tempfile) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "\"fastcgi_cache_lock\" and \"fastcgi_cache_tempfile\""
+                           " cannot be used together");
+        return NGX_CONF_ERROR;
+    }
+
     ngx_conf_merge_value(conf->upstream.cache_revalidate,
                               prev->upstream.cache_revalidate, 0);
 
diff --git a/src/http/modules/ngx_http_proxy_module.c b/src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c
+++ b/src/http/modules/ngx_http_proxy_module.c
@@ -590,6 +590,27 @@ static ngx_command_t  ngx_http_proxy_com
       offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_lock_age),
       NULL },
 
+    { ngx_string("proxy_cache_tempfile"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_tempfile),
+      NULL },
+
+    { ngx_string("proxy_cache_tempfile_timeout"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_tempfile_timeout),
+      NULL },
+
+    { ngx_string("proxy_cache_tempfile_loop"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_tempfile_loop),
+      NULL },
+
     { ngx_string("proxy_cache_revalidate"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -3384,6 +3405,9 @@ ngx_http_proxy_create_loc_conf(ngx_conf_
     conf->upstream.cache_lock = NGX_CONF_UNSET;
     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_tempfile = NGX_CONF_UNSET;
+    conf->upstream.cache_tempfile_timeout = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_tempfile_loop = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
     conf->upstream.cache_convert_head = NGX_CONF_UNSET;
     conf->upstream.cache_background_update = NGX_CONF_UNSET;
@@ -3699,6 +3723,22 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t
     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
                               prev->upstream.cache_lock_age, 5000);
 
+    ngx_conf_merge_value(conf->upstream.cache_tempfile,
+                              prev->upstream.cache_tempfile, 0);
+
+    ngx_conf_merge_msec_value(conf->upstream.cache_tempfile_timeout,
+                              prev->upstream.cache_tempfile_timeout, 5000);
+
+    ngx_conf_merge_msec_value(conf->upstream.cache_tempfile_loop,
+                              prev->upstream.cache_tempfile_loop, 50);
+
+    if (conf->upstream.cache_lock && conf->upstream.cache_tempfile) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "\"proxy_cache_lock\" and \"proxy_cache_tempfile\""
+                           " cannot be used together");
+        return NGX_CONF_ERROR;
+    }
+
     ngx_conf_merge_value(conf->upstream.cache_revalidate,
                               prev->upstream.cache_revalidate, 0);
 
diff --git a/src/http/modules/ngx_http_scgi_module.c b/src/http/modules/ngx_http_scgi_module.c
--- a/src/http/modules/ngx_http_scgi_module.c
+++ b/src/http/modules/ngx_http_scgi_module.c
@@ -321,6 +321,27 @@ static ngx_command_t ngx_http_scgi_comma
       offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_lock_age),
       NULL },
 
+    { ngx_string("scgi_cache_tempfile"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_tempfile),
+      NULL },
+
+    { ngx_string("scgi_cache_tempfile_timeout"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_tempfile_timeout),
+      NULL },
+
+    { ngx_string("scgi_cache_tempfile_loop"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_tempfile_loop),
+      NULL },
+
     { ngx_string("scgi_cache_revalidate"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -1273,6 +1294,9 @@ ngx_http_scgi_create_loc_conf(ngx_conf_t
     conf->upstream.cache_lock = NGX_CONF_UNSET;
     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_tempfile = NGX_CONF_UNSET;
+    conf->upstream.cache_tempfile_timeout = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_tempfile_loop = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
     conf->upstream.cache_background_update = NGX_CONF_UNSET;
 #endif
@@ -1562,6 +1586,22 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t 
     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
                               prev->upstream.cache_lock_age, 5000);
 
+    ngx_conf_merge_value(conf->upstream.cache_tempfile,
+                              prev->upstream.cache_tempfile, 0);
+
+    ngx_conf_merge_msec_value(conf->upstream.cache_tempfile_timeout,
+                              prev->upstream.cache_tempfile_timeout, 5000);
+
+    ngx_conf_merge_msec_value(conf->upstream.cache_tempfile_loop,
+                              prev->upstream.cache_tempfile_loop, 50);
+
+    if (conf->upstream.cache_lock && conf->upstream.cache_tempfile) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "\"scgi_cache_lock\" and \"scgi_cache_tempfile\""
+                           " cannot be used together");
+        return NGX_CONF_ERROR;
+    }
+
     ngx_conf_merge_value(conf->upstream.cache_revalidate,
                               prev->upstream.cache_revalidate, 0);
 
diff --git a/src/http/modules/ngx_http_uwsgi_module.c b/src/http/modules/ngx_http_uwsgi_module.c
--- a/src/http/modules/ngx_http_uwsgi_module.c
+++ b/src/http/modules/ngx_http_uwsgi_module.c
@@ -385,6 +385,27 @@ static ngx_command_t ngx_http_uwsgi_comm
       offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_lock_age),
       NULL },
 
+    { ngx_string("uwsgi_cache_tempfile"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
+      ngx_conf_set_flag_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_tempfile),
+      NULL },
+
+    { ngx_string("uwsgi_cache_tempfile_timeout"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_tempfile_timeout),
+      NULL },
+
+    { ngx_string("uwsgi_cache_tempfile_loop"),
+      NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+      ngx_conf_set_msec_slot,
+      NGX_HTTP_LOC_CONF_OFFSET,
+      offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_tempfile_loop),
+      NULL },
+
     { ngx_string("uwsgi_cache_revalidate"),
       NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
       ngx_conf_set_flag_slot,
@@ -1497,6 +1518,9 @@ ngx_http_uwsgi_create_loc_conf(ngx_conf_
     conf->upstream.cache_lock = NGX_CONF_UNSET;
     conf->upstream.cache_lock_timeout = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_lock_age = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_tempfile = NGX_CONF_UNSET;
+    conf->upstream.cache_tempfile_timeout = NGX_CONF_UNSET_MSEC;
+    conf->upstream.cache_tempfile_loop = NGX_CONF_UNSET_MSEC;
     conf->upstream.cache_revalidate = NGX_CONF_UNSET;
     conf->upstream.cache_background_update = NGX_CONF_UNSET;
 #endif
@@ -1798,6 +1822,22 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t
     ngx_conf_merge_msec_value(conf->upstream.cache_lock_age,
                               prev->upstream.cache_lock_age, 5000);
 
+    ngx_conf_merge_value(conf->upstream.cache_tempfile,
+                              prev->upstream.cache_tempfile, 0);
+
+    ngx_conf_merge_msec_value(conf->upstream.cache_tempfile_timeout,
+                              prev->upstream.cache_tempfile_timeout, 5000);
+
+    ngx_conf_merge_msec_value(conf->upstream.cache_tempfile_loop,
+                              prev->upstream.cache_tempfile_loop, 50);
+
+    if (conf->upstream.cache_lock && conf->upstream.cache_tempfile) {
+        ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
+                           "\"uwsgi_cache_lock\" and \"uwsgi_cache_tempfile\""
+                           " cannot be used together");
+        return NGX_CONF_ERROR;
+    }
+
     ngx_conf_merge_value(conf->upstream.cache_revalidate,
                               prev->upstream.cache_revalidate, 0);
 
diff --git a/src/http/ngx_http_cache.h b/src/http/ngx_http_cache.h
--- a/src/http/ngx_http_cache.h
+++ b/src/http/ngx_http_cache.h
@@ -114,11 +114,14 @@ struct ngx_http_cache_s {
     ngx_msec_t                       lock_timeout;
     ngx_msec_t                       lock_age;
     ngx_msec_t                       lock_time;
+    ngx_msec_t                       tempfile_timeout;
+    ngx_msec_t                       tempfile_loop;
     ngx_msec_t                       wait_time;
 
     ngx_event_t                      wait_event;
 
     unsigned                         lock:1;
+    unsigned                         serve_tempfile:1;
     unsigned                         waiting:1;
 
     unsigned                         updated:1;
diff --git a/src/http/ngx_http_upstream.c b/src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c
+++ b/src/http/ngx_http_upstream.c
@@ -892,6 +892,10 @@ ngx_http_upstream_cache(ngx_http_request
         c->lock_timeout = u->conf->cache_lock_timeout;
         c->lock_age = u->conf->cache_lock_age;
 
+        c->serve_tempfile = u->conf->cache_tempfile;
+        c->tempfile_timeout = u->conf->cache_tempfile_timeout;
+        c->tempfile_loop = u->conf->cache_tempfile_loop;
+
         u->cache_status = NGX_HTTP_CACHE_MISS;
     }
 
diff --git a/src/http/ngx_http_upstream.h b/src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h
+++ b/src/http/ngx_http_upstream.h
@@ -205,6 +205,10 @@ typedef struct {
     ngx_msec_t                       cache_lock_timeout;
     ngx_msec_t                       cache_lock_age;
 
+    ngx_flag_t                       cache_tempfile;
+    ngx_msec_t                       cache_tempfile_timeout;
+    ngx_msec_t                       cache_tempfile_loop;
+
     ngx_flag_t                       cache_revalidate;
     ngx_flag_t                       cache_convert_head;
     ngx_flag_t                       cache_background_update;


More information about the nginx-devel mailing list