How to Install ngx_pagespeed with Nginx, CWP, HestiaCP, VestaCP and on Linux OS
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 : 3rd December, 2022
Google Pagespeed version : 1.13.35.2-0
For Stable Nginx 1.22.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 https://www.uxlinux.com/upload/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.uxlinux.com/upload/ngx_pagespeed.zip unzip ngx_pagespeed.zip rm -rf ngx_pagespeed.zip
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/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
Hello,
I’ve done everything here correctly, but I’m getting this error.
nginx: [emerg] module “/etc/nginx/modules/ngx_pagespeed.so” version 1022000 instead of 1020002 in /etc/nginx/nginx.conf:1
why are website content after
/ var / ngx_pagespeed_cache / v3 copied and not to / home / user / website?
and how can I uninstall pagespeed?
I get following errors
2021/08/01 08:52:44 [error] 1935749#1937065: [ngx_pagespeed 1.13.35.2-0] http://propertymysore.com/themefiles/FindHouse/assets/js/rest.js (connecting to:localhost):0:serf_context_run error status=111 (Connection refused)
hi you need to map the origin domain
https://www.modpagespeed.com/doc/domains#mapping_origin
eg :
# Pagespeed
pagespeed off;
pagespeed MapOriginDomain http://localhost https://www.domain2.com;
pagespeed FileCachePath /var/ngx_pagespeed_cache;
location ~ "\.pagespeed\.([a-z]\.)?[a-z]{2}\.[^.]{10}\.[^.]+" { add_header "" ""; }
location ~ "^/ngx_pagespeed_static/" { }
location ~ "^/ngx_pagespeed_beacon" { }
Will be helpful if you can guide on setting pagespeed admin access
hi only nginx if you want to install in both you can install it.
Hi, i have a server running apache and nginx as reverse proxy. Should I install mod_pagespeed in apache, in nginx or in both?
Thanks
hello, you need to follow this tutorial from very first if you skip steps then pagespeed will not work.
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 ngx_pagespeed.so module?
Thanks!
hi its better to open a topic we’ll happy to check and help you there.
https://forum.mysterydata.com/
hello
My system:
CWP
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 https://mysite.com/ 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?
Thanks!
Please update for nginx 1.9.6 Ubuntu HestiaCP
no work nginx: [emerg] module “/usr/share/nginx/modules/ngx_pagespeed.so” 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
probably you did custom modification to nginx installation ?
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.
hello
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
Hello,
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/ngx_pagespeed.so” 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
Thanks
hi nginx version should match
but vestaCP install nginx v.1.19.0 by default so how what to do next. how to match the version of NGINX ?
Please, help me in fixing this, i have nginx v.1.19
it currently I’m not providing mainline nginx modules. You can create a topic in our forum to request.
some pictures give 404 error. Is there a solution for this?
Check the error lo there will be some more information
hello
nginx/1.16.1 I use
but how to update Nginx 1.18.0?
I’m using CWP (pro), thanks.
I found
nano /etc/yum.repos.d/nginx.repo
enabled=0 >>>>> enabled=1
yum update nginx
Restart services, it’s okay.
does it work when nginx is set as proxy to apache ?
Yes
Thank you for the new update for nginx 1.18. It works fine.
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
yes you can install
Thank you for all Sandy, As always with a perfect narrative
you’re welcome ๐
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
are you sure its from nginx ? seems its from apache
Yes, your right problem at apache. How do I fix this problem?
Please visit our forum and open a topic we’ll try to find out something there
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;
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.
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
Hi, its now lots of request for mainline nginx will publish it asap.
You can activate Brotli and disble Gzip in the domain.conf by:
#Pagespeed config
pagespeed on;
## For Brotli compresion a cero
pagespeed HttpCacheCompressionLevel 0;
great find ๐
Thank you
Thank you Works perfectly on stable nginx version, did you’ve any plan for releasing ngx_pagespeed for mainline nginx version?
currently i develop the modules upon requests. There are not too many demands for mainline. You can contact me via forum @sandeep
Don’t work cache and mod_broteli pleashe help, Web site traderbases.com
pagespeed is using gzip, brotli compression will not work. Already replied you in the forum
https://forum.mysterydata.com/post/28