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*

    Update the list of system packages:

    sudo apt-get update  
    

    Next, let's download all the programs we need to compile and test:

    sudo apt-get install build-essential zlib1g-dev libpcre3 libpcre3-dev unzip uuid-dev  
    

    Then create a subdirectory in your home directory to download Nginx packages.

    mkdir ~/custom-nginx  
    

    Open it up:

    cd ~/custom-nginx  
    

    Next, check the Nginx version

    [email protected]# nginx -v  
    nginx version: nginx/1.19.0
    
    

    And download the original Nginx package from official site.

    sudo wget http://nginx.org/download/nginx-1.19.0.tar.gz  
    
    [email protected]:~/custom-nginx# sudo wget http://nginx.org/download/nginx-1.19.0.tar.gz  
    --2020-07-01 14:44:06-- http://nginx.org/download/nginx-1.19.0.tar.gz
    Resolving nginx.org (nginx.org)... 62.210.92.35, 95.211.80.227, 2001:1af8:4060:a004:21::e3  
    Connecting to nginx.org (nginx.org)|62.210.92.35|:80... connected.  
    HTTP request sent, awaiting response... 200 OK  
    Length: 1043748 (1019K) [application/octet-stream]  
    Saving to: 'nginx-1.19.0.tar.gz'  
    
    nginx-1.19.0.tar.gz 100%[=======================================>] 1019K --.-KB/s in 0.1s  
    
    2020-07-01 14:44:06 (7.90 MB/s) - 'nginx-1.19.0.tar.gz' saved [1043748/1043748]
    
    

    Unpack the resulting archive:

    sudo tar zxvf nginx-1.19.0.tar.gz  
    

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

    ls ~/custom-nginx  
    

    The command will show the output:

    [email protected]:~/custom-nginx# ls ~/custom-nginx  
    nginx-1.19.0 nginx-1.19.0.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.19.0/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  
    
    [email protected]:~/custom-nginx/nginx-1.19.0/src/http/modules/ngx_pagespeed# sudo wget https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz  
    --2020-07-01 14:51:41-- https://dl.google.com/dl/page-speed/psol/1.13.35.2-x64.tar.gz
    Resolving dl.google.com (dl.google.com)... 172.217.22.110, 2607:f8b0:4006:810::200e  
    Connecting to dl.google.com (dl.google.com)|172.217.22.110|:443... connected.  
    HTTP request sent, awaiting response... 200 OK  
    Length: 18740791 (18M) [application/x-tar]  
    Saving to: '1.13.35.2-x64.tar.gz'  
    
    1.13.35.2-x64.tar.gz 100%[=======================================>] 17.87M 12.5MB/s in 1.4s  
    
    2020-07-01 14:51:43 (12.5 MB/s) - '1.13.35.2-x64.tar.gz' saved [18740791/18740791]  
    
    
    

    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.19.0/  
    

    See the configuration of nginx already installed on your system:

    # nginx -V
    
    [email protected]:~/custom-nginx/nginx-1.19.0# nginx -V  
    nginx version: nginx/1.19.0  
    built by gcc 7.4.0 (Ubuntu 7.4.0-1ubuntu1~18.04.1)  
    built with OpenSSL 1.1.1 11 Sep 2018  
    TLS SNI support enabled  
    configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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_temp --http-scgi-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_module --with-http_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_modulehttp_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_preread_module -with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.0/debian/debuild-base/nginx-1.19.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-as-needed -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/lib/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_temp --http-scgi-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_module --with-http_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_modulehttp_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_preread_module -with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.0/debian/debuild-base/nginx-1.19.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-as-needed -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:

    sudo apt-get install libssl-dev  
    и  
    sudo apt-get install openssl  
    
    [email protected]:~/custom-nginx/nginx-1.19.0# sudo ./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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_temp --http-scgi-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_module --with-http_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_modulehttp_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_preread_module -with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.0/debian/debuild-base/nginx-1.19.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-as-needed -pie' --add-module=./src/http/modules/ngx_pagespeed/  
    checking for OS  
     + Linux 4.15.0-106-generic x86_64
    checking for C compiler ... found  
     + using GNU C compiler
     + gcc version: 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04) 
    checking for gcc -pipe switch ... found  
    checking for --with-ld-opt="-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,--as-needed -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 ... 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 ioctl(FIONREAD) ... 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/lib/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:

    [email protected]:~/custom-nginx/nginx-1.19.0# nginx -V  
    nginx version: nginx/1.19.0  
    built by gcc 7.5.0 (Ubuntu 7.5.0-3ubuntu1~18.04)  
    built with OpenSSL 1.1.1 11 Sep 2018  
    TLS SNI support enabled  
    configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --modules-path=/usr/lib/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_temp --http-scgi-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_module --with-http_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_modulehttp_sub_module --with-http_v2_module --with-mail --with-mail_ssl_module --with-stream --with-stream_realip_module --with-stream_ssl_preread_module -with-cc-opt='-g -O2 -fdebug-prefix-map=/data/builder/debuild/nginx-1.19.0/debian/debuild-base/nginx-1.19.0=. -fstack-protector-strong -Wformat -Werror=format-security -Wp,-D_FORTIFY_SOURCE=2 -fPIC' --with-ld-opt='-Wl,-Bsymbolic-functions -Wl,-z,relro -Wl,-z,now -Wl,-as-needed -pie' --add-module=./src/http/modules/ngx_pagespeed/  
    

    Include ngx_pagespeed

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

    First you need to create a folder where the module will store the cache of the site files:

    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 www-data:www-data /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 and save your changes:

    ##
    # Pagespeed Settings
    ##
    pagespeed on;  
    pagespeed FileCachePath /var/ngx_pagespeed_cache;  
    

    The /etc/nginx/nginx.conf file now looks like this

    file

    Add the following to server-block:

    # 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" { }  
    

    These settings configure pagespeed to optimize sites.

    Restart Nginx to update the settings:

    sudo service nginx restart  
    

    To make sure that the ngx_pagespeed module works, run the Nginx binary.

    sudo /usr/sbin/nginx -V  
    

    If the installation was successful, the ngx_pagespeed module will appear in the list among other user arguments:

    nginx version: nginx/1.19.0  
    configure arguments: --add-module=./src/http/modules/ngx_pagespeed/  
    

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

    curl -I -p IP_your_server  
    

    You should see the following output:

    [email protected]:~# curl -I -p 5.187.7.109  
    HTTP/1.1 200 OK  
    Server: nginx  
    Content-Type: text/html  
    Connection: keep-alive  
    Vary: Accept-Encoding  
    ETag: "2aa6-58052495e6b7c"  
    Date: Wed, 01 Jul 2020 14:54:50 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 you have successfully installed Ngx_pagespeed on the server.


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