Home > Back-end >  Does nginx automatically served gzipped location matches when gzip_static is on?
Does nginx automatically served gzipped location matches when gzip_static is on?

Time:12-09

I have a question about serving gzipped static files from nginx. I did gzip -k style.min.css to produce style.min.css.gz and I uploaded it to the server in the static-root directory. My location block is an exact match and looks like this:

location =/style.min.css {
  root /home/ubuntu/.../static-root/;
  gzip_static on;
  expires 100d;
  add_header Cache-Control "public";
  access_log off;
}

Will nginx just serve up the style.min.css.gz in place of the style.min.css automagically, or do I have to tweak that location block so that the gzipped version is served?

Given the exact match at that location block this test does 404

$ curl -I https://example.com/style.css.gz -H "Accept-Encoding: gzip"
HTTP/1.1 404 Not Found

Is the compressed version still getting served up or do I need to tweak the location block to something like this so that the .gz file gets served?

location ~ /(style.min.css.*) {...

Update ... I can confirm that a gzipped version of a static css file is returned. So it seems to happen automatically. I don't know if the gzip on; in the server section or the gzip_static on; takes care of it but it is working.

$ curl -H "Accept-Encoding: gzip" -I https://example.com/bsmin.css
HTTP/1.1 200 OK
Cache-Control: max-age=7673705, public
Cache-control: no-cache="set-cookie"
Content-Encoding: gzip

CodePudding user response:

From the official documentation https://docs.nginx.com/nginx/admin-guide/web-server/compression/ under Sending Compressed Files

to service a request for /path/to/file, NGINX tries to find and send the file /path/to/file.gz. If the file doesn’t exist, or the client does not support gzip, NGINX sends the uncompressed version of the file.

Note that the gzip_static directive does not enable on-the-fly compression. It merely uses a file compressed beforehand by any compression tool. To compress content (and not only static content) at runtime, use the gzip directive.

  • Related