Can proxy_cache gzip cached content?
Massimiliano Mirra
hyperstruct at gmail.com
Fri Feb 17 13:03:17 UTC 2012
On Wed, Feb 15, 2012 at 3:55 PM, rmalayter <nginx-forum at nginx.us> wrote:
>
> There's no reason the "backend" for your caching layer cannot be another
> nginx server block running on a high port bound to localhost. This
> high-port server block could do gzip compression, and proxy-pass to the
> back end with "Accept-Encoding: identity", so the back-end never has to
> do compression. The backend server will have to use "gzip_http_version
> 1.0" and "gzip_proxied any" to do compression because it is being
> proxied from the front-end.
>
Ah, good point. I tried to take this an extra step further by using a
virtual host of the same server as "compression backend" and it appears to
work nicely. Below is what I did so far, in case anyone is looking for the
same and Google leads them here.
(Feels a bit like getting out of the door and back in through the window :)
but perhaps just like we have internal redirects it would be possible to
use ngx_lua to simulate an internal proxy and avoid the extra HTTP request.)
proxy_cache_path /var/lib/nginx/cache/myapp
levels=1:2
keys_zone=myapp_cache:10m
max_size=1g
inactive=2d;
log_format cache '***$time_local '
'$upstream_cache_status '
'Cache-Control: $upstream_http_cache_control '
'Expires: $upstream_http_expires '
'$host '
'"$request" ($status) '
'"$http_user_agent" '
'Args: $args ';
access_log /var/log/nginx/cache.log cache;
upstream backend {
server localhost:8002;
}
server { # this step only does compression
listen 85;
server_name myapp.local;
include proxy_params;
location / {
gzip_http_version 1.0;
proxy_set_header Accept-Encoding identity;
proxy_pass http://backend;
}
}
server {
listen 80;
server_name myapp.local;
include proxy_params;
location / {
proxy_pass http://127.0.0.1:85;
}
location /similar-to {
proxy_set_header Accept-Encoding gzip;
proxy_cache_key "$scheme$host$request_uri";
proxy_cache_valid 2d;
proxy_cache myapp_cache;
proxy_pass http://127.0.0.1:85;
}
}
> Also note there may be better options in the latest nginx versions, or
> by using the gunzip 3rd-party module:
> http://mdounin.ru/hg/ngx_http_gunzip_filter_module/file/27f057249155/README
>
> With the gunzip module, you can configure things so that you always
> cache compressed data, then only decompress it for the small number of
> clients that don't support gzip compression.
>
This looks perfect for having a gzip-only cache, which may not lead to save
that much disk space but it certainly helps with mind space.
Cheers,
Massimiliano
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mailman.nginx.org/pipermail/nginx/attachments/20120217/86a301cc/attachment.html>
More information about the nginx
mailing list