Skip to content

Instantly share code, notes, and snippets.

@elpescador-nl
Created May 24, 2020 15:17
Show Gist options
  • Save elpescador-nl/acff26c4f10c5e3c2cc58f7225a71ceb to your computer and use it in GitHub Desktop.
Save elpescador-nl/acff26c4f10c5e3c2cc58f7225a71ceb to your computer and use it in GitHub Desktop.
Build nginx with brotli on Ubuntu 18.04

Implement brotli in nginx on Ubuntu 18.04

Build the packages

Install dependencies for building packages...

sudo apt install dpkg-dev build-essential zlib1g-dev libpcre3 libpcre3-dev unzip

Go to build folder...

cd /usr/local/src

Get brotli module sources...

sudo git clone https://github.com/google/ngx_brotli

Get nginx sources and build dependencies

sudo apt source nginx
sudo apt build-dep nginx -y

Get into folder for nginx

cd /usr/local/src/nginx-*/

Edit the rules

sudo nano debian/rules

Put --add-module=/usr/local/src/ngx_brotli to all environments, remember to put a \ at the end of the last option.

sudo nano debian/changelog

Enter a package version that represents the current custom version, for example:

nginx (1.14.0-0ubuntu1.1-brotli) bionic; urgency=medium

Run the dpkg command below.

sudo dpkg-buildpackage -b -uc -us
  • b: Build binary-only, no source files.
  • uc: Unsigned .buildinfo and .changes file.
  • us: unsigned source package.

When the build is complete, you will get the nginx-*.deb packages on the '/usr/local/src' directory as shown below.

Install the packages

Install the packages using the following command:

sudo dpkg -i nginx_1.14.0-0ubuntu1.1-brotli_all.deb nginx-common_1.14.0-0ubuntu1.1-brotli_all.deb nginx-core_1.14.0-0ubuntu1.1-brotli_amd64.deb libnginx-mod-stream_1.14.0-0ubuntu1.1-brotli_amd64.deb libnginx-mod-mail_1.14.0-0ubuntu1.1-brotli_amd64.deb libnginx-mod-http-xslt-filter_1.14.0-0ubuntu1.1-brotli_amd64.deb libnginx-mod-http-image-filter_1.14.0-0ubuntu1.1-brotli_amd64.deb libnginx-mod-http-geoip_1.14.0-0ubuntu1.1-brotli_amd64.deb

Once installed check the current version and see if the module is compiled within the package:

nginx -V

Configure nginx

Now edit the nginx config file.

sudo nano /etc/nginx/nginx.conf 

And add the brotli configuration to the http {} part:

    brotli on;
    brotli_comp_level 6;
    brotli_static on;
    brotli_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/vnd.microsoft.icon image/bmp image/svg+xml;

Check the configuration file:

sudo nginx -t

...and restart the nginx server

sudo service nginx restart

Hold current version

To prevent auto updates of the custom packages hold the current versions:

sudo apt-mark hold nginx*
sudo apt-mark hold libnginx*

Based on these tutorials: https://www.howtoforge.com/tutorial/how-to-install-nginx-with-brotli-compression-on-ubuntu-1804/ https://www.digitalocean.com/community/tutorials/how-to-add-ngx_pagespeed-to-nginx-on-ubuntu-14-04

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment