Skip to content

Commit 8f1b8ca

Browse files
committed
now we check response header Content-Encoding by default and a non-empty header value will skip srcache_store; also introduced a new directive named srcache_ignore_content_encoding to ignore this response header.
1 parent 69d87b6 commit 8f1b8ca

File tree

3 files changed

+184
-1
lines changed

3 files changed

+184
-1
lines changed

src/ngx_http_srcache_filter_module.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,13 @@ static ngx_command_t ngx_http_srcache_commands[] = {
143143
offsetof(ngx_http_srcache_loc_conf_t, store_no_cache),
144144
NULL },
145145

146+
{ ngx_string("srcache_ignore_content_encoding"),
147+
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_HTTP_LOC_CONF|NGX_CONF_FLAG,
148+
ngx_conf_set_flag_slot,
149+
NGX_HTTP_LOC_CONF_OFFSET,
150+
offsetof(ngx_http_srcache_loc_conf_t, ignore_content_encoding),
151+
NULL },
152+
146153
ngx_null_command
147154
};
148155

@@ -291,9 +298,21 @@ ngx_http_srcache_header_filter(ngx_http_request_t *r)
291298
}
292299
#endif
293300

301+
if (!slcf->ignore_content_encoding && r->headers_out.content_encoding
302+
&& r->headers_out.content_encoding->value.len)
303+
{
304+
ngx_log_error(NGX_LOG_WARN, r->connection->log, 0,
305+
"srcache_store skipped due to response header "
306+
"\"Content-Encoding: %V\" (maybe you forgot to disable "
307+
"compression on the backend?)",
308+
&r->headers_out.content_encoding->value);
309+
310+
return ngx_http_next_header_filter(r);
311+
}
312+
294313
if (ngx_http_srcache_response_no_cache(r, slcf) == NGX_OK) {
295314
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
296-
"srcache_store skipped due to response Cache-Control setting");
315+
"srcache_store skipped due to response header Cache-Control");
297316

298317
return ngx_http_next_header_filter(r);
299318
}
@@ -621,6 +640,7 @@ ngx_http_srcache_create_loc_conf(ngx_conf_t *cf)
621640
conf->store_private = NGX_CONF_UNSET;
622641
conf->store_no_store = NGX_CONF_UNSET;
623642
conf->store_no_cache = NGX_CONF_UNSET;
643+
conf->ignore_content_encoding = NGX_CONF_UNSET;
624644

625645
return conf;
626646
}
@@ -658,6 +678,7 @@ ngx_http_srcache_merge_loc_conf(ngx_conf_t *cf, void *parent, void *child)
658678
ngx_conf_merge_value(conf->store_private, prev->store_private, 0);
659679
ngx_conf_merge_value(conf->store_no_store, prev->store_no_store, 0);
660680
ngx_conf_merge_value(conf->store_no_cache, prev->store_no_cache, 0);
681+
ngx_conf_merge_value(conf->ignore_content_encoding, prev->ignore_content_encoding, 0);
661682

662683
return NGX_CONF_OK;
663684
}

src/ngx_http_srcache_filter_module.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ typedef struct {
3737
ngx_flag_t store_private;
3838
ngx_flag_t store_no_store;
3939
ngx_flag_t store_no_cache;
40+
ngx_flag_t ignore_content_encoding;
4041

4142
unsigned postponed_to_access_phase_end;
4243
} ngx_http_srcache_loc_conf_t;

t/gzip.t

Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
# vi:filetype=
2+
3+
use lib 'lib';
4+
use Test::Nginx::Socket;
5+
6+
#repeat_each(2);
7+
8+
plan tests => repeat_each() * 4 * blocks();
9+
10+
$ENV{TEST_NGINX_MEMCACHED_PORT} ||= 11211;
11+
12+
#master_on();
13+
no_shuffle();
14+
15+
run_tests();
16+
17+
__DATA__
18+
19+
=== TEST 1: flush all
20+
--- config
21+
location /flush {
22+
set $memc_cmd 'flush_all';
23+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
24+
}
25+
--- response_headers
26+
Content-Type: text/plain
27+
Content-Length: 4
28+
--- request
29+
GET /flush
30+
--- response_body eval: "OK\r\n"
31+
32+
33+
34+
=== TEST 2: basic fetch (cache miss), and not stored due to Content-Encoding
35+
--- config
36+
location /foo {
37+
default_type text/css;
38+
srcache_fetch GET /memc $uri;
39+
srcache_store PUT /memc $uri;
40+
41+
content_by_lua '
42+
ngx.header.content_encoding = "gzip"
43+
ngx.say("hello")
44+
';
45+
}
46+
47+
location /memc {
48+
internal;
49+
50+
set $memc_key $query_string;
51+
set $memc_exptime 300;
52+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
53+
}
54+
--- request
55+
GET /foo
56+
--- response_headers
57+
Content-Length:
58+
Content-Encoding: gzip
59+
--- response_body
60+
hello
61+
62+
63+
64+
=== TEST 3: basic fetch (cache miss again, not stored in the previous case)
65+
--- config
66+
location /foo {
67+
default_type text/css;
68+
srcache_fetch GET /memc $uri;
69+
srcache_store PUT /memc $uri;
70+
71+
echo world;
72+
}
73+
74+
location /memc {
75+
internal;
76+
77+
set $memc_key $query_string;
78+
set $memc_exptime 300;
79+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
80+
}
81+
--- request
82+
GET /foo
83+
--- response_headers
84+
Content-Type: text/css
85+
Content-Length:
86+
--- response_body
87+
world
88+
89+
90+
91+
=== TEST 4: flush all
92+
--- config
93+
location /flush {
94+
set $memc_cmd 'flush_all';
95+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
96+
}
97+
--- response_headers
98+
Content-Type: text/plain
99+
Content-Length: 4
100+
--- request
101+
GET /flush
102+
--- response_body eval: "OK\r\n"
103+
104+
105+
106+
=== TEST 5: basic fetch (cache miss), and stored due to Content-Encoding + srcache_ignore_content_encoding
107+
--- config
108+
location /foo {
109+
default_type text/css;
110+
srcache_fetch GET /memc $uri;
111+
srcache_store PUT /memc $uri;
112+
srcache_ignore_content_encoding on;
113+
114+
content_by_lua '
115+
ngx.header.content_encoding = "gzip"
116+
ngx.say("hello")
117+
';
118+
}
119+
120+
location /memc {
121+
internal;
122+
123+
set $memc_key $query_string;
124+
set $memc_exptime 300;
125+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
126+
}
127+
--- request
128+
GET /foo
129+
--- response_headers
130+
Content-Length:
131+
Content-Encoding: gzip
132+
--- response_body
133+
hello
134+
135+
136+
137+
=== TEST 6: basic fetch (cache miss again, not stored in the previous case)
138+
--- config
139+
location /foo {
140+
default_type text/css;
141+
srcache_fetch GET /memc $uri;
142+
srcache_store PUT /memc $uri;
143+
144+
echo world;
145+
}
146+
147+
location /memc {
148+
internal;
149+
150+
set $memc_key $query_string;
151+
set $memc_exptime 300;
152+
memc_pass 127.0.0.1:$TEST_NGINX_MEMCACHED_PORT;
153+
}
154+
--- request
155+
GET /foo
156+
--- response_headers
157+
Content-Type: text/css
158+
Content-Length: 6
159+
--- response_body
160+
hello
161+

0 commit comments

Comments
 (0)