TYPO free

home

fighting for TYPO free code

TYPO3 Nginx, enable gzip and expires headers

5 Aug 2009

Nginx comes with a lot of modules. You can read all about them on the Nginx wiki. The syntax of the configuration files differs from Apache syntax, but up to now everything I need is possible. There are also a lot of nifty new features like an empty gif configuration. We can use this to serve an empty gif right from memory without having to read the clear.gif file from disk:

location = /clear.gif {
empty_gif;
}

That's cool! :-) But setting up gzip and expires headers in Nginx is also easy.

NginxHttpGzipModule

Nginx comes with the NginxHttpGzipModule which can compress files 'on the fly'. Nginx also comes with a gzip pre-compression module, but that is not enabled in the Debian version. Here is my configuration for the 'on the fly' compression:

gzip on;
gzip_http_version 1.1;
gzip_min_length 1000;
gzip_buffers 16 8k;
gzip_disable "MSIE [1-6] \.";
gzip_types text/html text/css text/xml application/x-javascript application/atom+xml text/mathml text/plain text/vnd.sun.j2me.app-descriptor text/vnd.wap.wml text/x-component
gzip_vary on;

The Nginx 'context' for the gzip options is: http, server, location. I put this configuration on the http level because I want all text format files to be compressed.

NginxHttpHeadersModule

To send expires headers, you will need the HTTP headers module. This is the server level configuration I use to send expires headers:

if ($request_uri ~* "\.(ico|gif|png|jpe?g|css|js|swf)(\?v\d\.\d\.\d)?$") {
expires max;
break;
}

This will send expires headers for files like:

And YES, I want those files cached indefinitely.

Update, 2009 august 12

I now use the following to support two digit version numbers:

if ($request_uri ~* "\.(ico|gif|png|jpe?g|css|js|swf)(\?v\d\d?\.\d\d?\.\d\d?)?$") {
expires max;
break;
} 
ben 13 Jan 2010, 06:29
Thanks for the great snippets- appreciate it
Neil 17 Nov 2011, 17:51
Hey
Thanks for the nginx/typo3 exmaples, very helpful.

Just wanted to point out that you can specify repetitions in regular expressions e.g. to match a decimal of length 1 to 5 digits, you could use \d{1,4}

Or indeed you can specify any number of repetitions using a * e.g. \d*

Just thought that might be helpful

Cheers
Neil
Commenting is closed for this item