13

I have been focusing heavily on optimizing a certain website so that it scores 100 on the Google PageSpeed Insights tool (for both mobile and desktop). Most of the items are working flawlessly, but I continue to get the "Enable Compression" warning for the website.

This is troublesome, because gzip is enabled on my server, and the only resources that are being served uncompressed are coming from the NGINX PageSpeed module. I have gone through the configuration pages on Google's website, but there is nothing that describes how to enable compression, other than the general NGINX configuration that is already in place.

My question is this: How do I enable gzip compression so that it works for pagespeed resources?

My server setup:

Ubuntu 12.0.4.3 LTS NGINX - Custom compiled 1.5.4 with PageSpeed module 1.6.29.5 beta

NGINX Server Config:

user  www-data;
#set worker processes to cpu processes
worker_processes  4;

error_log /var/log/nginx/error.log;
pid /run/nginx.pid;
events {
        worker_connections  1024;
}


http {
        client_max_body_size 200m;
        include /etc/nginx/mime.types;
        default_type application/octet-stream;
        access_log /var/log/nginx/access.log;
        sendfile on;
        keepalive_timeout 3;
        types_hash_max_size 2048;
        gzip  on;
        gzip_disable msie6;
        gzip_static on;
        gzip_types text/plain text/css application/x-javascript text/xml application/xml+rss text/javascript;
        gzip_vary on;
        fastcgi_read_timeout 2m;

        include global/caching.conf;
        include /etc/nginx/enabled-sites/*;
        upstream php {
                server 127.0.0.1:9000;
        }
        #fastcgi caching header
        add_header mcapp-fastcgi-cache $upstream_cache_status;
}

Website Config:

server {
        server_name www.examplesite.com;
        rewrite ^ $scheme://examplesite.com$request_uri permanent;
}

server {
        #pagespeed directives
        pagespeed On;
        pagespeed FileCachePath /var/cache/nginx-pagespeed;
        location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" {
                add_header "" "";
        }
        location ~ "^/ngx_pagespeed_static/" { }
        location ~ "^/ngx_pagespeed_beacon$" { }
        #pagespeed directives end

        server_name examplesite.com;
        root /path/to/examplesite;

        # wordpress config
        include global/restrictions.conf;
        include global/wordpress.conf;
}

EDIT Just to further elaborate, the specific assets that do not seem to be compressing are the javascript assets. As an example:

Enable compression for the following resources to reduce their transfer size by 355.5KiB (69% reduction).
    Compressing http://examplesite.com/wp-includes/js/jquery/jquery.js,qver=1.10.2.pagespeed.jm.iCH2ukpEYb.js could save 58.8KiB (64% reduction).
    Compressing http://examplesite.com/wp-content/themes/Avada/framework/plugins/revslider/rs-plugin/js/jquery.themepunch.revolution.min.js?ver=3.6.1 could save 43.9KiB (80% reduction).
Scrivvles
  • 391
  • 1
  • 2
  • 11
  • did you checked with other tools like redbot? i found pagespeed not reliable, esp when setting gzip or `expires 24hrs` for a reason i always get "BEEEEP, YOU'RE HOLDING IT WRONG". same applies for YSLOW – that guy from over there Oct 02 '13 at 06:32
  • I did check redbot, and it is compressing most of the assets with gzip (such as the actual html and css files). However, most of the javascript assets that come from PageSpeed are not being compressed. My config is set to compress application/x-javascript and text/javascript mime-types, and I have verified that it works on other websites that have javascript assets. For some reason though, it just doesn't seem to be working for the PageSpeed served assets. – Scrivvles Oct 02 '13 at 14:57

3 Answers3

16

After a lot more of hair pulling, gnashing of teeth, and speaker-punching, (and Googling), I came across a defect request in an NGINX support forum to change the javascript (.js) mime-type from application/x-javascript to application/javascript. See http://trac.nginx.org/nginx/ticket/306

As you can see by the nginx.conf in my question, I had:

gzip_types text/plain text/css application/x-javascript text/xml application/xml+rss text/javascript;

This was essentially causing my javascript files to be ignored by the gzip_types, because there IS no application/x-javascript mime-type anymore (unless you make a custom one in mime-types.conf or you have a really old version of NGINX).

I changed that line to this one:

gzip_types text/plain text/css application/javascript text/xml application/xml+rss;

After an NGINX -s reload, my javascript files compress just fine! So, it turns out it had nothing to do with the PageSpeed module, and instead was a problem with my configuration not identifying the correct mime-type to compress.

Scrivvles
  • 391
  • 1
  • 2
  • 11
  • 1
    Just a note - for some reason I had to keep both `application/x-javascript` and `application/javascript`, since during my requests I was getting both mime types(I wonder if I should switch to just `application/javascript`. – Nikola Ivanov Nikolov Sep 26 '14 at 10:03
3

As of Release 1.9.32.1-beta, ngx_pagespeed will enable and configure gzip itself when no explicit gzip configuration exists in nginx (and the gzip compression module is compiled in).

See https://developers.google.com/speed/pagespeed/module/release_notes#release_1.9.32.1-beta

2

As per RFC 4329, your webserver should use application/javascript and not application/x-javascript.

First, you should check that your /etc/nginx/nginx.conf file contains (at least) application/javascript next to gzip_types:

E.g.

gzip_types text/plain text/css application/javascript text/xml application/xml+rss;

Then, open your MIME types file /etc/nginx/mime.types and make sure that if you see this:

application/x-javascript                  js;

to

application/javascript                  js;

Finally, reload your configuration:

service nginx reload

That's it!

Maxime
  • 195
  • 1
  • 5