Category Archives: nginx

nginx and gzipped files

Modern web browsers are capable of decompressing/deflating gzipped files on the fly, so I wanted to be able to view the content of the compressed files stored under /usr/share/doc (i.e. Debian’s docs).

The web server I’m using nginx 1.2.1 (Debian’s Wheezy), ships with a proper config file:

       location /doc/ {
                alias /usr/share/doc/;
                autoindex on;
                allow ::1;
                deny all;

When clicking on a .gz file under /doc/ however, my web browser however (google’s Chrome) was asking me to save the file!.

Troubleshooting this turns on to be that the server is sending a Content-Type: application/octet-stream, make sense for general binary files.

In order to let a web browser capable of deflating gzipped files to handle them properly, we need a Content-Type which can be viewed (e.g. text/plain or text/html) and we need to tell the web browser about the encoding type i.e. Content-Encoding: gzip header for the .gz files.

One solution is to use the nginx’s location directive to identify these files in order to add the proper headers:

location ~ ^/doc/(.*)\.gz$ {
  alias /usr/share;
  gzip off;
  add_header Content-Encoding gzip;
  default_type text/plain;
  try_files $uri $uri =404;

If you’re using Debian, you may add the above to /etc/nginx/sites-available/default inside the server directive

If you like to access this over the LAN, just adding an allow x.y.z.0/24; before the deny all; inside the location /doc/ directive would do.

Another annoying thing, since most file under /usr/share/doc don’t have an extension and they’re mostly text file add a default_type text/plain; to the location /doc/ directive