Nginx trys to download file instead of displaying

Remove default_type application/octet-stream;. This lines makes the browser think it's some binary data and not HTML.


As others noted, this is the line that causes your trouble:

default_type application/octet-stream;

This line overrides the line before it and tells Nginx to send any response as generic binary data. And, since browsers cannot do anything specific with binary data, they just download it as a file.


Removing or commenting that line, as well as the line above:

#include /etc/nginx/mime.types;
#default_type application/octet-stream;

should work, as default Nginx behaviour is to send everything as text/plain and browsers are smart enough to handle HTML/CSS/JS this way.

Despite that I wouldn't recommend such solution, as you may need to serve different types of content from your server, i.e. pictures, mp3's, binaries, etc.


Here's a better solution:

First, check that file /etc/nginx/mime.types; actually exists and accessible to read for the user that runs nginx.

Then, check that the file actually contains types { ... } declarations. That file comes packaged with Nginx and contains sensible defaults for mime-types to filename extensions mapping. If something is wrong with the file you can copy-paste its content from this gist:

https://gist.github.com/KondorB/dd34feba1d63bd468ded4ee70e59ea07

And finally, in nginx.conf change

default_type application/octet-stream;

to

default_type text/html;

Now Nginx will serve each type of content based on filename extensions. And if none provided, which is the case with some backend systems/apps, - will try to serve HTML.


Notice that you may want to change some of that defaults. For example, with that setting:

types {
    audio/mpeg mp3;
}

browsers will usually try to playback the song rather than straight up download it. Which can be changed, if needed, by declaring:

types {
    application/octet-stream mp3;
}