Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I installed the apache dev headers:

sudo apt-get install apache2-prefork-dev

Downloaded and compiled the module as outlined here:

Added the following line to /etc/apache2/mods-available/xsendfile.load:

LoadModule xsendfile_module /usr/lib/apache2/modules/

Added this to my VirtualHost:

<VirtualHost *:80>
    XSendFile on
    XSendFilePath /path/to/protected/files/

Enabled the module by doing:

sudo a2enmod xsendfile

Then I restarted Apache. Then this code still just provides me with an empty file with 0 bytes:

file_path = '/path/to/protected/files/'
file_name = ''
response = HttpResponse('', mimetype='application/zip')
response['Content-Disposition'] = 'attachment; filename=%s' % smart_str(file_name)
response['X-Sendfile'] = smart_str(file_path)
return response

And there is not in the Apache error log that pertains to XSendFile. What am I doing wrong?

share|improve this question
What do the headers look like that are received by the client? Have you tried setting a more verbose log level (e.g., LogLevel debug)? – larsks Apr 21 '12 at 0:45

I got my code to work. The only difference is:

def serve_file(request, file):
    response = HttpResponse(mimetype='application/force-download')
    response['Content-Disposition'] = 'attachment; filename="%s"' % smart_str(os.path.basename(_(
    response['X-Sendfile'] = smart_str(_(file.file.path))
    # It's usually a good idea to set the 'Content-Length' header too.
    # You can also set any other required headers: Cache-Control, etc.
    return response
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.