Nginx

Install NGINX with Brotli module in CentOS / RedHat 7

by ,


Brotli is the new compression algorithm, and it is now widely supported across many browsers. With Brotli being a clear winner over standard GZIP compression, not implementing it for your website is not just an oversight, but also negligence of its huge optimization potential.

You’ve been neglecting it, haven’t you? Time to fix things up 🙂

You can use our repository to easily install RPM package for dynamic Brotli module for NGINX.
The dynamic module is compatible with the latest stable NGINX for CentOS 7.

Step 1. Add GetPageSpeed extras YUM repository

Run the following command to add our repository:

yum install https://extras.getpagespeed.com/release-el7-latest.rpm

Step 2. Install NGINX and Brotli module

Then, all you have to do to install NGINX with Brotli module:

yum install nginx nginx-module-nbr

Follow the installation prompt to import GPG public key that is used for verifying packages.

Step 3. Enable Brotli support in NGINX

Once you have installed Brotli NGINX module package, you’ll notice it actually consists of the two separate modules. You may want to simply follow the installer’s suggestion:

----------------------------------------------------------------------

The Brotil dynamic modules for nginx have been installed.
To enable these modules, add the following to /etc/nginx/nginx.conf
and reload nginx:
    load_module modules/ngx_http_brotli_filter_module.so;
    load_module modules/ngx_http_brotli_static_module.so;

Please refer to documentation here:
https://github.com/eustas/ngx_brotli

----------------------------------------------------------------------

Doing so will enable both regular and static Brotli filters. But what is their purpose and why two?

The regular Brotli module, ngx_http_brotli_filter_module.so, will dynamically compress everything that NGINX serves to end visitors.

The static Brotli module, ngx_http_brotli_static_module.so, allows you to serve Brotli-encoded versions of your static files which you have pre-compressed in advance. More on that in the documentation.

So in most cases, you’re fine with just the regular Brotli module, but if you’re a hardcore optimizer you will pre-compress your files with Brotli’s highest compression level, and enable the second module as well.

Enable Brotli compression for all websites

Now that our NGINX has Brotli compression capability, you can start tinkering with configuration and actually enable the new compression algorithm.

The best way to do this, is by using conf.d auto-include facility of your NGINX distribution. Create the new file /etc/nginx/conf.d/brotli.conf and define your desired compression parameters there. The following minimalistic configuration will ensure Brotli compression for the majority of compressible file formats:

brotli on;
brotli_types text/xml image/svg+xml application/x-font-ttf image/vnd.microsoft.icon application/x-font-opentype application/json font/eot application/vnd.ms-fontobject application/javascript font/otf application/xml application/xhtml+xml text/javascript  application/x-javascript text/plain application/x-font-truetype application/xml+rss image/x-icon font/opentype text/css image/x-win-bitmap;

Now we’ve listed all the MIME types of files which will be Brotli-compressed. To apply the changes, reload your NGINX (if it was running), or start it.

Verify Brotli compression works

Via Command-Line

It’s easiest to launch Terminal app (if you have OS X) and check if your NGINX emits Brotli encoded responses.

Simply use curl like this:

curl -IL https://example.com -H "Accept-Encoding: br"

As long as you see the response includes Content-Encoding: br, it means that NGINX properly handles Brotli compression.

Using CLI to check Brotli support, implies at least some knowledge of working with the Terminal app. So you can use your browser method instead.

Via browser

  • Launch Chrome (what developer doesn’t have it?) and navigate to your website
  • Right-click anywhere on the opened page and choose “Inspect”.
  • Developer tools sidebar will open.
  • Click on “Network” in the developer tools sidebar, then “Doc”. This tells Chrome to log only requests to the main document.
  • Now reload the page, and expand the log entry which was created by clicking it.
  • Scroll down to “Response Headers” and find “Content-Encoding” header. The value of “br” means that the response was encoded via Brotli.
Brotli-encoded HTML in Chrome
Brotli-encoded HTML in Chrome

Congrats!

Feels like something is still missing? Then continue reading on how to install PageSpeed module in NGINX 🙂


Also published on Medium.

  1. Hugh Pratt

    I already have Nginx installed. Will this reinstall it? I wonder if I’ll screw up any modules etc. I had installed with yum install Nginx command. Not sure if any special modules were installed. Can I therefore simply install the Brotli only?

    Reply
    • Danila Vershinin

      I don’t know which repository you had installled Nginx from but most likely you’ll be offered upgrade to our repository’s Nginx. It should go smooth. If not sure, backup /etc/nginx first.

      Reply
      • Hugh Pratt

        Which version of Nginx will this be?

        Reply
        • Danila Vershinin

          Latest stable nginx 1.14.2 at present.

          Reply
          • Hugh Pratt

            Thank you. Yes that’s what I have. I use certs from Let’s Encrypt and ssl etc. All is setup in Nginx.conf. Will all that’s remain intact if I copy the .conf files back after install? Or do I have to change this at all? Can I just leave Nginx as it is and only additionally install Brotli and maybe Headers More? Or will I have to reinstall everything?

          • Danila Vershinin

            It depends on repository where you installed nginx from. If from official stable nginx repository then you will only install the module. Either way, it will either work or not. Your nginx.conf will not be affected.

  2. Hugh Pratt

    I installed nginx from generic yum. EPEL and REMI are in the list I think. Will I still be able to do this one above?

    Reply
    • Danila Vershinin

      Nothing bad will happen if you just try to follow the provided instructions 🙂 Let me know how it goes.

      Reply
      • Hugh Pratt

        Thank you, this is very helpful. I’ve installed it and it’s working. The content-encoding shows in Curl. However, questions:

        In your sample encoding types you have not included JPG, PNG, SVG files. Is this intentional?
        Secondly I have the gzip stuff from before. Should I comment it all out, or leave it and have Brotli as an additional parameter? Right now my nginx.conf has the following. I think the gzip stuff for MSIE6 etc may be not necessary anymore? But the listing of “gzip_types” has several file types that are not in your recommended Brotli list.

        Thanks!

        — NGINX.CONF —

        gzip                  on;
          gzip_static           on;
          gzip_disable          "MSIE [1-6].";
          gzip_disable          "msie6";
          gzip_vary             on;
          gzip_proxied          any;
          gzip_comp_level       9;
          gzip_buffers          128 16k;
          gzip_http_version     1.1;
          gzip_types            text/plain text/css
                                application/json application/javascript application/x-javascript text/javascript 
                                text/xml application/xml application/rss+xml application/atom+xml application/rdf+xml
                                image/png image/gif image/jpeg
                                ;
        
        brotli                on;
          brotli_types          text/xml    image/svg+xml   application/x-font-ttf 
                                image/vnd.  microsoft.icon  application/x-font-opentype 
                                application/json    font/eot   application/vnd.ms-fontobject   application/javascript 
                                font/otf    application/xml   application/xhtml+xml   text/javascript
                                application/x-javascript   text/plain     application/x-font-truetype   application/xml+$
                                image/x-icon    font/opentype   text/css   image/x-win-bitmap;
        
        Reply
        • Danila Vershinin

          SVG images are already included (there is image/svg+xml), while JPEG and PNG are not compressible formats. You should keep gzip directives as GZip encoding will still be used for clients that support only GZip and have no knowledge about Brotli.

          Reply
  3. nhson47ggs

    “yum install nginx nginx-module-nbr”
    No package nginx-module-nbr available.

    Reply
    • Danila Vershinin

      Have you run yum install https://extras.getpagespeed.com/release-el7-latest.rpm

      Reply
  4. Hien D. Nguyen

    Somehow when I run “yum install nginx” (step 2), it tries to install (nginx-1.16.0-1.el7.ngx.x86_64) which does exists and causes Error downloading packages:
    1:nginx-1.16.0-1.el7.ngx.x86_64: [Errno 256] No more mirrors to try.

    Any advises?

    Reply
    • Danila Vershinin

      Please provide complete command you run with complete output.

      Reply

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.