11

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).

3 Answers 3

18

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
1
  • 1
    with tornado: tornado.web.RequestHandler.clear(self) self.set_header('X-Accel-Redirect', redirect_location) self.clear_header('Content-Type') as the .clear method sets content-type to text/html
    – Anthony
    Jan 16, 2015 at 18:17
1

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;
5
  • Thank you for your answer. I want to server different content types so that images render as images and zip files are offered as a download option. So unfortunately this does not quite solve it.
    – Tomas Kohl
    Oct 27, 2010 at 14:45
  • Did you try it or are you just assuming it won't work? By ignoring the back-end supplied header it should try to determine it based on the file. Oct 27, 2010 at 16:08
  • 3
    One cannot ignore Content-Type. According to wiki.nginx.org/HttpFcgiModule#fastcgi_ignore_headers possible values are X-Accel-Redirect, X-Accel-Expires, Expires or Cache-Control
    – jnns
    Dec 7, 2011 at 17:11
  • 1
    "It is possible to specify headers like" while shitty phrasing does not mean "the possible values are". I have ignored Content-Type before in combination with x-accel-redirect so I don't think it's impossible, though, I'll admit I'm not intimate with the gritty details. Dec 8, 2011 at 15:51
  • @MartinFjordvald, tried it and nginx refused to start complaining nginx: [warn] invalid value "Content-Type" in /etc/nginx/conf.d/default.conf:27 but fastcgi_ignore_headers "Expires" works
    – dnozay
    Feb 27, 2014 at 23:43
1

with php-fpm:

<?php
    header("Content-Type: ");
    header("X-accel-redirect: (...)");

tells php to not send a Content-Type header at all (not even an empty one), and nginx will guess the type for you and add the header :)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .