Question

NGINX location directive - Not serving gzip pre-compressed files

Posted October 31, 2019 1.4k views
NginxWordPressUbuntu 18.04

Hi all!

I have a Wordpress website where I am trying to serve through nginx, gzip pre-compressed html files generated by Wp Super Cache plugin and for that I created a location directive with special headers and gzip off. For an unknown reason to me nginx is not taking in consideration the content-header options and whenever I access a page Firefox is attempting to download gz files.

Could you please tell me what am I doing wrong?

Nginx is not throwing errors.

# nginx -t

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

I really appreciate your help!

Here is the code:

       set $full_cachefile '/wp-content/cache/supercache/$http_host${condition}';
       set $full_gzipcachefile '/wp-content/cache/supercache/$http_host${condition}';


    location ~ /wp-content/cache/supercache.*html$ {
        add_header Vary "Accept-Encoding, Cookie";
        add_header Pragma "public";
        add_header Cache-Control "max-age=3600, public";
        }


    location ~ /wp-content/cache/supercache.*gz$ {
        gzip off;
        types {}
        default_type text/html;
        add_header Vary "Accept-Encoding, Cookie";
        add_header Pragma "public";
        add_header Cache-Control "max-age=3600, public";
        add_header Content-Encoding gzip;
    }



    location / {

        set $cachefile $full_cachefile${uri}index-https.html;
        set $gzipcachefile $full_cachefile${uri}index-https.html.gz;

        try_files $gzipcachefile $cachefile $uri $uri/ /index.php$is_args$args;
    }




        location ~ \.php$ {
        try_files $uri =404;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_buffers 256 16k;
        fastcgi_buffer_size 128k;
        fastcgi_connect_timeout 3s;
        fastcgi_send_timeout 120s;
        fastcgi_read_timeout 120s;
        fastcgi_busy_buffers_size 256k;
        fastcgi_temp_file_write_size 256k;
        reset_timedout_connection on;
        include fastcgi_params;
        #fastcgi_pass 127.0.0.1:9012;
        fastcgi_pass unix:/tmp/lunarp.sock;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $request_filename;
        #fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        fastcgi_param PATH_INFO $fastcgi_path_info;
    }

edited by bobbyiliev

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

×
1 answer

Hi @konkhra

It is not really clear on what you want to do exactly. The Gzip part in your Nginx would tell basically add a Gzip header. The .gz files that you are accessing are simply gziped files. The two things are kind of not related directly.

To test if the gzip off is working you could check the headers when accessing your /wp-content/cache/supercache.*gz URL:

curl -IL yourdomain.com/wp-content/cache/supercache.somefile.gz

If you do not see the gzip header, then the location rules are working as expected.

Submit an Answer