cache gzipped content with proxy_pass
chrisl
nginx-forum at nginx.us
Sat Aug 21 15:27:41 MSD 2010
Maxim,
thank you for your answer. I've patched nginx 0.7.67 to export gzip_ok
to config file variable space (see attachement). I'm not a C guy, but it
seems to work :-) I' currently using it like this:
location /json-data/ {
set $ae "";
if ($gzip_ok) {
set $ae "gzip";
}
proxy_pass http://10.2.3.4/json-upstream/;
proxy_cache_key "$gzip_ok:$uri";
proxy_set_header User-Agent "nginx";
proxy_set_header Accept-Encoding $ae;
[ ... ]
}
I get cache keys like this:
# grep -a ^KEY /var/spool/nginx/*
/var/spool/nginx/0c341794ed57bf03d72fb9a207f43d0e:KEY:
:/json-data/123/45/
/var/spool/nginx/17961a45b339647f5e8f239dde713428:KEY:
OK:/json-data/123/45/
By hiding the client User-Agent and Accept-Encoding, I deactivate
upstream gzip detection logic. For this specific task this patch/config
works perfectly. I more general approach would be nice, though.
Chris
--- src/http/ngx_http_variables.c.orig 2010-08-21 12:17:42.000000000
+0200
+++ src/http/ngx_http_variables.c 2010-08-21 12:19:07.000000000
+0200
@@ -66,6 +66,10 @@
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t
ngx_http_variable_request_completion(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
+#if (NGX_HTTP_GZIP)
+static ngx_int_t ngx_http_variable_gzip_ok(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data);
+#endif
static ngx_int_t ngx_http_variable_request_body(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_variable_request_body_file(ngx_http_request_t
*r,
@@ -123,6 +127,9 @@
#if (NGX_HTTP_GZIP)
{ ngx_string("http_via"), NULL, ngx_http_variable_header,
offsetof(ngx_http_request_t, headers_in.via), 0, 0 },
+
+ { ngx_string("gzip_ok"), NULL, ngx_http_variable_gzip_ok,
+ 0, 0, 0 },
#endif
#if (NGX_HTTP_PROXY || NGX_HTTP_REALIP)
@@ -1538,6 +1545,41 @@
return NGX_OK;
}
+#if (NGX_HTTP_GZIP)
+static ngx_int_t
+ngx_http_variable_gzip_ok(ngx_http_request_t *r,
+ ngx_http_variable_value_t *v, uintptr_t data)
+{
+ ngx_int_t rc;
+
+ if (!r->gzip_tested) {
+ rc = ngx_http_gzip_ok(r);
+
+ if (rc != NGX_OK && rc != NGX_DECLINED) {
+ v->not_found = 1;
+ return NGX_OK;
+ }
+ }
+
+ if (r->gzip_ok) {
+ v->len = 2;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = (u_char *) "OK";
+
+ return NGX_OK;
+ }
+
+ v->len = 0;
+ v->valid = 1;
+ v->no_cacheable = 0;
+ v->not_found = 0;
+ v->data = (u_char *) "";
+
+ return NGX_OK;
+}
+#endif
static ngx_int_t
ngx_http_variable_request_body(ngx_http_request_t *r,
Posted at Nginx Forum: http://forum.nginx.org/read.php?2,120314,122064#msg-122064
More information about the nginx
mailing list