Skip to content

Instantly share code, notes, and snippets.

@neatshell
Last active April 26, 2020 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neatshell/7b45a95a8f16567d543b239bcd682a24 to your computer and use it in GitHub Desktop.
Save neatshell/7b45a95a8f16567d543b239bcd682a24 to your computer and use it in GitHub Desktop.
Add dynamic nginx-brotli-module to nginx

Pre-requisites:

nginx installed with homebrew from the core tap

git installed and all the software needed to build something from source

Getting sources:

First of all, you need to get your currently installed nginx version simply by executing:

nginx -v

You should obtain something like this:

nginx version: nginx/1.17.10

The 1.17.10 is the version number that you must use to get the nginx source code you can create a VAR called nginx_version, and use this in further commands.

download and extract nginx sources:

Create a new folder that will be further mentioned as "working folder", cd into it and:

curl https://nginx.org/download/nginx-${nginx_version}.tar.gz | tar -xz -C .

you should have a new folder called nginx-${nginx_version}

getting ngx_brotli sources:

git clone https://github.com/google/ngx_brotli.git
cd ngx_brotli && git submodule update --init && cd -

Compiling ngx_brotli module:

You should be in you working folder with two folders: nginx-${nginx_version} and ngx_brotli

cd nginx-${nginx_version}
./configure --with-compat --add-dynamic-module=../ngx_brotli

this will build nginx with ngx_brotli module

make modules

this will generate the *.so files, ready to be placed under the modules folder of your local installed version of nginx.

coping the modules under the modules/ folder:

At the time of writing, the version 1.17.10 installed with homebrew, doesn't set a specific modules path and in this case you have to create it under the prefix folder of your nginx.

Anyway, running this command will tell you if the modules-path has been specified at compile-time for your current nginx binary:

nginx -V &>/dev/stdout | sed 's/--/\
 --/g' | grep 'modules-path=' | awk -F '=' '{print $NF}'

If this command doesn't output anything, you must create the modules folder under nginx prefix's folder. Running the following, you'll get the prefix folder of your installed nginx:

nginx -V &>/dev/stdout | sed 's/--/\
 --/g' | grep 'prefix=' | awk -F '=' '{print $NF}'

In my case, I get /usr/local/Cellar/nginx/1.17.10 so:

mkdir /usr/local/Cellar/nginx/1.17.10/modules

if the previous return something like: mkdir: /usr/local/Cellar/nginx/1.17.10/modules: File exists

you have the modules folder already created, problably because another dynamic module has been already installed.

Now that you got the modules folder's path, you can copy the previously compiled *.so files

cp objs/*.so /usr/local/Cellar/nginx/1.17.10/modules

Configure nginx:

Having added the brotli modules under your modules path, the last remaining step is to load them in the top-level (“main”) context of the main nginx configuration file (nginx.conf), above the http block, with load_module directive:

load_module modules/ngx_http_brotli_filter_module.so;
load_module modules/ngx_http_brotli_static_module.so;

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