How to Install ngx_pagespeed with Nginx, CWP, 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 :

Updated on :ย  22nd April, 2020
Google Pagespeed version :
For Stable Nginx 1.18.0 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 :


Donate with Paytm :


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.


Leave a Comment