9

I have found an interesting problem.

I am trying to serve some gzipped files without the sources using NGINX's gzip_static module (I know the downsides to this). This means you can have gzipped files on the server that will be served with transfer-encoding: gzip. For example, if there's a file /foo.html.gz, a request for /foo.html will be served the compressed file with content-encoding: text/html.

While this usually works it turns out that when looking for index files in a directory the gzipped versions are not considered.

GET /index.html
200

GET /
403

I was wondering if anyone knows how to fix this. I tried setting index.html.gz as in index file but it is served as a gzip file rather then a gzip encoded html file.

3

2 Answers 2

6
+50

This clearly won't work this way.

This is a part of the module source:

 if (r->uri.data[r->uri.len - 1] == '/') {
     return NGX_DECLINED;
 }

So if the uri ends in slash, it does not even look for the gzipped version.

But, you probably could hack around using rewrite. (This is a guess, I have not tested it)

rewrite ^(.*)/$ $1/index.html;

Edit: To make it work with autoindex (guess) you can try using this instead of rewrite:

location ~ /$ { 
    try_files ${uri}/index.html $uri;
}

It probably is better overall than using rewrite. But you need to try ...

5
  • Thanks, this works! Unfortunately, it breaks autoindex. Do you think there's a way to make it work with autoindex?
    – ReyCharles
    May 15, 2015 at 9:04
  • @ReyCharles maybe this one is better. but i haven't tested
    – Fox
    May 15, 2015 at 9:28
  • Unfortunately that solution has the same problem.
    – ReyCharles
    May 15, 2015 at 10:13
  • Actually, scratch that - You need to put ${uri}/index.html in quotes. It'll only show the autoindex.
    – ReyCharles
    May 15, 2015 at 10:32
  • The rewrite worked for me. Since I did not need autoindex that was not a dealbreaker for me. Thank you Fox.
    – Sharmila
    Sep 12, 2015 at 13:32
0

You can prepare your precompressed files then serve it. Below it's prepared by PHP and served without checking if the client supports gzip.

// PHP prepare the precompressed gzip file
file_put_contents('/var/www/static/gzip/script-name.js.gz', gzencode($s, 9));
// where $s is the string containing your file to pre-compress
// NginX serve the precompressed gzip file
location ~ "^/precompressed/(.+)\.js$" {
    root /var/www;
    expires 262144;
    add_header Content-Encoding gzip;
    default_type application/javascript;
    try_files /static/gzip/$1.js.gz =404;
}
# Browser request a file - transfert 113,90 Kb (uncompressed size 358,68 Kb)
GET http://inc.ovh/precompressed/script-name.js

# Response from the server
Accept-Ranges bytes
Cache-Control max-age=262144
Connection keep-alive
Content-Encoding gzip
Content-Length 113540
Content-Type application/javascript; charset=utf-8
ETag "63f00fd5-1bb84"
Server NginX

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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