Nginx – How to enable gzip Compression on NGINX PageSpeed Module resources

nginxpage-speedubuntu-12.04

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).

Best Answer

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.