How to configure nginx to show file content instead of downloading it?

Thanks to Sergey Moiseev's comment

the answer is quite simple.

go to your configuration file and add the following

types {
    text/plain sh;
}

this maps the extension .sh to mime-type text/plain


Nginx do this in a very simpler way.

location ^~ /log {
           sendfile on;
           sendfile_max_chunk 1m;
           alias  /var/log/nginx;
           autoindex on;
           try_files $uri $uri/ =404;

           types {
                    text/plain log;
                }
        }

In the above example, it will match the file with extension .log as plain text added in nginx log.


Just set the content type in the location block.

In my case I want to serve a VERSION file, but by default the browser downloads it (because it's served as application/x-octet-stream). If I make nginx send it with a Content-type of text/plain, the browser will display it instead of downloading it.

location = /VERSION {
    root /usr/share/nginx/html;
    add_header Cache-Control 'must-revalidate';
    add_header Content-Type text/plain;
}

Tags:

Nginx