Skip to content

Commit

Permalink
Cache: proxy_cache_max_range_offset and friends.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
xeioex committed Nov 2, 2016
1 parent 6917d29 commit 6d9023f
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/http/modules/ngx_http_fastcgi_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,13 @@ static ngx_command_t ngx_http_fastcgi_commands[] = {
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,
Expand Down Expand Up @@ -2754,6 +2761,7 @@ ngx_http_fastcgi_create_loc_conf(ngx_conf_t *cf)
#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;
Expand Down Expand Up @@ -2999,6 +3007,10 @@ ngx_http_fastcgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
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
Expand Down
12 changes: 12 additions & 0 deletions src/http/modules/ngx_http_proxy_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,6 +492,13 @@ static ngx_command_t ngx_http_proxy_commands[] = {
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,
Expand Down Expand Up @@ -2847,6 +2854,7 @@ ngx_http_proxy_create_loc_conf(ngx_conf_t *cf)
#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;
Expand Down Expand Up @@ -3108,6 +3116,10 @@ ngx_http_proxy_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
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
Expand Down
12 changes: 12 additions & 0 deletions src/http/modules/ngx_http_scgi_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,13 @@ static ngx_command_t ngx_http_scgi_commands[] = {
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,
Expand Down Expand Up @@ -1204,6 +1211,7 @@ ngx_http_scgi_create_loc_conf(ngx_conf_t *cf)
#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;
Expand Down Expand Up @@ -1444,6 +1452,10 @@ ngx_http_scgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
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
Expand Down
12 changes: 12 additions & 0 deletions src/http/modules/ngx_http_uwsgi_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,13 @@ static ngx_command_t ngx_http_uwsgi_commands[] = {
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,
Expand Down Expand Up @@ -1410,6 +1417,7 @@ ngx_http_uwsgi_create_loc_conf(ngx_conf_t *cf)
#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;
Expand Down Expand Up @@ -1658,6 +1666,10 @@ ngx_http_uwsgi_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
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
Expand Down
55 changes: 55 additions & 0 deletions src/http/ngx_http_upstream.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ static ngx_int_t ngx_http_upstream_cache_get(ngx_http_request_t *r,
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,
Expand Down Expand Up @@ -922,6 +924,10 @@ ngx_http_upstream_cache(ngx_http_request_t *r, ngx_http_upstream_t *u)
return rc;
}

if (ngx_http_upstream_cache_check_range(r, u) == NGX_DECLINED) {
u->cacheable = 0;
}

r->cached = 0;

return NGX_DECLINED;
Expand Down Expand Up @@ -1023,6 +1029,55 @@ ngx_http_upstream_cache_send(ngx_http_request_t *r, ngx_http_upstream_t *u)
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


Expand Down
2 changes: 2 additions & 0 deletions src/http/ngx_http_upstream.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 6d9023f

Please sign in to comment.