Nginx: serving files as text without having .txt appended to it

mime-typenginx

I have some .rb (Ruby script) files that I link to on my website.

By default, the browser just downloads them to the "Downloads" folder, as it does zip files.

I want the browser to treat it as text, so the Ruby script will show within the browser.

If I do this the normal way however, it does indeed show up in the browser, but the problem is that when the user does a "right-click save as" on the file, the downloaded file gets appended with .txt, such as myscript.rb.txt

Can this .txt extension be avoided with nginx settings?

Best Answer

You can specify in which mimetype you would like nginx to serve files by setting the types directive in your nginx configuration. I don't know about other linux distributions, but if you're using ubuntu, /etc/nginx/mime.types is included by default.

To serve .rb files as text\plain make sure /etc/nginx/mime.types contains the following:

types {
   ...
   text/plain    txt rb;
   ....
}

Then you should be able to download the files as txt without the txt extension. I did this using Chrome/Mac OS X and it worked as expected. Note: This means that if you are serving multiple sites with nginx all will return files with the .rb extension as text.