Objective: To provide instructions for building a dynamic module, to give an understanding of the principle of building a dynamic module for
Nginx supplied from the Debian repository .
Target audience: Server administrators, advanced website administrators, SEOs, and just those who want to achieve a good rating from the
Google PageSpeed service.
There are many articles on this topic in search that shine, but in my opinion, none have been disclosed as it should. Yes and no explanatory explanation. For an inexperienced user, this can be difficult, and most of the articles explain how to build Nginx along with the module, and only a few show that it is possible to assemble a dynamic one.
')
The action plan is:
1. We rent vps for an hour.
2. We build a dynamic module
on the rented vps
on the same Nginx version that was used in the combat (
it is IMPORTANT !!! .
Nginx started supporting dynamic modules from version 1.9.11 ).
3. Transfer the module to the new server.
4. Configure Nginx.
5. Apply the settings.
6. Freeze the Nginx version (
If this is not done, the next time Nginx is updated, the server will crash. The reason is that the compiled module will work only with a specific version of Nginx. If you want to upgrade, compile the module for the new version and upgrade ).
Or we watch that we update and in due time we add the new assembled module.
The advantage is that the combat server is not subject to change, and if for example you donβt like ngx_pagespeed, then you simply remove the lines that configure it and the module itself. Well, if you use multiple servers with Nginx, you can once compile the module for them all.
Good links to similar articles, but not enough disclosed in my opinion:
β
Install Nginx ngx_pagespeed Module on Ubuntu 16.04β
How to Install Nginx and Google PageSpeed ββon Debian / Ubuntuβ
SETUP NGINXOfficial ngx_pagespeed links
β
ngx_pagespeed - gihubβ
Documentation ngx_pagespeedWe proceed to the instructions.
Connect to the test server via ssh.
Install
apt-src .
apt install apt-src
Add the source code repository and package repository with the required version of Nginx. I have this
Nginx / 1.13.5 echo -e 'deb-src http://ftp.ru.debian.org/debian sid main\ndeb http://ftp.ru.debian.org/debian sid main' >> /etc/apt/sources.list
Update the package cache.
apt update apt-src update
Download the Nginx source in the current folder.
apt-src install nginx
In the process of executing the command, not only all the source codes will be downloaded, but also all the necessary dependencies will be installed.
Install the necessary dependencies for the module:
apt install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip ca-certificates
Check the
current version of the module.
We indicate the version to install.
NPS_VERSION=1.12.34.3-stable
Download the source of the module:
wget https://github.com/pagespeed/ngx_pagespeed/archive/v${NPS_VERSION}.zip
Unpack the current folder:
unzip v${NPS_VERSION}.zip
Prepare the source for the assembly:
cd ngx_pagespeed-${NPS_VERSION}/ NPS_RELEASE_NUMBER=${NPS_VERSION/stable/} psol_url=https://dl.google.com/dl/page-speed/psol/${NPS_RELEASE_NUMBER}.tar.gz [ -e scripts/format_binary_url.sh ] && psol_url=$(scripts/format_binary_url.sh PSOL_BINARY_URL) wget ${psol_url} tar -xzvf $(basename ${psol_url})
Now we will add information about our module to the raws so that it will be gathered together with Nginx, we will first learn the path to the raw materials of our module.
pwd nano ../nginx-1.13.5/debian/rules
We see the highlighted path after the first command in order to add it to the rules.
--add-dynamic-module=/root/ngx_pagespeed-1.12.34.3-stable

We proceed to the assembly before returning to a higher level.
cd ../ apt-src build nginx
During the assembly process, a question will be asked, we answer it no.

After a successful build, I found the module at:
./nginx-1.13.5/debian/build-extras/objs/ngx_pagespeed.so
On the combat server we place in the folder:
/usr/lib/nginx/modules
Next in the file:
/etc/nginx/nginx.conf
add module loading:
load_module modules/ngx_pagespeed.so;
If you want to use the module in all sites, specify these two lines in the same place.
If the folder that you specify does not exist, create it and assign it the rights of the nginx user.
mkdir -p /var/cache/nginx/ngx_pagespeed chown www-data:www-data /var/cache/nginx/ngx_pagespeed

In the virtual host of sites on which we want the module to work, we add:
location ~ "\.pagespeed\.([az]\.)?[az]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; } location ~ "^/pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { }
In the virtual hosts of sites on which it should not work we add:
pagespeed off;
I will be glad to any comments, if something did not work out for you, write. I will supplement the article as questions arise.