ngx_pagespeed is an Nginx module that allows you to automatically optimize the resources of your web pages, it can setup a bunch of really cool cache rules to reduce the bandwidth used across the network, minify & combine elements to reduce the size of the files, optimize images, photos and much more to make your websites blazing fast.
How can I add mod_pagespeed into Nginx on CentOS 7?
If you compiled and installed Nginx from source, then it’s your lucky day because you will have to configure and build Nginx again, it will be really easy. And if you installed Nginx from a binary package like RPM, then you will have to remove it and install from source, but don’t worry, we will guide you during the entire process, it’s easy if you do it carefully.
Requirements
Let’s install some package requirements before we begin.
yum install gcc-c++ pcre-devel zlib-devel make unzip
Get your current Nginx configuration by typing:
nginx -V
This will provide you the current configuration of your Nginx server. The most important options are after “configure arguments:”.
Example:
[root@scalescale.com:~]nginx -V nginx version: nginx/1.8.0 built by gcc 4.4.7 20120313 (Red Hat 4.4.7-16) (GCC) built with OpenSSL 1.0.1e-fips 11 Feb 2013 TLS SNI support enabled configure arguments: --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --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-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic'
Create a backup of your init.d script
cp /etc/init.d/nginx /etc/init.d/nginx.bak
Create a backup of your Nginx configuration files
rsync -avpr /etc/nginx /etc/nginx.bak --exclude="logs"
Download ngx_pagespeed module
cd NPS_VERSION=1.9.32.10 wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zip unzip release-${NPS_VERSION}-beta.zip cd ngx_pagespeed-release-${NPS_VERSION}-beta/ wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz tar -xzvf ${NPS_VERSION}.tar.gz # extracts to psol/
Download and build Nginx with support for PageSpeed
cd # check http://nginx.org/en/download.html for the latest version NGINX_VERSION=1.8.0 wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gz tar -xvzf nginx-${NGINX_VERSION}.tar.gz cd nginx-${NGINX_VERSION}/ ./configure your_previous_nginx_config_goes_here--add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta
If you are running a 32-bit userland with a 64-bit kernel, you will have build a 32 bit version of pagespeed instead:
setarch i686 ./configure --add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta
A full example of the ./configure command adding the ngx_pagespeed module would be:
./configure --prefix=/etc/nginx --sbin-path=/usr/sbin/nginx --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-http_ssl_module --with-http_realip_module --with-http_addition_module --with-http_sub_module --with-http_dav_module --with-http_flv_module --with-http_mp4_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_random_index_module --with-http_secure_link_module --with-http_stub_status_module --with-http_auth_request_module --with-mail --with-mail_ssl_module --with-file-aio --with-ipv6 --with-http_spdy_module --with-cc-opt='-O2 -g -pipe -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic' --add-module=$HOME/ngx_pagespeed-release-${NPS_VERSION}-beta
Remove Nginx binary package
This will cause you 1 minute of downtime, or less, depending on how powerful your CPU is. We will now remove Nginx RPM package because it may create conflicts with the source package that we will install later.
yum remove nginx
Install Nginx from source
Just run make and make install from the previous directory where you were
make make install
Restore init.d files and Nginx configuration files
mv /etc/init.d/nginx.bak /etc/init.d/nginx cp /etc/nginx.bak/nginx/nginx.conf /etc/nginx/ -fv cp /etc/nginx.bak/nginx/conf.d/* /etc/nginx/conf.d/* -fv chkconfig nginx on service nginx restart
And you are done, now you have a full Nginx package compiled from scratch with ngx_pagespeed support.
How can I use ngx_pagespeed?
First, let’s create the ngx_pagespeed_cache directory, and set the right owner to avoid writing issues when you are using the ngx_pagespeed module:
mkdir /var/ngx_pagespeed_cache chown nginx.nginx /var/ngx_pagespeed_cache
In order t activate pagespeed, you will have to add a few lines to your nginx.conf file or other include files that you may use. Just add this into every server block where you want PageSpeed enabled:
pagespeed on; pagespeed FileCachePath /var/ngx_pagespeed_cache; # 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 ~ "^/pagespeed_static/" { } location ~ "^/ngx_pagespeed_beacon$" { }
After done, reload Nginx to apply changes:
service nginx reload
Now PageSpeed is active, but the hard part starts now, and that is the configuration of the rules. You will have to read the PageSpeed Configuration Documents and start learning which rule is right for your websites and applications: read more
How can I disable PageSpeed Module?
If you ever need to disable ngx_pagespeed then you can disable it easily, edit your nginx.conf file and set:
pagespeed off;
Reload Nginx to apply the changes:
service nginx reload
Conclusion
At this time you should have successfully installed Nginx from source with ngx_pagespeed module fully working. Now it’s up to you to configure it and tune it to get your site speed and performance into the next direction. The only disadvantage from this tutorial is the fact that you will have to update your Nginx installation manually (configuring, making and installing) each time there is a new Nginx version.
RSS Feed Powered by MaxBlogPress Bring My Blog Visitors Back