How to Install ngx_pagespeed with Nginx, CWP, HestiaCP, VestaCP and on Linux OS

by Sandeep B.

In this tutorial I’ll show you how to enable and install ngx_pagespeed nginx module easily and with less configuration. It is compatible with only nginx stable release and tested on CWP, VestaCP and official Nginx.

ngx_pagespeed speeds up your site and reduces page load time by automatically applying web performance best practices to pages and associated assets (CSS, JavaScript, images) without requiring you to modify your existing content or workflow. Features include:

  • Image optimization: stripping meta-data, dynamic resizing, recompression
  • CSS & JavaScript minification, concatenation, inlining, and outlining
  • Small resource inlining
  • Deferring image and JavaScript loading
  • HTML rewriting
  • Cache lifetime extension

Follow this easy steps to enable ngx_pagespeed module in Nginx :

If you’re using mainline version of nginx please move to stable version of nginx in order to use this module

Updated on :  26th May, 2021
Google Pagespeed version :
For Stable Nginx 1.20.1 pagespeed module (tested on 64bit system CWP|Vesta and on custom env)

cd /usr/lib64/nginx
mkdir modules #skip if folder exists
cd modules
rm -rf ngx_pagespeed*
wget --no-cache
rm -rf


cd /etc/nginx/modules
rm -rf ngx_http_brotli*
wget --no-cache
rm -rf

How to update this module?

just follow the upper step and then update nginx (don’t update nginx before)

Now create pagespeed config for Nginx :

For CWP :

mkdir -p /var/ngx_pagespeed_cache
chown -R nobody:root /var/ngx_pagespeed_cache

For VestaCP :

mkdir -p /var/ngx_pagespeed_cache
chown -R nginx:root /var/ngx_pagespeed_cache

For Nginx installed manually from official repo :

mkdir -p /var/ngx_pagespeed_cache
chown -R user:root /var/ngx_pagespeed_cache

** For Nginx installed manually from official repo you need to replace user:root user with nginx user

Now add nginx module configuration on “nginx.conf” :
nginx.conf can be default found in the dir : /etc/nginx

edit /etc/nginx/nginx.conf

nano /etc/nginx/nginx.conf

then add this line to top of the config line i.e. on first line :

load_module "modules/";

Example nginx.conf :

load_module "modules/";
user nobody;
worker_processes auto;
#worker_rlimit_nofile    65535;
error_log               /var/log/nginx/error.log crit;
pid                     /var/run/;

events {
    worker_connections  1024;
    use                 epoll;
    multi_accept        on;

http {
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    client_header_timeout 3m;
    client_body_timeout 3m;
    client_max_body_size 256m;
    client_header_buffer_size 4k;
    client_body_buffer_size 256k;
    large_client_header_buffers 4 32k;
    send_timeout 3m;
    keepalive_timeout 60 60;
    reset_timedout_connection       on;
    server_names_hash_max_size 1024;
    server_names_hash_bucket_size 1024;
    ignore_invalid_headers on;
    connection_pool_size 256;
    request_pool_size 4k;
    output_buffers 4 32k;
    postpone_output 1460;

Now add this nginx pagespeed config on Nginx domain Vhosts under server { block :

#Pagespeed config
pagespeed on;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }

Example Nginx domain vhost :

server {
    access_log /usr/local/ bytes;
    access_log /usr/local/ combined;
    error_log /usr/local/ error;
    # Pagespeed
    pagespeed on;
    pagespeed FileCachePath /var/ngx_pagespeed_cache;
    location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
    location ~ "^/ngx_pagespeed_static/" { }
    location ~ "^/ngx_pagespeed_beacon" { }

    location / {
        location ~.*\.(3gp|gif|jpg|jpeg|png|ico|wmv|avi|asf|asx|mpg|mpeg|mp4|pls|mp3|mid|wav|swf|flv|html|htm|txt|js|css|exe|zip|tar|rar|gz|tgz|bz2|uha|7z|doc|docx|xls|xlsx|pdf|iso|woff|ttf|svg|eot|sh)$ {
            root /usr/local/apache/htdocs/;                     
            expires max;
            try_files $uri @backend;

You can also create custom nginx vhost template and rebuild webserver to apply ensure you added on both template for http and https (.tpl and .stpl).

Restart nginx Service :

before restarting check the nginx configs are correct :

nginx -t

if it outputs successful proceed with restart

service nginx restart
systemctl restart nginx

To disable pagespeed module for a domain you need to change on to off and restart nginx service :

# Pagespeed
pagespeed off;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }


Test nginx pagespeed is working or not via this Online checker :

Or via CURL :

curl -I -p https//www.domain.tld/

Find X-Page-Speed header if nginx pagespeed is enabled successfully :

[root@mysterydata]# curl -I -p https//www.domain.tld/
HTTP/1.1 200 OK
Server: nginx/1.16.1
Content-Type: text/html
Connection: keep-alive
Keep-Alive: timeout=60
Vary: Accept-Encoding
ETag: "13cd-5926bf0e6bbf0"
X-Cache: HIT from Backend
Date: Wed, 06 Nov 2019 20:30:21 GMT
Cache-Control: max-age=0, no-cache

If this post helps you in any way please consider a donation

Donate with PayPal :


You may also like


Kena November 6, 2019 - 10:11 pm

Don’t work cache and mod_broteli pleashe help, Web site

Sandy November 7, 2019 - 9:13 am

pagespeed is using gzip, brotli compression will not work. Already replied you in the forum

Andrew November 7, 2019 - 5:00 am

Thank you Works perfectly on stable nginx version, did you’ve any plan for releasing ngx_pagespeed for mainline nginx version?

Sandy November 7, 2019 - 9:11 am

currently i develop the modules upon requests. There are not too many demands for mainline. You can contact me via forum @sandeep

tomas November 7, 2019 - 11:13 pm

Thank you

Glorency November 8, 2019 - 12:38 am

At first i would like to thank for a great tutorial . I am using both gzip & brotli Compression on nginx.conf file . can i use ngx_pagespeed module with gzip & brotli Compression altogether ?

# Compression brotli
brotli on;
brotli_comp_level 6;


# Compression gzip
gzip on;
gzip_vary on;

Sandy November 7, 2019 - 7:52 pm

Hi, pagespeed by default uses gzip compression if you enabled pagespeed for a particular domain then you can’t use brotli compression on it but for other domains which doesn’t have pagespeed enabled then brotli will continue to work.

Glorency March 19, 2020 - 8:15 am

how to use ngx_pagespeed module for nginx/1.17.9 on Ubuntu 16.04 LTS ( using Vesta )
nginx version: nginx/1.17.9
built by gcc 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.12)
built with OpenSSL 1.0.2g 1 Mar 2016

Sandeep B. March 20, 2020 - 6:16 pm

Hi, its now lots of request for mainline nginx will publish it asap.

Tomas April 28, 2020 - 8:02 am

You can activate Brotli and disble Gzip in the domain.conf by:
#Pagespeed config
pagespeed on;
## For Brotli compresion a cero
pagespeed HttpCacheCompressionLevel 0;

Sandeep B. April 28, 2020 - 10:37 am

great find :)

Alper November 11, 2019 - 6:43 am

Thank you for all Sandy, As always with a perfect narrative

Sandy November 11, 2019 - 8:26 am

you’re welcome :)

Alper November 28, 2019 - 5:46 pm

Everything was working well, but today it’s making this mistake all the time. :(

[Thu Nov 28 12:34:42.448546 2019] [pagespeed:error] [pid 7195:tid 139638804924160] [mod_pagespeed @7195] Serf status 120171(APR does not understand this error code) polling for 4 threaded fetches for 0.05 seconds

Sandy December 1, 2019 - 6:03 pm

are you sure its from nginx ? seems its from apache

Alper December 5, 2019 - 1:36 am

Yes, your right problem at apache. How do I fix this problem?

Sandy December 5, 2019 - 9:20 pm

Please visit our forum and open a topic we’ll try to find out something there

roberto January 16, 2020 - 6:25 pm

Thank you for the information.
but I..
I am using nginx proxy + varnish + apache + php-fpm
can I install pagespeed on this system?
thank you

Sandy January 17, 2020 - 9:01 am

yes you can install

Tomas April 28, 2020 - 7:56 am

Thank you for the new update for nginx 1.18. It works fine.

sed May 2, 2020 - 1:18 am

does it work when nginx is set as proxy to apache ?

Sandeep B. May 2, 2020 - 7:12 am


roberto May 27, 2020 - 10:44 am


nginx/1.16.1 I use

but how to update Nginx 1.18.0?

I’m using CWP (pro), thanks.

roberto May 27, 2020 - 11:07 am

I found
nano /etc/yum.repos.d/nginx.repo
enabled=0 >>>>> enabled=1
yum update nginx
Restart services, it’s okay.

roberto May 28, 2020 - 8:54 am

some pictures give 404 error. Is there a solution for this?

Sandeep B. May 28, 2020 - 9:01 am

Check the error lo there will be some more information

Muhammad Khan June 14, 2020 - 12:13 pm


Cheers for your guide but when I tried this guide, everything works fine except when i try to restart nginx i got this error “nginx: [emerg] module “/etc/nginx/modules/” version 1018000 instead of 1019000 in /etc/nginx/nginx.conf:1″
Can you help me in fixing this. I am sharing my server details below…

OS : Ubuntu 18.04
Control Panel – VestaCP
Setup Using – NGINX + PHP-FPM


Sandeep B. June 16, 2020 - 1:09 am

hi nginx version should match

Muhammad Khan June 16, 2020 - 1:12 am

but vestaCP install nginx v.1.19.0 by default so how what to do next. how to match the version of NGINX ?

Andrei June 22, 2020 - 4:49 pm

Please, help me in fixing this, i have nginx v.1.19

Sandeep B. June 22, 2020 - 7:12 pm

it currently I’m not providing mainline nginx modules. You can create a topic in our forum to request.

hakan September 30, 2020 - 3:15 am

nginx version: nginx/1.18.0
php : 7.4.10
nginx proxy + varnish + apache + php-fpm
I haven’t used pagespeed for a long time. it was working but now it is not working.
what could be the problem
thank you

John December 23, 2020 - 3:44 am

which nginx vhost template should you reproduce and edit inorder to create a custom one please. I see so many of them in CWP panel.

Alex Bond February 4, 2021 - 12:31 pm

no work nginx: [emerg] module “/usr/share/nginx/modules/” is not binary compatible

nginx -V
nginx version: nginx/1.18.0
built by gcc 8.3.1 20191121 (Red Hat 8.3.1-5) (GCC)
built with OpenSSL 1.1.1g FIPS 21 Apr 2020
TLS SNI support enabled

Sandeep B. February 8, 2021 - 5:41 pm

probably you did custom modification to nginx installation ?

Surjo February 17, 2021 - 8:32 am

Please update for nginx 1.9.6 Ubuntu HestiaCP

Andrew February 24, 2021 - 3:46 pm

My system:
Nginx + Varnish + Apache PHP-FPM
nginx version: nginx/1.18.0
made all from this guide…
nginx -t:
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
I see, that in “var/ngx_pagespeed_cache” some files appeared, but after curl -I -p I can’t find the X-Page-Speed header :( and also any changes on site

Should nginx -V show something like –with-pagespeed_module or smth like that?

Sandeep B. February 24, 2021 - 5:18 pm

hello, you need to follow this tutorial from very first if you skip steps then pagespeed will not work.

Andrew February 24, 2021 - 5:47 pm

I go thru all steps from this tutorial and here my curl -I -p:
HTTP/1.1 200 OK
Server: nginx/1.18.0
Date: Wed, 24 Feb 2021 17:41:43 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Keep-Alive: timeout=60
Content-Encoding: gzip
Expires: Wed, 17 Aug 2005 00:00:00 GMT
Cache-Control: no-store, no-cache, must-revalidate, post-check=0, pre-check=0, no-cache, no-transform, public
Pragma: no-cache, no-cache
Set-Cookie: 70f44a7dee1eac7d01ca09c7bc2f2fd2=db64edhgqstqhacmeoa6hovv93; path=/; secure; HttpOnly
Last-Modified: Wed, 24 Feb 2021 17:40:20 GMT
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
X-XSS-Protection: 1; mode=block
X-Varnish: 196972
Age: 0
Via: 1.1 varnish (Varnish/5.2)
ETag: W/2959bf7b46ac6ab65957ea9cb72b79ae
X-Cache: HIT from Backend
Strict-Transport-Security: max-age=31536000
X-XSS-Protection: 1; mode=block
X-Content-Type-Options: nosniff

Should I configure nginx with this module?

Sandeep B. February 25, 2021 - 2:33 pm

hi its better to open a topic we’ll happy to check and help you there.

Igor March 24, 2021 - 2:52 pm

Hi, i have a server running apache and nginx as reverse proxy. Should I install mod_pagespeed in apache, in nginx or in both?


Sandeep B. March 24, 2021 - 3:55 pm

hi only nginx if you want to install in both you can install it.

Anand May 2, 2021 - 4:52 pm

Will be helpful if you can guide on setting pagespeed admin access


Leave a Comment