NGINX & ngx_pagespeed on CentOS 7
I wanted to have a quick play with Googles Pagespeed module, as I use NGINX and not Apache I needed to compile NGINX from source to enable the ngx_pagespeed module. The following assumes you are have a clean CentOS 7 installation.
First off you need to install the packages which enable you to compile NGINX with ngx_pagespeed from source.
yum install -y gcc-c++ pcre-dev pcre-devel zlib-devel make unzip openssl-devel
Now create a NGINX user and make so the user doesn’t have a shell.
useradd nginxusermod -s /sbin/nologin nginx
Once the required packages have been installed and have created the NGINX user it’s time to grab a copy of the ngx_pagespeed and psol source code. See https://github.com/pagespeed/ngx_pagespeed/releases for latest release details.
cd /usr/local/src/NPS_VERSION=1.9.32.3wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.zipunzip release-${NPS_VERSION}-beta.zipcd ngx_pagespeed-release-${NPS_VERSION}-beta/wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gztar -xzvf ${NPS_VERSION}.tar.gz
Now grab the latest version of NGINX. See http://nginx.org/en/download.html for details on the latest release.
cd /usr/local/src/NGINX_VERSION=1.7.10wget http://nginx.org/download/nginx-${NGINX_VERSION}.tar.gztar -xvzf nginx-${NGINX_VERSION}.tar.gzcd nginx-${NGINX_VERSION}/
Now you have the source its time to configure NGIX and compile
./configure — add-module=/usr/local/src/ngx_pagespeed-release-${NPS_VERSION}-beta — with-http_ssl_module — 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.logmake && make install
Now you have NGINX compiled and installed on your server. While you could manage the process manually it’s best to create an init script. As CentOS 7 uses systemd you need to create a service file in /usr/lib/systemd/system/.
cat >> /usr/lib/systemd/system/nginx.service << NGINX_SERVICE[Unit]Description=The nginx HTTP and reverse proxy serverAfter=syslog.target network.target remote-fs.target nss-lookup.target
[Service]Type=forkingPIDFile=/run/nginx.pidExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.confExecReload=/bin/kill -s HUP $MAINPIDExecStop=/bin/kill -s QUIT $MAINPIDPrivateTmp=true
[Install]WantedBy=multi-user.targetNGINX_SERVICEFinally you need to enable and start the service.
systemctl enable nginxsystemctl start nginx
If everything has gone as expected you should have NGINX running your server with the ngx_pagespeed module also loaded. Now you will to do playing with ngx_pagespeed, first of all create a cache directory and make sure that NGINX can read and write to it;
mkdir /var/ngx_pagespeed_cachechown -R nginx: /var/ngx_pagespeed_cache
then add some directives to your NGINX virtual host block. I used to the following for my tests;
pagespeed on;pagespeed FileCachePath /var/ngx_pagespeed_cache;
location ~ “^/pagespeed_static/” { }location ~ “^/ngx_pagespeed_beacon$” { }location /ngx_pagespeed_statistics { allow 127.0.0.1; deny all; }location /ngx_pagespeed_global_statistics { allow 127.0.0.1; deny all; }location /ngx_pagespeed_message { allow 127.0.0.1; deny all; }location /pagespeed_console { allow 127.0.0.1; deny all; }location /pagespeed_admin { allow 127.0.0.1; deny all; }
# 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 “” “”;}
pagespeed EnableFilters canonicalize_javascript_libraries,extend_cache,extend_cache_pdfs,combine_css,combine_javascript,move_css_above_scripts,insert_dns_prefetch,rewrite_javascript,rewrite_images,prioritize_critical_css,rewrite_css,rewrite_style_attributes,convert_meta_tags,lazyload_images,collapse_whitespace,move_css_to_head,remove_comments,remove_quotes,inline_css,inline_javascript;pagespeed UseExperimentalJsMinifier on;
Related Posts

Rocky Linux and Packer
Explore Packer's use with Rocky Linux. Migrate CentOS projects efficiently. Use provided templates for Virtualbox and VMWare.

Packer CentOS 8
Easily transition to CentOS 8 with Packer and Vagrant, featuring Cockpit for efficient server management.

Ansible AWX
Author Russ Mckendrick explores Ansible AWX, the open-source version of Ansible Tower. Follow his guide to quickly set up AWX with CentOS 7 using Vagrant.