nginx, x-accel-redirect and mime types

In my nginx 0.8.34 setup I'm using the X-Accel-Redirect feature to control file downloads in the application code while not having the application handle the download itself.

After much pain this now basically works except nginx always returns the file with text/html content type.

The default content type is application/octet-stream, specified in the http block.

The server block contains, among other things, the definition of the directory where the files are stored:

location /files {
  default_type  application/octet-stream;
  alias /srv/www/uploads;
  internal;  
}

So I specified the content type even here but nothing has changed.

I don't want to set the Content-Type by the application because then I would slow me down (I'd first have to determine it). So ideally nginx would return the correct mimetype based on the file extension (I do include mime.types in the http block).


If you want to let nginx guess the right mime type, you juste have to make sure no content-type is returned from your backend server.

With django:
    response = HttpResponse()
    response['Content-Type'] = ''
    response['X-Accel-Redirect'] ='/my/file.jpg'
    return response

I personally just set application/octet-stream in the application but you might be able to use fastcgi_ignore_headers to prevent Nginx from using the back-end supplied header.

fastcgi_ignore_headers Content-Type;