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 ?

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 /;
}
  • 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 '16 at 15:48
1

Set up nginx gzip_static

example:

location / {
gzip_static on
}

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

  • 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 – Anurag Uniyal Dec 1 '14 at 21:28
  • It shouldn't require the original. As always, test it in dev first. – Andrew Domaszek Dec 1 '14 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 – Anurag Uniyal Dec 15 '14 at 22:54
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.

  • 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 – Anurag Uniyal Dec 1 '14 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 '14 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 – Anurag Uniyal Dec 1 '14 at 21:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.