3

To save space we gzip all our logs and text files and these files are browsed by the user, but these files are downloaded instead of opened in the browser, I couldn't find a way to set mime-type text/plain for such files e.g. this does not work

types {
    text/plain txt txt.gz log.gz
}

So is there a way in nginx to tell txt.gz and log.gz files are to be served as text/plain ?

3 Answers 3

2

Set up nginx gzip_static

example:

location / {
gzip_static on
}

http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html

3
  • Doesn't it require the original file to be there, so if user asks for my.txt and if my.txt.gz exists it will be served, but I can't keep original files Dec 1, 2014 at 21:28
  • It shouldn't require the original. As always, test it in dev first. Dec 1, 2014 at 21:31
  • I tested it and it doesn't work file is still served as application/octet-stream, which seems correct because why gzip_static should change mime types Dec 15, 2014 at 22:54
2

This worked for me, basically match location based on txt.gz extension and then for such files, set correct encoding and mime type:

location ~* /my/data/.*txt.gz$ {
    add_header  Content-Encoding  gzip;
    gzip off;
    types { text/plain gz; };
    root /;
}
1
  • This worked for me to serve gzipped IRC logs as if they were plain text. Using the root /; line resulted in 404s though.
    – dgw
    May 2, 2016 at 15:48
0

Nginx can't just decompress these files before serving them. You'd need some sort of script that does this for you then serves the results to the user. Attempting to force the .gz file as text will just result in garbage output.

3
  • Why they need to be decompressed? nginx should just serve them as text/plain with gzip encoding, Apache does that, or any other simple server Dec 1, 2014 at 21:23
  • 1
    It's not the same. Gzip compression on the server tells the web browser that it's serving otherwise plain-text content as gzipped and does it on-the-fly with plain data. A module would accomplish what you want (as shown by another answer).
    – Nathan C
    Dec 1, 2014 at 21:26
  • You may be right in general but browsers like chrome handles gz text files served as 'text/plain' and thats what I want, just to set correct mime-type Dec 1, 2014 at 21:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .