Using X-Sendfile in Rails with lighttpd

November 2, 2006 Link to post  Permalink

As a followup to my post on using x-sendfile, I sent some email to Jan, owner of lighttpd. He clued me into the mod_setenv module that allows me to write this in my lighttpd.conf:

1etenv.add-environment = (
2  "ENABLE_X_SENDFILE" => "true"
3)

1etenv.add-environment = (
2 "ENABLE_X_SENDFILE" => "true"
3)

as well as the allow-x-send-file property for FastCGI and soon to be proxy-core configuration.

Now my Rails source code can look like this:

1 if request.env["ENABLE_X_SENDFILE"] == "true"
2    response.headers["X-Sendfile"] = filename_to_send
3  else
4    response.body = read_file(filename_to_send)
5  end

The configuration to enable x-sendfile is duplicated, but at least it’s in a single .conf file.