Can proxy_cache gzip cached content?

Massimiliano Mirra hyperstruct at
Fri Feb 17 13:03:17 UTC 2012

On Wed, Feb 15, 2012 at 3:55 PM, rmalayter <nginx-forum at> 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

    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 / {

            location /similar-to {
                    proxy_set_header Accept-Encoding gzip;
                    proxy_cache_key "$scheme$host$request_uri";
                    proxy_cache_valid 2d;
                    proxy_cache myapp_cache;

> Also note there may be better options in the latest nginx versions, or
> by using the gunzip 3rd-party module:
> 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.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <>

More information about the nginx mailing list