How to speed up Nginx web server with PageSpeed

MooseLucifer

Well-known member
Registered
Joined
May 20, 2016
Messages
149
Points
28
Nginx itself is a web server with very good performance. However there are ways to optimize more and one of them is using the module developed by Google called PageSpeed (ngx_pagespeed)

ngx_pagespeed.png

ngx_pagespeed help your website improve the speed and reduced load time by automatically applying optimized engineering components and static page such as CSS, JavaScript, Image.

Some filters of ngx_pagespeed:

Collapse Whitespace: reduce bandwidth usage by replacing multiple blanks in HTML by 1 space only.
Canonicalize JavaScript Libraries: reduce bandwidth usage by automatically using the popular Javascript library on free servers (eg, Google).
Combine CSS: reducing the number of HTTP requests by combining multiple CSS files into one file.
Combine JavaScript: reducing the number of HTTP requests using a combination of JavaScript files into one file.
Extend Cache: reduce bandwidth usage by optimizing the function of the browser cache.
Flatten CSS Imports: reducing the number of HTTP requests by deleting @import CSS file.
Lazyload Images: slowing down the loading of images are not displayed on the user's browser.
JavaScript minify: reduce bandwidth usage by optimizing the JavaScript file size.
Optimize Images: optimize images using inline images, image compression, or convert GIF to PNG.
Pre-Resolve DNS: DNS resolution time decreased by DNS pre-resolution using HTML.
And there are many filters and other examples of ngx_pagespeed on official page.

We can not install ngx_pagespeed as an individual module that needs to install by build Nginx from source code.

Build and install Nginx with ngx_pagespeed

- Prepare the needed ingredients to build nginx and ngx_pagespeed

On Debian, Ubuntu or Linux Mint:

Code:
apt-get install build-essential zlib1g-dev libpcre3-dev
On Fedora, CentOS or RHEL:

Code:
yum install gcc-c++ pcre-devel zlib-devel make wget
- Download source ngx_pagespeed (latest 1.8.31.4-beta) and unzip into the directory /usr/local/nginx/modules/

Code:
cd
NPS_VERSION=1.8.31.4
mkdir -p /usr/local/nginx/modules
wget https://github.com/pagespeed/ngx_pagespeed/archive/release-${NPS_VERSION}-beta.tar.gz
tar xvfvz release-${NPS_VERSION}-beta.tar.gz -C /usr/local/nginx/modules --no-same-owner

- Download PSOL (PageSpeed Optimization Libraries) and unzip into the directory ngx_pagespeed

Code:
wget https://dl.google.com/dl/page-speed/psol/${NPS_VERSION}.tar.gz
tar xvfvz ${NPS_VERSION}.tar.gz -C /usr/local/nginx/modules/ngx_pagespeed-release-${NPS_VERSION}-beta --no-same-owner
find /usr/local/nginx/modules/ngx_pagespeed-release-${NPS_VERSION}-beta/ -type d -exec chmod +rx {} \;
find /usr/local/nginx/modules/ngx_pagespeed-release-${NPS_VERSION}-beta/ -type f -exec chmod +r {} \;
- Download the latest nginx and unzip into the directory /usr/local

Code:
wget http://nginx.org/download/nginx-1.6.0.tar.gz
tar xvfvz nginx-1.6.0.tar.gz -C /usr/local --no-same-owner
- Finally, compile Nginx with module ngx_pagespeed and starting the installation

Code:
cd /usr/local/nginx-1.6.0
./configure --add-module=/usr/local/nginx/modules/ngx_pagespeed-release-${NPS_VERSION}-beta --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --with-http_ssl_module --conf-path=/etc/nginx/nginx.conf --with-http_gzip_static_module --with-http_realip_module --group=nginx --user=nginx --pid-path=/var/run/nginx.pid --with-http_stub_status_module

make
make install
Note: depending on your needs that adding the module to compile Nginx with ngx_pagespeed.

- Restart Nginx

Code:
service nginx restart
- Check that the module has been integrated into ngx_pagespeed Nginx with the command

Code:
nginx -V
If you see returned results similar to this then it was successful.

Code:
nginx version: nginx/1.6.0
built by gcc 4.4.7 20120313 (Red Hat 4.4.7-4) (GCC)
TLS SNI support enabled
configure arguments: --add-module=/usr/local/nginx/modules/ngx_pagespeed-release-1.8.31.4-beta --prefix=/usr/share/nginx --sbin-path=/usr/sbin/nginx --with-http_ssl_module --conf-path=/etc/nginx/nginx.conf --with-http_gzip_static_module --with-http_realip_module --group=nginx --user=nginx --pid-path=/var/run/nginx.pid --with-http_stub_status_module

Configuration module ngx_pagespeed

- Prior to the configuration, you need to create the cache directory for the module

Code:
mkdir /var/ngx_pagespeed_cache
chown nginx:nginx /var/ngx_pagespeed_cache
- To enable and configure ngx_pagespeed, you need to edit the configuration file of Nginx (/etc/nginx/nginx.conf).

ngx_pagespeed have more different filters, depending on the different uses that you have appropriate choices. There are two different filters that you can use is CoreFilters (by default) and passthrough.

CoreFilters

CoreFilters is a set of Google-certified filter is safe for most websites. Therefore, this way you fit the new newbie try. If needed, you can disable a filter from any other CoreFilters or add a filter to.

This is an example configuration with CoreFilters ngx_pagespeed:

Code:
server {
        listen   80 forumweb.hosting;

        access_log off;
        error_log off;
        # error_log /home/forumweb.hosting/logs/error.log;
        root /home/forumweb.hosting/public_html;
        index index.php index.html index.htm;
        server_name forumweb.hosting;
		
        # enable ngx_pagespeed
        pagespeed on;

        pagespeed FileCachePath /var/ngx_pagespeed_cache;

        # let's speed up PageSpeed by storing it in the super duper fast memcached
        # pagespeed MemcachedThreads 1;
        # pagespeed MemcachedServers "localhost:11211";

        # enable CoreFilters
        [COLOR="#FF8C00"]pagespeed RewriteLevel CoreFilters[/COLOR];

        # disable particular filter(s) in CoreFilters
        pagespeed DisableFilters rewrite_images;

        # enable additional filter(s) selectively
        pagespeed EnableFilters collapse_whitespace;
        pagespeed EnableFilters lazyload_images;
        pagespeed EnableFilters insert_dns_prefetch;
}
See full list of filters available in CoreFilters here.

Passthrough

With the knowledge you already have, you should use the experience passthrough. When it will have to activate the filter to be used.

Configuration example with passthrough:

Code:
server {
        listen   80 forumweb.hosting;

        access_log off;
        error_log off;
        # error_log /home/forumweb.hosting/logs/error.log;
        root /home/forumweb.hosting/public_html;
        index index.php index.html index.htm;
        server_name forumweb.hosting;
		
        # enable ngx_pagespeed
        pagespeed on;

        pagespeed FileCachePath /var/ngx_pagespeed_cache;

        # let's speed up PageSpeed by storing it in the super duper fast memcached
        # pagespeed MemcachedThreads 1;
        # pagespeed MemcachedServers "localhost:11211";

        # disable CoreFilters
        pagespeed RewriteLevel [COLOR="#FF8C00"]PassThrough[/COLOR];

        # enable collapse whitespace filter
        pagespeed EnableFilters collapse_whitespace;

        # enable JavaScript library offload
        pagespeed EnableFilters canonicalize_javascript_libraries;

        # combine multiple CSS files into one
        pagespeed EnableFilters combine_css;

        # combine multiple JavaScript files into one
        pagespeed EnableFilters combine_javascript;

        # remove tags with default attributes
        pagespeed EnableFilters elide_attributes;

        # improve resource cacheability
        pagespeed EnableFilters extend_cache;

        # flatten CSS files by replacing @import with the imported file
        pagespeed EnableFilters flatten_css_imports;
        pagespeed CssFlattenMaxBytes 5120;

        # defer the loading of images which are not visible to the client
        pagespeed EnableFilters lazyload_images;

        # enable JavaScript minification
        pagespeed EnableFilters rewrite_javascript;

        # enable image optimization
        pagespeed EnableFilters rewrite_images;

        # pre-solve DNS lookup
        pagespeed EnableFilters insert_dns_prefetch;

        # rewrite CSS to load page-rendering CSS rules first.
        pagespeed EnableFilters prioritize_critical_css;
}
Restart web server for the changes to take effect

Code:
[COLOR="#FF8C00"][/COLOR]]nginx service restart
ngx_pagespeed with memcache, Opcache Zend is one of its modules to encourage people to use and I would recommend to install them on your host.

Good luck!
 

VirtuBox

Well-known member
Registered
Joined
May 3, 2016
Messages
1,622
Points
83
This tutorial is the best way to have good performance with a small VPS. Thanks !

But as I have already look for a good tutorial to speed up nginx, I will share with you some useful links :

1) A bash script to compile automatically nginx with pagespeed + SSL + Brotli (Better than Gzip) -> https://github.com/Angristan/nginx-autoinstall
It's the work of a french student but you will not have to speak french to install nginx :ertery:

2) If you don't want to use beta features in production, you should install only the package nginx-extras (nginx + pagespeed) in stable version using the dotdeb.org repository.

Now you can stop using Apache :rolleyes2:
 

macklong

Active member
Registered
Joined
Jun 17, 2016
Messages
67
Points
0
yaa.. it's good... But who use nginx if you have CloudLinux and Amazon Linux! then why you are fighting with the speed man.

Ok if you can't afford the Amazon Linux is Understandable!

But guys CloudLinux is so better than Nginx. If you ask me I'm happy with the APACHE2 performance - if the load bothering me I will use CDN services like CloudFlare and all that..

Think about it. Thanks
 

VirtuBox

Well-known member
Registered
Joined
May 3, 2016
Messages
1,622
Points
83
Hi macklong.
Yes I have already try Cloudlinux, but I have still some issues with paying for linux ... :rolleyes2:
It's not about the fact nginx is better than apache because with the mod mpm events, you should not have any load on your cpu.
But for big traffic, using nginx is a step before running cloudlinux. And I haven't try Amazon linux, but it seems to be a fork of redhat/centos. I'm not telling there are no good features (but i was wondering when I will use the cuda gpu driver include with), but AWS is still very expensive to run a webserver. I will not pay the price of my dedicated for 8GB RAM and 4 vCore, even if they have some good distro.
 

Marc A

Well-known member
Registered
Joined
Jun 14, 2016
Messages
125
Points
18
Wow thanks for this! It helped me a lot with vps! :)

Thanks a lot man :D!
 
Older Threads
Replies
5
Views
4,075
fwh
Replies
0
Views
2,387
Replies
2
Views
3,106
Replies
14
Views
6,585

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top