Gzip javascript and css with NGinx

June 29th, 2012

Warning: This post is 11 years old. Some of this information may be out of date.

Nginx by default will only compress text/html content types. Heres how to gzip javascript and css with NGinx:

In your nginx.conf file:

...
http {
    include       /etc/nginx/mime.types;

    access_log  /var/log/nginx/access.log;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;
    tcp_nodelay        on;

    gzip  on;
    gzip_disable "MSIE [1-6]\.(?!.*SV1)";
    gzip_types application/x-javascript text/css;

    include /etc/nginx/conf.d/*.conf;
    include /etc/nginx/sites-enabled/*;
}
....

Or in your sites-enabled site file:

server {
    listen   127.0.0.1:81;
    server_name  localhost;

    gzip_types application/x-javascript text/css;

    access_log  /var/log/nginx/localhost.access.log;
    ....
}

Reload nginx and your css and javascript files should now be gzip compressed.

For more information on NGinx's gzip module, look at http://wiki.nginx.org/NginxHttpGzipModule.