Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

2 Answers 2

up vote 3 down vote accepted

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
share|improve this answer

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;
share|improve this answer
    
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 '10 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. –  Martin Fjordvald Oct 27 '10 at 16:08
2  
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 '11 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. –  Martin Fjordvald Dec 8 '11 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 at 23:43

Your Answer

 
discard

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.