ngx_pagespeed (or simply pagespeed) is a Nginx module designed to automatically optimize the website by reducing its load time in the browser.

Source code and dependencies download*

First of all, you will need to update your system to the latest stable version:

sudo yum update -y  

Next, download all the programs you need to compile and test:

sudo yum install wget curl unzip gcc-c++ pcre-devel zlib-devel  

Then create a folder in your home directory to download the Nginx package.

mkdir ~/custom-nginx  

Open this folder:

cd ~/custom-nginx  

Next, download the Nginx source package from official site.

sudo wget http://nginx.org/download/nginx-1.16.1.tar.gz  
[root@kvmde54-19861 custom-nginx]# sudo wget http://nginx.org/download/nginx-1.16.1.tar.gz  
--2020-06-24 15:15:43-- http://nginx.org/download/nginx-1.16.1.tar.gz
Resolving nginx.org (nginx.org)... 2001:1af8:4060:a004:21::e3, 62.210.92.35, 95.211.80.227  
Connecting to nginx.org (nginx.org)|2001:1af8:4060:a004:21::e3|:80... connected.  
HTTP request sent, awaiting response... 200 OK  
Length: 1032630 (1008K) [application/octet-stream]  
Saving to: 'nginx-1.16.1.tar.gz'  

100%[========================================================>] 1,032,630 --.-K/s in 0.1s  

2020-06-24 15:15:43 (10.1 MB/s) - 'nginx-1.16.1.tar.gz' saved [1032630/1032630]  

Unpack the resulting archive:

sudo tar zxvf nginx-1.16.1.tar.gz  

Query the content of the ~/custom-nginx folder:

ls ~/custom-nginx  

The command will show the output:

[root@kvmde54-19861 custom-nginx]# ls ~/custom-nginx
nginx-1.16.1 nginx-1.16.1.tar.gz  

To add the ngx_pagespeed module, you must first open the modules folder in the nginx-1.16.1 directory:

cd nginx-1.16.1/src/http/modules/  

Download the ngx_pagespeed archive from Github repository into this directory.

sudo wget https://github.com/pagespeed/ngx_pagespeed/archive/master.zip  

After the download is complete, unzip the archive:

sudo unzip master.zip  

Rename it to ngx_pagespeed for convenience:

sudo mv incubator-pagespeed-ngx-master ngx_pagespeed  

Open the directory:

cd ngx_pagespeed  

Download the PageSpeed Optimization Libraries (psol) package you need to compile into it:

sudo wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz  

Finally, extract the psol archive into the ngx_pagespeed directory:

sudo tar -xzvf 1.13.35.2-x64.tar.gz  

Setting up and compiling the source code

Now you need to edit the Nginx code and add the pagespeed module to it. Open the Nginx parent code directory:

cd ~/custom-nginx/nginx-1.16.1/  

See the configuration of nginx already installed on your system:

# nginx -V
nginx -V  
nginx version: nginx/1.16.1  
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-36) (GCC)  
built with OpenSSL 1.0.2k-fips 26 Jan 2017  
TLS SNI support enabled  
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_tempscgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_modulehttp_gunzip_module -with-http_gzip_static_module -with-http_mp4_module -with-http_random_index_module -with-http_realip_module -with-http_secure_link_module -with-http_slice_module -with-http_ssl_module -with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' -with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie'

Build nginx from source:

# sudo ./configure __parameters_of_old_nginx_ --add-module=:
sudo ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_tempscgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_modulehttp_gunzip_module -with-http_gzip_static_module -with-http_mp4_module -with-http_random_index_module -with-http_realip_module -with-http_secure_link_module -with-http_slice_module -with-http_ssl_module -with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' -with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/src/http/modules/ngx_pagespeed/  

An error may occur after trying to build Nginx:

./configure: error: SSL modules require the OpenSSL library.
You can either do not enable the modules, or install the OpenSSL library  
into the system, or build the OpenSSL library statically from the source  
with nginx by using --with-openssl=<path>option.  

If this is the case, you should install the missing packages and try again:

yum install libssl-dev  
и  
yum install -y openssl-devel  
[root@kvmde54-19861 nginx-1.16.1]# sudo ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginxconf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_tempscgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_modulehttp_gunzip_module -with-http_gzip_static_module -with-http_mp4_module -with-http_random_index_module -with-http_realip_module -with-http_secure_link_module -with-http_slice_module -with-http_ssl_module -with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' -with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/src/http/modules/ngx_pagespeed/
checking for OS  
 + Linux 3.10.0-1127.8.2.el7.x86_64 x86_64
checking for C compiler ... found  
 using GNU C compiler
 + gcc version: 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC) 
checking for gcc -pipe switch ... found  
checking for --with-ld-opt="-Wl,-z,relro -Wl,-z,now -pie" ... found  
checking for -Wl,-E switch ... found  
checking for gcc builtin atomic operations ... found  
checking for C99 variadic macros ... found  
checking for gcc variadic macros ... found  
checking for gcc builtin 64 bit byteswap ... found  
checking for unistd.h ... found  
checking for inttypes.h ... found  
checking for limits.h ... found  
checking for sys/filio.h ... not found  
checking for sys/param.h ... found  
checking for sys/mount.h ... found  
checking for sys/statvfs.h ... found  
checking for crypt.h ... found  
checking for Linux specific features  
checking for epoll ... found  
checking for EPOLLRDHUP ... found  
checking for EPOLLEXCLUSIVE ... not found  
checking for O_PATH ... found  
checking for sendfile() ... found  
checking for sendfile64() ... found  
checking for sys/prctl.h ... found  
checking for prctl(PR_SET_DUMPABLE) ... found  
checking for prctl(PR_SET_KEEPCAPS) ... found  
checking for capabilities ... found  
checking for crypt_r() ... found  
checking for sys/vfs.h ... found  
checking for poll() ... found  
checking for /dev/poll ... not found  
checking for kqueue ... not found  
checking for crypt() ... not found  
checking for crypt() in libcrypt ... found  
checking for F_READAHEAD ... not found  
checking for posix_fadvise() ... found  
checking for O_DIRECT ... found  
checking for F_NOCACHE ... not found  
checking for directio() ... not found  
checking for statfs() ... found  
checking for statvfs() ... found  
checking for dlopen() ... not found  
checking for dlopen() in libdl ... found  
checking for sched_yield() ... found  
checking for sched_setaffinity() ... found  
checking for SO_SETFIB ... not found  
checking for SO_REUSEPORT ... found  
checking for SO_ACCEPTFILTER ... not found  
checking for SO_BINDANY ... not found  
checking for IP_TRANSPARENT ... found  
checking for IP_BINDANY ... not found  
checking for IP_BIND_ADDRESS_NO_PORT ... found  
checking for IP_RECVDSTADDR ... not found  
checking for IP_SENDSRCADDR ... not found  
checking for IP_PKTINFO ... found  
checking for IPV6_RECVPKTINFO ... found  
checking for TCP_DEFER_ACCEPT ... found  
checking for TCP_KEEPIDLE ... found  
checking for TCP_FASTOPEN ... found  
checking for TCP_INFO ... found  
checking for accept4() ... found  
checking for kqueue AIO support ... not found  
checking for Linux AIO support ... found  
checking for int size ... 4 bytes  
checking for long size ... 8 bytes  
checking for long long size ... 8 bytes  
checking for void * size ... 8 bytes  
checking for uint32_t ... found  
checking for uint64_t ... found  
checking for sig_atomic_t ... found  
checking for sig_atomic_t size ... 4 bytes  
checking for socklen_t ... found  
checking for in_addr_t ... found  
checking for in_port_t ... found  
checking for rlim_t ... found  
checking for uintptr_t ... uintptr_t found  
checking for system byte ordering ... little endian  
checking for size_t size ... 8 bytes  
checking for off_t size ... 8 bytes  
checking for time_t size ... 8 bytes  
checking for AF_INET6 ... found  
checking for setproctitle() ... not found  
checking for pread() ... found  
checking for pwrite() ... found  
checking for pwritev() ... found  
checking for sys_nerr ... found  
checking for localtime_r() ... found  
checking for clock_gettime(CLOCK_MONOTONIC) ... found  
checking for posix_memalign() ... found  
checking for memalign() ... found  
checking for mmap(MAP_ANON|MAP_SHARED) ... found  
checking for mmap("/dev/zero", MAP_SHARED) ... found  
checking for System V shared memory ... found  
checking for POSIX semaphores ... not found  
checking for POSIX semaphores in libpthread ... found  
checking for struct msghdr.msg_control ... found  
checking for ioctl(FIONBIO) ... found  
checking for struct tm.tm_gmtoff ... found  
checking for struct dirent.d_namlen ... not found  
checking for struct dirent.d_type ... found  
checking for sysconf(_SC_NPROCESSORS_ONLN) ... found  
checking for sysconf(_SC_LEVEL1_DCACHE_LINESIZE) ... found  
checking for openat(), fstat() ... found  
checking for getaddrinfo() ... found  
configuring additional modules  
adding module in ./src/http/modules/ngx_pagespeed/  
mod_pagespeed_dir=./src/http/modules/ngx_pagespeed//psol/include  
build_from_source=false  
checking for psol ... found  
List of modules (in reverse order of applicability): ngx_http_write_filter_module ngx_http_header_filter_module ngx_http_chunked_filter_module ngx_http_v2_filter_module ngx_http_range_header_filter_module ngx_pagespeed_etag_filter ngx_http_gzip_filter_module ngx_pagespeed ngx_http_postpone_filter_module ngx_http_ssi_filter_module ngx_http_charset_filter_module ngx_http_sub_filter_module ngx_http_addition_filter_module ngx_http_gunzip_filter_module ngx_http_userid_filter_module ngx_http_headers_filter_module  
checking for psol-compiler-compat ... found  
 + ngx_pagespeed was configured
checking for PCRE library ... found  
checking for PCRE JIT support ... found  
checking for OpenSSL library ... found  
checking for zlib library ... found  
creating objs/Makefile  

Configuration summary  
  + using threads
  + using system PCRE library
  + using system OpenSSL library
  + using system zlib library

  nginx path prefix: "/etc/nginx"
  nginx binary file: "/usr/sbin/nginx"
  nginx modules path: "/usr/lib64/nginx/modules"
  nginx configuration prefix: "/etc/nginx"
  nginx configuration file: "/etc/nginx/nginx.conf"
  nginx pid file: "/var/run/nginx.pid"
  nginx error log file: "/var/log/nginx/error.log"
  nginx http access log file: "/var/log/nginx/access.log"
  nginx http client request body temporary files: "/var/cache/nginx/client_temp"
  nginx http proxy temporary files: "/var/cache/nginx/proxy_temp"
  nginx http fastcgi temporary files: "/var/cache/nginx/fastcgi_temp"
  nginx http uwsgi temporary files: "/var/cache/nginx/uwsgi_temp"
  nginx http scgi temporary files: "/var/cache/nginx/scgi_temp"

After completing the configuration, run the compilation:

sudo make  

After that, you can install the program:

sudo make install  

After that we will see that the module has been added:

[root@kvmde54-19861 nginx-1.16.1]# nginx -V
nginx version: nginx/1.16.1  
built by gcc 4.8.5 20150623 (Red Hat 4.8.5-39) (GCC)  
built with OpenSSL 1.0.2k-fips 26 Jan 2017  
TLS SNI support enabled  
configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib64/nginx/modules --conf-path=/etc/nginx/nginx.conf --error-log-path=/var/log/nginx/error.log --http-log-path=/var/log/nginx/access.log --pid-path=/var/run/nginx.pid --lock-path=/var/run/nginx.lock --http-client-body-temp-path=/var/cache/nginx/client_temp --http-proxy-temp-path=/var/cache/nginx/proxy_temp --http-fastcgi-temp-path=/var/cache/nginx/fastcgi_temp --http-uwsgi-temp-path=/var/cache/nginx/uwsgi_tempscgi-temp-path=/var/cache/nginx/scgi_temp --user=nginx --group=nginx --with-compat --with-file-aio --with-threads --with-http_addition_module --with-http_auth_request_module --with-http_dav_module --with-http_flv_modulehttp_gunzip_module -with-http_gzip_static_module -with-http_mp4_module -with-http_random_index_module -with-http_realip_module -with-http_secure_link_module -with-http_slice_module -with-http_ssl_module -with-http_stub_status_module --with-http_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_module --with-stream_ssl_preread_module --with-cc-opt='-O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector-strong --param=ssp-buffer-size=4 -grecord-gcc-switches -m64 -mtune=generic -fPIC' -with-ld-opt='-Wl,-z,relro -Wl,-z,now -pie' --add-module=/src/http/modules/ngx_pagespeed/

The custom Nginx installation is stored in /usr/local/nginx. Now we need to create two symbolic links

One is to the configuration file:

sudo ln -s /usr/local/nginx/conf/ /etc/nginx  

The configuration files will now be available in the /etc/nginx/ directory.

The second link is to the main binary in the directory /usr/sbin/. To do this, run the command:

sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/nginx  

Create startup script

The previous installation process creates the necessary user account and group with which to run Nginx

To begin, create a new file in the /etc/init.d/ directory:

sudo nano /etc/init.d/nginx  

Then go to this link, copy the script and paste it into the file.

Make the script executable by running the command:

sudo chmod +x /etc/init.d/nginx  

After that, you can start Nginx:

sudo service nginx start  

To have Nginx start and stop automatically with the server, add the following command to the startup levels:

sudo chkconfig nginx on  

Include the pagespeed module

After installing the Nginx server, you need to enable ngx_pagespeed.

But first you need to create a folder where the module can store the site file cache:

sudo mkdir -p /var/ngx_pagespeed_cache  

Pass the rights to this folder to the Nginx user, so that the web server has the necessary level of access.

sudo chown -R nobody:nobody /var/ngx_pagespeed_cache  

Open the main Nginx configuration file, nginx.conf, to edit:

sudo nano /etc/nginx/nginx.conf  

Add the following lines to the http block:

# Pagespeed main settings
pagespeed on  
pagespeed FileCachePath /var/ngx_pagespeed_cache  

The pagespeed configuration must be added to the server block and the changes saved:

# Ensure requests for pagespeed optimized resources go to the pagespeed
# handler and no extraneous headers get set.
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }  
location ~ "^/ngx_pagespeed_static/" { }  
location ~ "^/ngx_pagespeed_beacon" { }  

file

Restart Nginx:

sudo service nginx restart  

Now let's check if the module is working or not. You can check it by running the following command:

curl -I -p http://localhost  

You should see the following output:

[root@kvmde54-19861 /]# curl -I -p http://localhost
HTTP/1.1 200 OK  
Server: nginx  
Content-Type: text/html  
Connection: keep-alive  
Keep-Alive: timeout=30  
Date: Thu, 25 Jun 2020 15:07:35 GMT  
X-Page-Speed: 1.13.35.2-0  
Cache-Control: max-age=0, no-cache  

You should see X-Page-Speed and the version number in the output above. This means that you have successfully installed Ngx_pagespeed on the server.


If you have configuration difficulties or any additional questions, you can always contact our support team via ticket system.</path>

Updated July 3, 2020