Nginx with gzip static (gzip_static) support on Arch Linux ABS

Written by
Date: 2010-03-29 10:36:30 00:00

Nginx is a web server / proxy server, according to Wikipedia, this is the description:

nginx (pronounced as "engine X") is a lightweight, high performance web server/reverse proxy and e-mail (IMAP/POP3) proxy, licensed under a BSD-like license. It runs on UNIX, GNU/Linux, BSD variants, Mac OS X, Solaris, and Microsoft Windows[2]. According to Netcraft, 7% of all domains on the internet use nginx.

It is really fast serving static content, so a lot of sites are using it as a front end to Apache, where Nginx is only serving the static content and proxying the dynamic content to be processed by Apache.

Nginx, has a lot of features, and one I really like is its capacity to serve gzipped content, I mean it can gzip it on the fly before serving it, but this requires CPU power, but it can also sent previously gzipped content. For instance, you have a file custom_style.css and also have the same file in the same directory gzipped as custom_style.css.gz, this way if the browser accept gzipped files, Nginx will serve the .gz version, if not the not gzipped version.

As you can see this really efficient, and that is what IMHO defines Nginx efficiency.

Normally the most of distros versions of Nginx does not support gzip static out of the box, I have read that Ubuntu does, but if you are running nginx on an Arch Linux operating system, you will have to recompile it to support that version (maybe the same with Debian or CentOS), I will cover how to use ABS from Arch Linux to recompile Nginx to support this feature.

Install ABS

Arch Build System ABS lets you re-compile Arch Linux software from source to add or removes features, in way that is still pacman who takes care for the install un-install package, so lets install ABS on our system.

sudo pacman -Sy abs base-devel

Download the tree from ABS

I will only download the info for nginx, and not the whole tree

sudo abs community/nginx

Copy the downloaded info your build directory

mkdir $HOME/abs

cd $HOME/abs

cp -r /var/abs/community/nginx/ $HOME/abs

Edit the PKGBUILD file

vim $HOME/build/nginx/PKGBUILD

look for this section:

./configure \
    --prefix=${_server_root} \
    --sbin-path=/usr/sbin/nginx \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --http-client-body-temp-path=${_tmp_path}/client_body_temp \
    --http-proxy-temp-path=${_tmp_path}/proxy_temp \
    --http-fastcgi-temp-path=${_tmp_path}/fastcgi_temp \
    --http-log-path=${_log_path}/access.log \
    --error-log-path=${_log_path}/error.log \
    --user=${_user} --group=${_group} \
    --with-imap --with-imap_ssl_module --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_dav_module \
    --with-ipv6 

    make || return 1

And add this line –with-http_gzip_static_module after –with-ipv6, I will look like this:

./configure \
    --prefix=${_server_root} \
    --sbin-path=/usr/sbin/nginx \
    --pid-path=/var/run/nginx.pid \
    --lock-path=/var/lock/nginx.lock \
    --http-client-body-temp-path=${_tmp_path}/client_body_temp \
    --http-proxy-temp-path=${_tmp_path}/proxy_temp \
    --http-fastcgi-temp-path=${_tmp_path}/fastcgi_temp \
    --http-log-path=${_log_path}/access.log \
    --error-log-path=${_log_path}/error.log \
    --user=${_user} --group=${_group} \
    --with-imap --with-imap_ssl_module --with-http_ssl_module \
    --with-http_stub_status_module \
    --with-http_dav_module \
    --with-ipv6 \
    --with-http_gzip_static_module

    make || return 1

Compile the software

cd ~/abs/nginx

makepkg -s

And install it

sudo pacman -U nginx-0.7.65-1-i686.pkg.tar.gz

Your version may be different

Finally configure it to server gzipped files

Add this line to the http section of your /etc/nginx/conf/nginx.conf file

gzip              on;
gzip_buffers      16 8k;
gzip_comp_level   9;
gzip_http_version 1.1;
gzip_min_length   10;
gzip_types        text/plain text/css image/png image/gif image/jpeg application/x-javascript text/xml application/xml application/xml+rss text/javascript;
gzip_vary         on;
gzip_static       on;
gzip_proxied      any;
gzip_disable      "MSIE [1-6]\.";

Restart the server

sudo /etc/rc.d/nginx restart

Now you have nginx serving static gzipped content, and gzipping on the fly those files that does not have a gzipped version.