[nginx] Cache: proxy_cache_max_range_offset and friends.
Dmitry Volyntsev
xeioex at nginx.com
Wed Nov 2 18:39:54 UTC 2016
details: http://hg.nginx.org/nginx/rev/0fba3ed4e7eb
branches:
changeset: 6793:0fba3ed4e7eb
user: Dmitry Volyntsev <xeioex at nginx.com>
date: Wed Nov 02 20:05:21 2016 +0300
description:
Cache: proxy_cache_max_range_offset and friends.
It configures a threshold in bytes, above which client range
requests are not cached. In such a case the client's Range
header is passed directly to a proxied server.
diffstat:
src/http/modules/ngx_http_fastcgi_module.c | 12 ++++++
src/http/modules/ngx_http_proxy_module.c | 12 ++++++
src/http/modules/ngx_http_scgi_module.c | 12 ++++++
src/http/modules/ngx_http_uwsgi_module.c | 12 ++++++
src/http/ngx_http_upstream.c | 55 ++++++++++++++++++++++++++++++
src/http/ngx_http_upstream.h | 2 +
6 files changed, 105 insertions(+), 0 deletions(-)
diffs (235 lines):
diff -r 45d553812055 -r 0fba3ed4e7eb src/http/modules/ngx_http_fastcgi_module.c
--- a/src/http/modules/ngx_http_fastcgi_module.c Wed Nov 02 11:47:12 2016 +0300
+++ b/src/http/modules/ngx_http_fastcgi_module.c Wed Nov 02 20:05:21 2016 +0300
@@ -420,6 +420,13 @@ static ngx_command_t ngx_http_fastcgi_c
offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_min_uses),
NULL },
+ { ngx_string("fastcgi_cache_max_range_offset"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_off_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_fastcgi_loc_conf_t, upstream.cache_max_range_offset),
+ NULL },
+
{ ngx_string("fastcgi_cache_use_stale"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
@@ -2754,6 +2761,7 @@ ngx_http_fastcgi_create_loc_conf(ngx_con
#if (NGX_HTTP_CACHE)
conf->upstream.cache = NGX_CONF_UNSET;
conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
+ conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
@@ -2999,6 +3007,10 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf
ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
prev->upstream.cache_min_uses, 1);
+ ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
+ prev->upstream.cache_max_range_offset,
+ NGX_MAX_OFF_T_VALUE);
+
ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
prev->upstream.cache_use_stale,
(NGX_CONF_BITMASK_SET
diff -r 45d553812055 -r 0fba3ed4e7eb src/http/modules/ngx_http_proxy_module.c
--- a/src/http/modules/ngx_http_proxy_module.c Wed Nov 02 11:47:12 2016 +0300
+++ b/src/http/modules/ngx_http_proxy_module.c Wed Nov 02 20:05:21 2016 +0300
@@ -492,6 +492,13 @@ static ngx_command_t ngx_http_proxy_com
offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_min_uses),
NULL },
+ { ngx_string("proxy_cache_max_range_offset"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_off_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_proxy_loc_conf_t, upstream.cache_max_range_offset),
+ NULL },
+
{ ngx_string("proxy_cache_use_stale"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
@@ -2847,6 +2854,7 @@ ngx_http_proxy_create_loc_conf(ngx_conf_
#if (NGX_HTTP_CACHE)
conf->upstream.cache = NGX_CONF_UNSET;
conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
+ conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
@@ -3108,6 +3116,10 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t
ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
prev->upstream.cache_min_uses, 1);
+ ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
+ prev->upstream.cache_max_range_offset,
+ NGX_MAX_OFF_T_VALUE);
+
ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
prev->upstream.cache_use_stale,
(NGX_CONF_BITMASK_SET
diff -r 45d553812055 -r 0fba3ed4e7eb src/http/modules/ngx_http_scgi_module.c
--- a/src/http/modules/ngx_http_scgi_module.c Wed Nov 02 11:47:12 2016 +0300
+++ b/src/http/modules/ngx_http_scgi_module.c Wed Nov 02 20:05:21 2016 +0300
@@ -270,6 +270,13 @@ static ngx_command_t ngx_http_scgi_comma
offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_min_uses),
NULL },
+ { ngx_string("scgi_cache_max_range_offset"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_off_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_scgi_loc_conf_t, upstream.cache_max_range_offset),
+ NULL },
+
{ ngx_string("scgi_cache_use_stale"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
@@ -1204,6 +1211,7 @@ ngx_http_scgi_create_loc_conf(ngx_conf_t
#if (NGX_HTTP_CACHE)
conf->upstream.cache = NGX_CONF_UNSET;
conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
+ conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
@@ -1444,6 +1452,10 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t
ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
prev->upstream.cache_min_uses, 1);
+ ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
+ prev->upstream.cache_max_range_offset,
+ NGX_MAX_OFF_T_VALUE);
+
ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
prev->upstream.cache_use_stale,
(NGX_CONF_BITMASK_SET
diff -r 45d553812055 -r 0fba3ed4e7eb src/http/modules/ngx_http_uwsgi_module.c
--- a/src/http/modules/ngx_http_uwsgi_module.c Wed Nov 02 11:47:12 2016 +0300
+++ b/src/http/modules/ngx_http_uwsgi_module.c Wed Nov 02 20:05:21 2016 +0300
@@ -330,6 +330,13 @@ static ngx_command_t ngx_http_uwsgi_comm
offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_min_uses),
NULL },
+ { ngx_string("uwsgi_cache_max_range_offset"),
+ NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_TAKE1,
+ ngx_conf_set_off_slot,
+ NGX_HTTP_LOC_CONF_OFFSET,
+ offsetof(ngx_http_uwsgi_loc_conf_t, upstream.cache_max_range_offset),
+ NULL },
+
{ ngx_string("uwsgi_cache_use_stale"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_1MORE,
ngx_conf_set_bitmask_slot,
@@ -1410,6 +1417,7 @@ ngx_http_uwsgi_create_loc_conf(ngx_conf_
#if (NGX_HTTP_CACHE)
conf->upstream.cache = NGX_CONF_UNSET;
conf->upstream.cache_min_uses = NGX_CONF_UNSET_UINT;
+ conf->upstream.cache_max_range_offset = NGX_CONF_UNSET;
conf->upstream.cache_bypass = NGX_CONF_UNSET_PTR;
conf->upstream.no_cache = NGX_CONF_UNSET_PTR;
conf->upstream.cache_valid = NGX_CONF_UNSET_PTR;
@@ -1658,6 +1666,10 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t
ngx_conf_merge_uint_value(conf->upstream.cache_min_uses,
prev->upstream.cache_min_uses, 1);
+ ngx_conf_merge_off_value(conf->upstream.cache_max_range_offset,
+ prev->upstream.cache_max_range_offset,
+ NGX_MAX_OFF_T_VALUE);
+
ngx_conf_merge_bitmask_value(conf->upstream.cache_use_stale,
prev->upstream.cache_use_stale,
(NGX_CONF_BITMASK_SET
diff -r 45d553812055 -r 0fba3ed4e7eb src/http/ngx_http_upstream.c
--- a/src/http/ngx_http_upstream.c Wed Nov 02 11:47:12 2016 +0300
+++ b/src/http/ngx_http_upstream.c Wed Nov 02 20:05:21 2016 +0300
@@ -17,6 +17,8 @@ static ngx_int_t ngx_http_upstream_cache
ngx_http_upstream_t *u, ngx_http_file_cache_t **cache);
static ngx_int_t ngx_http_upstream_cache_send(ngx_http_request_t *r,
ngx_http_upstream_t *u);
+static ngx_int_t ngx_http_upstream_cache_check_range(ngx_http_request_t *r,
+ ngx_http_upstream_t *u);
static ngx_int_t ngx_http_upstream_cache_status(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_upstream_cache_last_modified(ngx_http_request_t *r,
@@ -922,6 +924,10 @@ ngx_http_upstream_cache(ngx_http_request
return rc;
}
+ if (ngx_http_upstream_cache_check_range(r, u) == NGX_DECLINED) {
+ u->cacheable = 0;
+ }
+
r->cached = 0;
return NGX_DECLINED;
@@ -1023,6 +1029,55 @@ ngx_http_upstream_cache_send(ngx_http_re
return rc;
}
+
+static ngx_int_t
+ngx_http_upstream_cache_check_range(ngx_http_request_t *r,
+ ngx_http_upstream_t *u)
+{
+ off_t offset;
+ u_char *p, *start;
+ ngx_table_elt_t *h;
+
+ h = r->headers_in.range;
+
+ if (h == NULL
+ || !u->cacheable
+ || u->conf->cache_max_range_offset == NGX_MAX_OFF_T_VALUE)
+ {
+ return NGX_OK;
+ }
+
+ if (u->conf->cache_max_range_offset == 0) {
+ return NGX_DECLINED;
+ }
+
+ if (h->value.len < 7
+ || ngx_strncasecmp(h->value.data, (u_char *) "bytes=", 6) != 0)
+ {
+ return NGX_OK;
+ }
+
+ p = h->value.data + 6;
+
+ while (*p == ' ') { p++; }
+
+ if (*p == '-') {
+ return NGX_DECLINED;
+ }
+
+ start = p;
+
+ while (*p >= '0' && *p <= '9') { p++; }
+
+ offset = ngx_atoof(start, p - start);
+
+ if (offset >= u->conf->cache_max_range_offset) {
+ return NGX_DECLINED;
+ }
+
+ return NGX_OK;
+}
+
#endif
diff -r 45d553812055 -r 0fba3ed4e7eb src/http/ngx_http_upstream.h
--- a/src/http/ngx_http_upstream.h Wed Nov 02 11:47:12 2016 +0300
+++ b/src/http/ngx_http_upstream.h Wed Nov 02 20:05:21 2016 +0300
@@ -198,6 +198,8 @@ typedef struct {
ngx_uint_t cache_use_stale;
ngx_uint_t cache_methods;
+ off_t cache_max_range_offset;
+
ngx_flag_t cache_lock;
ngx_msec_t cache_lock_timeout;
ngx_msec_t cache_lock_age;
More information about the nginx-devel
mailing list