18

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?

10

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

20

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.

  • 3
    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
4

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.

  • 4
    Using "add_header" it's absolutely wrong, use "default_type" instead: location /smth/ { default_type application/octet-stream;; } – Oleg Neumyvakin Oct 25 '15 at 8:12
  • @OlegNeumyvakin Why is default_type better than add_header? – user193661 Aug 17 '17 at 22:49
  • 2
    @user193661 Because "add_header" it's litterally adds new header "Content-Type", so in response you can get two "Content-Type" headers what can lead to strange behaviour of HTTP clients. "default_type" is set(overwrite) single "Content-Type" header in response. – Oleg Neumyvakin Aug 18 '17 at 7:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.