How to Enable SPDY on Nginx Using Ubuntu 14.04

Enabling SPDY is a relatively simple way to improve the performance of Nginx and reduce the time it takes for clients to load pages from a web server.

SPDY is a web transport protocol developed by Google that reduces page load times using stream multiplexing, prioritized requests, and HTTP header compression.

SPDY is already well supported by most modern web browsers including Chrome, Firefox, and even Internet Explorer so nearly every client can take advantage of the increase in speed.

In this post you’ll find the steps I used to configure Nginx to use SPDY on my Ubuntu 14.04 server running in the Linode Cloud.

Step 1 – Enable SSL support

In order to fully take advantage of the benefits provided by SPDY you will need to have SSL enabled.  Linode has a great guide that explains how to enable SSL on Nginx.

I purchased a domain validated SSL certificate from NameCheap for $9/year which is extremely cheap for a commercial SSL certificate.

The main benefit to buying a signed SSL certified from a trusted vendor is the green padlock users will see in their address bar when visiting your site.

SSL Padlock

 

On my web server I leave port 80 enabled and redirect incoming traffic on that port over to 443 (HTTPS).  I do this using the following block of code in my /etc/nginx/sites-available/samkear.com file.

server {
listen 96.126.118.167:80;
server_name samkear.com;

location / {
rewrite ^ https://$server_name$request_uri permanent;
}

 

Step 2 – Upgrade Nginx to the latest version

The version of Nginx in the Ubuntu repositories is fairly old (1.4.6 at the time of this post).  For the best SPDY support I highly recommend upgrading the latest version which is currently 1.9.1-1.

Fortunately the Nginx team maintains their own Ubuntu repository which contains the latest versions.  Below are the steps to utilize their repositories.

Before doing so I strongly recommend making a full backup of your server in case things go wrong.

You should also make a quick copy of the contents of /etc/nginx/* so you can reference your existing config files later if needed.

Add the Nginx team’s PGP signing keys.

# curl http://nginx.org/keys/nginx_signing.key | apt-key add -

Add the Nginx.org repositories.

# echo -e "deb http://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx\ndeb-src http://nginx.org/packages/mainline/ubuntu/ `lsb_release -cs` nginx" > /etc/apt/sources.list.d/nginx.list

Update the apt sources.

# apt-get update

At this point I found that apt-get upgrade (or dist-upgrade) wouldn’t allow me to upgrade the package. I also found that apt-get install nginx would give me the following error message.

trying to overwrite '/etc/default/nginx', which is also in package nginx-common 1.4.6-1ubuntu3.2
E: Sub-process /usr/bin/dpkg returned an error code (1)

The issue seems to be that the package in the Ubuntu repositories is called nginx-common and the Nginx.org package is just called Nginx.

The easiest solution I found was to remove the current nginx-common package and reinstall the new one.

In my case this wasn’t nearly as troublesome as it might sound, I only had to modify one config line to get everything up and running again but your experience may vary.

Again make sure you backup first.  I used the manual snapshot feature offered through Linode to quickly grab the state of my server before proceeding.

# apt-get remove nginx-common
# apt-get install nginx

The installer will ask you if you want to replace ‘/etc/nginx.conf’, I went ahead and let the installer replace it and I only had to add one line but you may want to just preserve your existing file.

include /etc/nginx/sites-enabled/*;

(This is where having a backup of the original config file comes in handy)

Step 3 – Verify that Nginx is compiled with SPDY support

At this point you should do a quick check to confirm that the version of Nginx installed has support for the SPDY module.

nginx -V

If support for SPDY is present you will see –with-http_spdy_module in the output.

Step 4 – Edit the config file for your site to enable SPDY

To turn on the SPDY module you will need to modify the listen line in the server block of the configuration file for your site.  In my case this was /etc/nginx/sites-available/samkear.com

My listen line looked like this before the change:

listen 443 ssl;

Simply add spdy to this line to enable the module

listen 443 ssl spdy;

Step 5 – Reload Nginx to apply the changes

In order for Nginx to pick up the config file changes you need to either reload, or restart the service.
#service nginx reload

Step 6 – Verify that SPDY is enabled

The easiest way to confirm if you have correctly configured SPDY is to visit spdycheck.org.  This site will connect to your site and analyze the connection to determine if SPDY is enabled and properly configured.

It will also offer some tips to tweak your settings to take full advantage of the protocol.

SPDYCheck

Alternate verification method

I also discovered a plugin for Chrome called HTTP/2 and SPDY indicator.

HTTP2 and SPDY indicator

This plugin adds an indicator to your address bar in the form of a lightning bolt that turns various different colors to indicate the level of SPDY or HTTP/2 support on a web site.

SPDY enabled

The future of SPDY

SPDY has laid down the framework for what will eventually become HTTP2.

Nginx has announced that they plan to implement support for HTTP2 by the end of 2015 so be sure to keep your copy of Nginx up to date in order to take advantage of support for these new features as they come out.

Sam Kear

Sam graduated from the University of Missouri - Kansas City with a bachelors degree in Information Technology. Currently he works as a network analyst for an algorithmic trading firm. Sam enjoys the challenge of troubleshooting complex problems and is constantly experimenting with new technologies.

Leave a Reply

Your email address will not be published. Required fields are marked *