Looking through the Libravatar access logs, I found that most of the traffic we currently serve consists of 302 redirects to Gravatar. Optimising that path is therefore very important.
While Apache allows admins to provide custom error pages for things like 404 or 500, it's not quite that straightforward for 30x return codes.
Standard 301 / 302 responses
By default, Apache (and most web servers out there) returns a fairly large HTML page along with a 30x redirection. Try it for yourself by disabling automatic redirections in Firefox (Preferences | Advanced | General | Accessibility) or by installing the Request Policy add-on.
The 302 responses sent by Libravatar looked like this:
$ curl -i http://cdn.libravatar.org/avatar/12345678901234567890123456789012
HTTP/1.1 302 Found
Date: Wed, 21 Sep 2011 01:51:52 GMT
Server: Apache
Cache-Control: max-age=86400
Location: http://www.gravatar.com/avatar/12345678901234567890123456789012.jpg?r=g&s=80&d=http://cdn.libravatar.org/nobody/80.png
Vary: Accept-Encoding
Content-Length: 310
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>302 Found</title>
</head><body>
<h1>Found</h1>
<p>The document has moved <a href="http://www.gravatar.com/avatar/12345678901234567890123456789012.jpg?r=g&s=80&d=http://cdn.libravatar.org/nobody/80.png">here</a>.</p>
</body></html>
As you can see, the body of the response is just as large as the headers and isn't really necessary.
Body-less 301 responses
After reading about the ErrorDocument directive, I created an empty file called 302
in the root of the web server and included this directive in my vhost configuration file:
ErrorDocument 302 /302
which made the responses look like this:
$ curl -i http://example.com/redir
HTTP/1.1 302 Found
Date: Wed, 21 Sep 2011 03:39:26 GMT
Server: Apache
Last-Modified: Wed, 21 Sep 2011 03:39:17 GMT
ETag: "8024d-0-4ad6b52201036"
Accept-Ranges: bytes
Content-Length: 0
Content-Type: text/plain
This one does have a completely empty body, however, there's an important problem with this solution: the Location
header is missing! Not much point in reducing the size of the redirect if it's no longer working.
Custom 302 response page
The next thing I tried (and ended up settling on) is this:
ErrorDocument 302 " "
which results in a 1-byte response (a single space) in the body:
$ curl -i http://example.com/redir
HTTP/1.1 302 Found
Date: Wed, 21 Sep 2011 03:37:50 GMT
Server: Apache
Location: http://www.example.com
Vary: Accept-Encoding
Content-Length: 1
Content-Type: text/html; charset=iso-8859-1
There is still a little bit of unnecessary information in this response (character set, Vary
and Server
headers), but it's a major improvement over the original.
If you know of any other ways to reduce this further, please leave a comment!
Have you tried Apache's mod_headers, specifically the "Header" directive? That should allow you to eliminate headers you don't want.
I don't know how to end up with a completely empty body and not need a one-byte response, though. If you figure it out, please consider making a follow-up post.
Is the content of the page required for screen readers or something similar? Interesting that the setting to turn off automatic redirects is in the accessibility section.
I would be a little worried that I had broken something else by removing the content that some software may be expecting.
I may be totally wrong and it's just left over from a time when browsers did not support that header.
Yes, you're wrong, on both guesses. It's not 'left over' at all. The human-readable response text is there for humans who choose to use a text-based browser. Without the human-readable text, there would be way for the user to determine the problem short of carefully examining the headers.
And, yes, some of us actually do use text-based browsers; especially when we're wanting to look under the hood while diagnosing problems with servers and browsers.