How To Increase PageSpeed Score on Ubuntu 16.04

Even though a web page can seem fast, the default Nginx configuration will cause Google's PageSpeed Insights tool to flag inefficiencies in your site and grade it poorly. In this tutorial, you'll make quick edits to the configuration file for your domain that instantly boost your site's response speed and its PageSpeed metric to above 80/100.

Please ensure that you have an Ubuntu 16.04 server set up, including a sudo non-root user and a firewall, including a Nginx installed on your server.

Open your terminal with:

Ctrl+Alt+T

Run the following commands:

Get PageSpeed Score

Paste the site URL into Google’s PageSpeed Insights service and clicking Run Insights.

Enable Compression

Open the Nginx configuration file for your site in nano:

sudo nano /etc/nginx/sites-available/default

Locate the server configuration block and add a series of snippets to configure compression.

First, enable Gzip compression and set the compression level:

server {
listen 80 default_server;
listen [::]:80 default_server;

gzip on;
gzip_comp_level    X;

Replace X with a number between 1 and 9.

Below this, set minimum compression levels. The default of 20 bytes but should be replaced with 256:

...
gzip_comp_level    5;
gzip_min_length    256;

Then, compress data even for clients connected via proxies:

...
gzip_min_length    256;
gzip_proxied       any;

beneath that, set these proxies to cache both the compressed and regular version of a resource whenever the client's Accept-Encoding capabilities header varies

...
gzip_proxied       any;
gzip_vary          on;

Lastly, specify the MIME-types for the output you want to compress:

...
gzip_vary          on;

gzip_types
application/atom+xml
application/javascript
application/json
application/ld+json
application/manifest+json
application/rss+xml
application/vnd.geo+json
application/vnd.ms-fontobject
application/x-font-ttf
application/x-web-app-manifest+json
application/xhtml+xml
application/xml
font/opentype
image/bmp
image/svg+xml
image/x-icon
text/cache-manifest
text/css
text/plain
text/vcard
text/vnd.rim.location.xloc
text/vtt
text/x-component
text/x-cross-domain-policy;
# text/html is always compressed by gzip module
gzip_proxied       any;
gzip_vary          on;

Save and close the file.

Test the Nginx configuration:
sudo nginx -t

If you've made the changes exactly as stated in this tutorial, you'll see no error messages.

Configure Browser Caching

sudo nano /etc/nginx/sites-available/default

Insert the following snippet inside the server block directly after the previous code for Gzip compression:

 ... # text/html is always compressed by gzip module location ~*  \.(jpg|jpeg|png|gif|ico|css|js|pdf)$ {
expires 7d;}

Save and close the file to exit.

Ensure the configuration has no errors:

sudo nginx -t

Then restart Nginx to apply these new directives for incoming requests:

sudo systemctl restart nginx

Compare Outcome

Run your site through the Google’s PageSpeed Insights service once more by pasting the URL and clicking Run Insights.

Our goal was to have a score that is above 80. If your site is still below this threshold, there are other things you need to pay attention to. PageSpeed Insights will detail exactly what these are and show you how to fix them if you click on the Show how to fix the link for each issue.