clubmate.fi

A good[ish] website

Web development blog, loads of UI and JavaScript topics

Display any file in a browser as plain text

Filed under: Server— Tagged with: nginx, snippets

Here’s how to add Content-Type: text/plain with NGINX to all files inside a given directory.

If you don’t want to download a file, nor execute it, but just view it as raw text in browser, in same fashion as .txt files. You can tell the web server to render it as plain text, but setting content type in the headers:

Content-Type text/plain

Edit headers with NGINX

With NGINX you can do it like this:

location test.php {
  add_header Content-Type text/plain;
}

That goes into your NGInx Server Block file. It only targets a single file, some regex can be added to target all files in a specific directory for instance:

# Target code dir
location /code/ {
  # All files in it
  location ~* {
    add_header Content-Type text/plain;
  }
}

Comments would go here, but the commenting system isn’t ready yet, sorry.

  • © 2022 Antti Hiljá
  • About
  • All rights reserved yadda yadda.
  • I can put just about anything here, no one reads the footer anyways.
  • I love u!