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 :  7th Nov, 2019
Google Pagespeed version : 1.13.35.2-0
For Stable Nginx 1.16.1 (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 https://www.mysterydata.com/wp-content/uploads/2019/11/ngx_pagespeed.zip
unzip ngx_pagespeed.zip
rm -rf ngx_pagespeed.zip

or

cd /etc/nginx/modules
rm -rf ngx_http_brotli*
wget --no-cache https://www.mysterydata.com/wp-content/uploads/2019/11/ngx_pagespeed.zip
unzip ngx_pagespeed.zip
rm -rf ngx_pagespeed.zip

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/ngx_pagespeed.so";

Example nginx.conf :

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

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 {
	listen 123.123.123.123:80;	
	server_name mysterydata.com;
	access_log /usr/local/mysterydata.com.bytes bytes;
	access_log /usr/local/mysterydata.com.log combined;
	error_log /usr/local/mysterydata.com.error.log 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
or
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" { }

TESTS

Test nginx pagespeed is working or not via this Online checker : https://ismodpagespeedworking.com/

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
X-Page-Speed: 1.13.35.2-0
Cache-Control: max-age=0, no-cache

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

Donate with PayPal :

md-donate

Donate with Paytm :

md-donate

You may also like

17 comments

Kena November 6, 2019 - 10:11 pm

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

Reply
Sandy November 7, 2019 - 9:13 am

pagespeed is using gzip, brotli compression will not work. Already replied you in the forum
https://forum.mysterydata.com/post/28

Reply
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?

Reply
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

Reply
tomas November 7, 2019 - 11:13 pm

Thank you

Reply
Glorency November 8, 2019 - 12:38 am

Hi
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;

and

# Compression gzip
gzip on;
gzip_vary on;

Reply
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.

Reply
Glorency March 19, 2020 - 8:15 am

Hi
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

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

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

Reply
Alper November 11, 2019 - 6:43 am

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

Reply
Sandy November 11, 2019 - 8:26 am

you’re welcome 🙂

Reply
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 1.13.35.2-0 @7195] Serf status 120171(APR does not understand this error code) polling for 4 threaded fetches for 0.05 seconds

Reply
Sandy December 1, 2019 - 6:03 pm

are you sure its from nginx ? seems its from apache

Reply
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

Reply
Sandy January 17, 2020 - 9:01 am

yes you can install

Reply

Leave a Comment