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.

I have Rails application (Redmine) which works with Nginx. If I am clicking on any attachment my browser (firefox, ie, chrome) ask me to download file. But if I am clicking on txt-type attachment my browser open this file in the browser.

As I understand it is task of Nginx to decide - open file in the browser or to download it. How can I setup it?

share|improve this question

3 Answers 3

up vote 6 down vote accepted

Default type for txt extension is text/plain. This behavior is described in mime.types file. Use http://nginx.org/en/docs/http/ngx_http_core_module.html#default_type or http://nginx.org/en/docs/http/ngx_http_core_module.html#types to override it (maybe set to application/octet-stream).

share|improve this answer

Changing the Content-Type of .txt files may work, but it's a somewhat risky way to solve this problem, because you can't guarantee 100% that the user's browser will respond how you expect. And furthermore, it's misleading to label a .txt file as a binary file.

Instead, I suggest using the standard way to force a browser to download rather than display the file, which is to use the Content-Disposition header with a value of attachment (RFC 2183, see also RFC 2616).

For example, this nginx location block will send down such a header with .txt files under the /downloads/ URI and thus they will be forced to download:

location ~ ^/downloads/.*\.txt$ {
  add_header Content-Disposition "attachment";
}

So you could add other appropriate matching, etc, appropriate to your configuration.

share|improve this answer
    
It seems to be the best solution. –  Vadim Feb 28 '12 at 17:17
    
But Content-Disposition hack in chromium browser can start saying bad words into console and even stop downloading any files from page. It says thinhgs (with yellow triangles) like: "Resource interpreted as Document but transferred with MIME type image/png:" –  Nakilon May 24 '13 at 16:00

You need to extract location for downloadable file and set their content-type as application/octet-stream.

We have following location on our service for downloadable promomaterial (which includes pdfs, images and some docs, but I'm sure it will work with txt files as well):

location /promomaterial/download/ {
         add_header Content-Type application/octet-stream;
}

Every browser we've tried downloads all files from this location instead of trying to display them.

share|improve this answer

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.