Display any file in a browser as plain text

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

Don’t download a file, don’t execute it, just view it as raw is same fashion as .txt files.

The key is to add

Content-Type text/plain

to the header.

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
location /code/ {
    # All files in it
    location ~* {
        add_header Content-Type text/plain;

See, it works.


Club-Mate, the beverage → club-mate.fi