django through lighttpd + mod_proxy + gunicorn: X-Sendfile returns empty responses

341 Views Asked by At

I'm using Django behind Gunicorn and lighttpd. As of Gunicorn documentation, I configured Lighttpd to act as reverse proxy towards Gunicorn:

$SERVER["socket"] == "0.0.0.0:80" {
    server.document-root = "/home/user/app"
    server.errorlog = "/var/log/lighttpd/app-error.log"
    accesslog.filename = "/var/log/lighttpd/app-access.log"

    $HTTP["url"] !~ "^/static/" {
        proxy.server = (
            "" => (
                (
                    "host" => "127.0.0.1",
                    "port" => 8888,
                    "allow-x-send-file" => "enable"
                )
            )
        )
    }
}

So far so good, everything works and even static files are served correctly.

Now I'm trying to use the header X-Sendfile. In my Django code:

response['Content-Type'] = 'application/force-download'
response['Content-Disposition'] = f'attachment; filename={smart_str(send_name)}'
response['X-Sendfile'] = '/tmp/big-file.txt'

/tmp/big-file.txt exists and is accessible by the Lighttpd process.

Every time I try to serve a file this way, the browser gets an empty response; no payload and Content-Length: 0.

I find nowhere any error from Lighttpd (I think the Django + Gunicorn part is fine) about what's going wrong.

What can I do? Where's my mistake?

1

There are 1 best solutions below

0
gstrauss On

Check that your script did not already send response headers before you tried to set X-Sendfile.

You can use strace on the lighttpd process to see what is received from the backend, as well as to see if lighttpd is trying to open '/tmp/big-file.txt'