fbpx

NGINX

Install NGINX with Brotli module in CentOS/RHEL or Amazon Linux

by , , revisited on


We have by far the largest RPM repository with NGINX module packages and VMODs for Varnish. If you want to install NGINX, Varnish, and lots of useful performance/security software with smooth yum upgrades for production use, this is the repository for you.
Active subscription is required.

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 the RPM package for the dynamic Brotli module for NGINX.
The dynamic module is compatible with the latest stable/mainline NGINX for CentOS/RHEL or Amazon Linux.

Step 1. Add GetPageSpeed extras RPM repository

Run the following command to add our repository:

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

Step 2. Install NGINX and Brotli module

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

sudo yum -y install nginx nginx-module-brotli

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 the 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. You may want to check out our guide on serving pre-compressed brotli files with NGINX’s brotli_static module.

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;
brotli_comp_level 4;

A more complete MIME type list has been suggested here:

application/atom+xml
application/geo+json
application/javascript
application/json
application/ld+json
application/manifest+json
application/rdf+xml
application/rss+xml
application/vnd.ms-fontobject
application/wasm
application/x-font-opentype
application/x-font-truetype
application/x-font-ttf
application/x-javascript
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
application/xml+rss
font/eot
font/opentype
font/otf
image/bmp
image/svg+xml
image/vnd.microsoft.icon
image/x-icon
image/x-win-bitmap
text/cache-manifest
text/calendar
text/css
text/javascript
text/markdown
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy
text/xml

Now we’ve listed all the MIME types of files which will be Brotli-compressed.

We choose compression level 4 over the default 6, because the sweet spot of Brotli’s compression vs processor load, compared to Gzip, is at level 4. At that level, the level of compression is already higher than Gzip’s default, while also finishing compression more quickly.

To apply the changes, reload your NGINX (if it was running), or start it.

PageSpeed module compatibility

As it stands, the PageSpeed NGINX module does not support Brotli compression internally.
You can use if together with Brotli NGINX module, but you will have to disable internal PageSpeed compression via:

pagespeed HttpCacheCompressionLevel 0;

This will ensure that the Brotli compression works, at the cost of storing optimized assets uncompressed.

Verify Brotli compression works

Via Command-Line

It’s easiest to launch the 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 🙂

  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-brotli”
    No package nginx-module-brotli available.

    Reply
    • Danila Vershinin

      Have you run yum install https://extras.getpagespeed.com/release-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
  5. Scott MacDonald

    Installing this broke nginx for me. Any ideas?

    running sudo nginx -t complains about conflicts with a lot of modules, such as:

    nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_geoip_module.so" version 1012002 instead of 1016001 in /usr/share/nginx/modules/mod-http-geoip.conf:1
    nginx: configuration file /etc/nginx/nginx.conf test failed

    and

    nginx: [emerg] module "/usr/lib64/nginx/modules/ngx_http_image_filter_module.so" version 1012002 instead of 1016001 in /usr/share/nginx/modules/mod-http-image-filter.conf:1
    nginx: configuration file /etc/nginx/nginx.conf test failed

    Reply
    • Danila Vershinin

      Hi Scott,

      Try running yum list installed | grep nginx and post the output here.
      Without looking, it seems that you have installed NGINX from EPEL repository (?) or elsewhere.
      … Which is more or less impossible though, because the nginx-module-brotli has a hard dependency on newer (than at least EPEL provides) NGINX version.

      Reply
      • Scott MacDonald

        I did indeed install nginx from epel, pretty new CentOS install from last week. Looks like when I installed with the instructions above it updated to nginx from your repo, but all my modules are still from the epel nginx version?

        Here’s my results from yum list installed | grep nginx

        nginx.x86_64 1:1.16.1-1.el7.ngx @getpagespeed-extras
        nginx-all-modules.noarch 1:1.12.2-3.el7 @epel
        nginx-filesystem.noarch 1:1.12.2-3.el7 @epel
        nginx-mod-http-geoip.x86_64 1:1.12.2-3.el7 @epel
        nginx-mod-http-image-filter.x86_64 1:1.12.2-3.el7 @epel
        nginx-mod-http-perl.x86_64 1:1.12.2-3.el7 @epel
        nginx-mod-http-xslt-filter.x86_64 1:1.12.2-3.el7 @epel
        nginx-mod-mail.x86_64 1:1.12.2-3.el7 @epel
        nginx-mod-stream.x86_64 1:1.12.2-3.el7 @epel
        nginx-module-brotli.x86_64 1:1.16.1.0.1.2-1.el7.gps @getpagespeed-extras
        python2-certbot-nginx.noarch 0.36.0-1.el7 @epel

        Reply
        • Danila Vershinin

          The official NGINX package (the one you installed from our repo is the exact copy of it) does not obsolete all NGINX-related module packages from EPEL. You can cleanup the EPEL NGINX packages via something like: yum remove $(yum list installed | grep 'nginx.*@epel' | awk '{ print $1 }')

          Reply
          • Scott MacDonald

            Thanks, removed the incompatible packages and all seems to be working well.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

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