Speed Up Your Site with SPDY and Nginx

Speed is a Good Thing ™ when it comes to your site loading. Generally speaking, if people can’t start interacting with your site quickly, meaning within a few seconds of when they first get there, your engagement numbers are going to plummet. The folks over at Google are keenly aware of this and have therefore introduced a new protocol called SPDY which is designed to make the initial load time of modern web pages faster. It’s already in use at places such as GMail and Twitter, so this is not just a theoretical endeavor. My friends over at Automattic sponsored an initial implementation of SPDY for the Nginx web server, and I’ll explain how to use this cool new tech on your own sites.

Both your browser and the web server it’s communicating with have to be able to speak in SPDY for you to gain any benefit. As I’m writing this, modern versions of Chrome, Firefox and Opera know what to do on the browser side. On the server side, there are ways to get SPDY happening if you’re using Apache, Node.js or Nginx. We’ll be talking about Nginx here.

I’m making a few assumptions. Specifically:

  • You are currently using Nginx to serve your site.
  • You have SSL enabled (this is a requirement for SPDY)

Assuming both of those are true, getting your site SPDYized is actually pretty easy.

The first step is to install a build of Nginx that has SPDY support enabled. This is, at the time of this writing, done by applying a patch to the source tree and recompiling. Be sure to check out the attached README.txt file to get some instructions about how to do it, and pay attention to the bit where you have to use a very recent version of openssl. If you’re using Ubuntu Linux (and I recommend that you do) and are on release 10.04 (Lucid) or newer, I’ve made easily installable packages that you can use.

Please note that my packages are statically linked to openssl version 1.0.1c (at the time of this writing). They will not link against your system’s installed openssl libraries. This allows SPDY support on older distros that didn’t ship with a new enough openssl.

After adding my PPA to your list of repositories, you’ll likely want to run the following:

1
2
sudo apt-get update
sudo apt-get -y install nginx nginx-common nginx-full

This will install binaries that were built with the SPDY patch applied. Now you just have to enable it in your site’s configuration. You literally just add the keyword ‘spdy’ next to ‘ssl’ and you’re done. The relevant section should look something like this:

server {
    listen 443 ssl spdy;
    server_name example.com;
 
...

And that’s really it, your site is now being served up via SPDY if your browser supports it! If you’re using Google Chrome (and I recommend that you do), you can install the SPDY indicator extension to double check that everything is working. There is an equivalent extension for Firefox. The current SPDY implementation for Nginx isn’t perfect or complete, as there are some cool things that are not yet supported, most notably (to me) the server push features. But it should still get your pages served up faster to supported browsers on first touch than would otherwise happen.

If there are questions or comments, please let me know.

12 Comments

  1. Hi Chris,

    Just as a heads up, SPDY patch seems to be buggy because I keep getting these sort of errors in NGINX error log:

    2013/01/23 11:39:56 [alert] 2723#0: *4051 getsockname() failed (9: Bad file descriptor) while SPDY processing, client: x.x.x.x

    Then, the latest SPDY patch must be applied to fix that:

    http://nginx.org/patches/spdy/CHANGES.txt

    • Hi Heitor –

      The builds I have up currently have the newest SPDY patch (58 at the time of this writing) applied. Please try installing (apt-get update && apt-get upgrade will probably do it) and hopefully this will resolve your issue.

      • Hi Chris, thanks for the reply, I’m not sure what you mean by “installing the newest builds”. I’m currently using nginx/1.1.19.

        Can you provide a linux command to explain? Installing stuff on linux is not my strongpoint.

        • No worries Tim. The instructions you need are here:

          https://launchpad.net/~chris-lea/+archive/nginx-devel
          

          If you’re using Ubuntu Lucid or newer, this command should do it for you:

          sudo add-apt-repository ppa:chris-lea/nginx-devel
          

          After that, you can do

          sudo apt-get update
          sudo apt-get upgrade
          

          and it should install my Nginx builds that have SPDY enabled. Hope this helps!

          • Bam! awesome, that worked!

            I just did the 3 sudo commands you suggested then added “ssl spdy” to the listen 443 statement of my nginx.conf, restarted my server, loaded mysite.com on Chrome and it worked!

            This is a great day, thanks!

  2. Did exactly what you said but on restart got the following error message:

    invalid parameter “spdy” in /etc/nginx/nginx.conf:138

    Before that some of the apt-get update didn’t download properly either:

    W: Failed to fetch http://ppa.launchpad.net/kilian/trimage/ubuntu/dists/precise/main/source/Sources 404 Not Found

    W: Failed to fetch http://ppa.launchpad.net/kilian/trimage/ubuntu/dists/precise/main/binary-amd64/Packages 404 Not Found

    W: Failed to fetch http://ppa.launchpad.net/kilian/trimage/ubuntu/dists/precise/main/binary-i386/Packages 404 Not Found

    E: Some index files failed to download. They have been ignored, or old ones used instead.

    • Hi Tim –

      Please try installing the newest builds, they should fix that error. I’m not sure what’s going on with the 404 errors as that’s not my PPA, but it looks like those file aren’t there anymore.

  3. installed nginx 1.3.15 enabled the spdy module and added ssl spdy to the ssl host but http://spdycheck.org/ tells me that “Sorry, but this server is not including an NPN Extension during the SSL/TLS handshake.” suggestions?

Leave a Reply

Navigate