Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NGINX Serve Precompressed index file without source

Tags:

nginx

gzip

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.

like image 853
Kevin Cox Avatar asked Apr 21 '14 05:04

Kevin Cox


1 Answers

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 ...

like image 52
Fox Avatar answered Nov 07 '22 20:11

Fox