Brotli on Nginx

Brotli compression was introduced by Google, exactly 3 years ago. It is yet to come by default with the standard Nginx packages of OS vendors or the official Nginx repo. Google maintains its own repo for brotli support on Nginx web server. However, it’s been two years since we had the last commit. Fortunately, a Googler, named Eugene Kliuchnikov, continues to develop a fork of Google’s repo. There are even plan to merge both repos. Still, there isn’t an easy way to compile Nginx from source.

While searching the internet for any existing solutions, I found a couple of excellent solutions.

Both provide step by step guidelines on how to compile Nginx from source, along with brotli compression support. Since, I like to automate things, I put together all the steps mentioned by the above articles and created a single script that does the following…

  • Compiles Nginx from source with brotli support
  • Removes any existing installation of Nginx
  • Installs the new Nginx version (with brotli support, of course)
  • Starts or restarts Nginx

Here’s the complete code…

#!/usr/bin/env bash

# use it while developing / testing.
# you may use it in production as well.
# set -o errexit -o pipefail -o noclobber -o nounset
# set -x

# compile Nginx from the official repo with brotli compression

[ ! -d /root/log ] && mkdir /root/log

# logging everything
exec > >(tee -a ${log_file} )
exec 2> >(tee -a ${log_file} >&2)

# Defining return code check function
check_result() {
    if [ $1 -ne 0 ]; then
        echo "Error: $2"
        exit $1

export DEBIAN_FRONTEND=noninteractive

printf '%-72s' "Updating apt repos..."
apt-get -qq update
echo done.

printf '%-72s' "Installing pre-requisites..."
apt-get -qq install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip
echo done.

codename=$(lsb_release -c -s)

# function to add the official repo
nginx_repo_add() {
    distro=$(gawk -F= '/^ID=/{print $2}' /etc/os-release)
    codename=$(lsb_release -c -s)
    if [ "$codename" == "juno" ] ; then

    if [ "$distro" == "elementary" ] ; then

    [ -f nginx_signing.key ] && rm nginx_signing.key
    curl -LSsO
    check_result $? 'Nginx key could not be downloaded!'
    apt-key add nginx_signing.key &> /dev/null
    check_result $? 'Nginx key could not be added!'
    rm nginx_signing.key

    # for updated info, please see
    nginx_branch= # leave this empty to install stable version
    # or nginx_branch="mainline"

    if [ "$nginx_branch" == 'mainline' ]; then

    [ -f /etc/apt/sources.list.d/nginx.list ] && rm /etc/apt/sources.list.d/nginx.list
    echo "deb ${nginx_src_url} ${codename} nginx" > /etc/apt/sources.list.d/nginx.list
    echo "deb-src ${nginx_src_url} ${codename} nginx" >> /etc/apt/sources.list.d/nginx.list

    # finally update the local apt cache
    apt-get update -qq
    check_result $? 'Something went wrong while updating apt repos.'

case "$codename" in
        echo "Distro: $codename"
        echo 'Warning: Could not figure out the distribution codename. Continuing to install Nginx from the OS.'

cd /usr/local/src
apt-get source nginx
apt-get build-dep nginx -y

git clone --recursive

cd /usr/local/src/nginx-*/
# modify the existing config
grep -wq '--add-module=/usr/local/src/ngx_brotli' debian/rules || sed -i -e '/\.\/configure/ s:$: --add-module=/usr/local/src/ngx_brotli:' debian/rules

# build the updated pacakge
dpkg-buildpackage -b

# optional
# install the updated package in the current server
cd /usr/local/src
dpkg -i nginx*.deb

# take a backup
[ ! -d ~/backups/ ] && mkdir ~/backups
mv nginx*.deb ~/backups/

# remove all the sources and apt sources file
cd ~/
rm -rf /usr/local/src/nginx*
rm -rf /usr/local/src/ngx_brotli
rm /etc/apt/sources.list.d/nginx.list
apt-get -qq update

# hold the package nginx from updating accidentally in the future by someone else!
apt-mark hold nginx

# stop the previously running instance, if any
nginx -t && systemctl stop nginx

# start the new Nginx instance
nginx -t && systemctl start nginx

This has been made as a Github repo for Nginx Brotli compression. Please check it out for updates and support.

Please remember that Brotli is a new standard (even though, it is 3 years old). It works best if assets are compressed already. Nginx takes time to compress on the fly, especially using brotli. If you still wish to use it dynamically, you may want to check it Akamai’s in-depth research on how brotli fairs with gzip in terms of size and speed. Nevertheless, brotli does make a site a bit leaner, thus faster!

Oops Moments in DevOps

A post on a new year is usually about resolutions! But, isn’t the best time to revisit the last year’s mistakes and resolve to never repeat it this year (and years to come)?! Since, my strong skills are with DevOps, I’d like to share some oops moments (you may call them blunders) that you’d never want to do it, if you are starting on DevOps or if you just want to understand where things go wrong in DevOps. In general, you go by the defaults, you’d be in trouble in the future. Whatever software you use, make sure you understand the default values and what each of them does! Here are the top three mistakes that I did… Continue reading “Oops Moments in DevOps”

WP Rocket – Nginx configuration

I prefer open-source software and have been a long-time advocate of OSS in general. Recently, I started liking WP Rocket plugin that offers some unique features. I already have a perfect Nginx configuration for WP Super Cache plugin (that I consider as the best full-cache plugin till date). Since, WP Rocket uses disk caching like WPSC, I wanted to quickly convert the existing configurations to fit WP Rocket. I did succeed in it and you can find it in my WordPress-Nginx repo. Here I explain how WP Rocket stores the cached content and how it could be integrated into Nginx.

Continue reading “WP Rocket – Nginx configuration”

W3 Total Cache configuration for Nginx-Apache server stack

During the past 24 hours, there were two people had the following situation and were looking for a solution. Their use-case is…

I’m currently using a Nginx frontend / Apache backend setup. The W3 Total Cache plugin detects Apache and will only show me the Apache rewrite rules.

Here’s another quote from the other person who was looking for a similar solution…

The nginx.conf file does not exist. W3 Total Cache plugin detects that Apache is running – thus gives me the rewrites for that webserver instead. I am using Nignx in front of Apache – not a Nginx/PHP-FPM solution

Interesting, but not uncommon. So, I dived in and modified my existing Nginx rules for WP Super Cache plugin and provided a unique solution. Continue reading “W3 Total Cache configuration for Nginx-Apache server stack”

Efficient 301 Redirects

Traditionally, web sites use either www or non-www version to display their content to the visitors. Sub-domains are also becoming popular in recent times. When www is chosen as the preferred domain when installing WordPress (or after installing WordPress), whenever a visitor types non-www version of the site, WordPress redirects the visitor to the correct URL, the www version the site, through an internal 301 redirect. Google recommends this 301 redirect and Google WebMaster Tools has an option to set the preferred domain too. Continue reading “Efficient 301 Redirects”

Fix Incorrect IP Address in WP Comments

Image of an IP logoThe cost of running a VPS is becoming cheaper and cheaper. There are more things we could get for the same bucks. Once your site is ready for a VPS, there are multiple server stack options available, than the traditional LAMP setup. For example, you could completely ignore Apache and can use Nginx with php-fpm . Either case, you wouldn’t have any issues related to IPs in comments. However, on a complicated setups, such as Varnish => Nginx => php-fpm or Nginx => Apache, or Varnish => Apache, WordPress doesn’t display the IP address of the visitors correctly. There is nothing wrong with WordPress. It’s all about the implementation. Forwarding the correct client IP can be tricky as the complexity of the server stack increases. There are situations where you just don’t have any options to forward the correct address. Continue reading “Fix Incorrect IP Address in WP Comments”

W3 Total Cache 1.0? – Sneak Peek!

Banner of W3 Total Cache pluginUpdate (Feb 5, 2013): The beta version that I tested, has been released as version with more features than mentioned below. So, this post is void as of February 5, 2013 (in less than 2 weeks of publishing it). :(-

W3 Total Cache is back in active development, nearly after a year. I’m one of the lucky people who got the opportunity to test the beta version of the upcoming release, probably! It brings a few new features, including a more-intuitive troubleshooter. Continue reading “W3 Total Cache 1.0? – Sneak Peek!”

Implementation is everything

Image #1 for QA series - Check out more posts tagged as NginxThere was an interesting question in the Nginx mailing list regarding replacing Varnish with Nginx as a load balancer. Let me quote the question directly here…

The question:

We are using Varnish in front of 3 load balanced web servers running apache.  We had migrated from one hosting platform where we had 1 app server and 1 database server using Varnish (Drupal 6.x) and had no issues.  Now that we are running in a load balanced environment (3 load balanced apache web servers, a Varnish server, and 1 database server) we are seeing mulitple examples of cacheing issues. (Pages not displaying correctly… style issues, data input staying cached and used on another page, etc).

We think we can just replace the Varnish server and use a NGinx server.  I don’t want to necessarily remove all the apache servers, but we have to get this cacheing issue corrected….

any thoughts…?

Continue reading “Implementation is everything”