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 ?

masegaloeh
  • 18,236
  • 10
  • 57
  • 106
Anurag Uniyal
  • 151
  • 1
  • 4

3 Answers3

2

Set up nginx gzip_static

example:

location / {
gzip_static on
}

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

Andrew Domaszek
  • 5,163
  • 1
  • 15
  • 27
  • 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 01 '14 at 21:28
  • It shouldn't require the original. As always, test it in dev first. – Andrew Domaszek Dec 01 '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
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 /;
}
Anurag Uniyal
  • 151
  • 1
  • 4
  • 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 02 '16 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.

Nathan C
  • 15,059
  • 4
  • 43
  • 62
  • 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 01 '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 01 '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 01 '14 at 21:35