How to tell gzip_static not to search for image files?

I have nginx installed with gzip_static activated. It works great for CSS and JavaScript files, but it also looks for gzipped versions of image files such as .png and .gif, although they are not on the list of compressed files:

# strace -p 25044 2>&1 | grep gz
open("/var/www/css/ymax.css.gz", O_RDONLY|O_NONBLOCK) = 438
open("/var/www/images/tools.png.gz", O_RDONLY|O_NONBLOCK) = -1 ENOENT (No such file or directory)
open("/var/www/images/ads/bs.gif.gz", O_RDONLY|O_NONBLOCK) = -1 ENOENT (No such file or directory)
open("/var/www/images/gfxborder/border_right.gif.gz", O_RDONLY|O_NONBLOCK) = -1 ENOENT (No such file or directory)
open("/var/www/images/ads/hocuto03.gif.gz", O_RDONLY|O_NONBLOCK) = -1 ENOENT (No such file or directory)

Here is my nginx configuration:

gzip  on;
gzip_disable     "msie6";
gzip_min_length 1000;
gzip_types text/plain text/css application/x-javascript text/javascript;
gzip_static on;

Is there any way to prevent this?

+3
source share
3 answers

I have found a solution. In fact, there are two possible solutions:

  • save javascript and CSS files in a separate directory and create a "location" rule to use gzip_static only for this directory

  • ( -, , ), open() open_file_cache_errors, , ( ), .gz .

+5

nginx, :

  • :

ngx_http_gzip_static_module.c ( 1.0.1, ):

2.in:

ngx_http_gzip_static_handler (ngx_http_request_t * r)

3.:

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

  • :

if (r- > uri.data [r- > uri.len - 1] == 'g' & r- > uri.data [r- > uri.len - 2] == 'n '& r- > uri.data [r- > uri.len - 3] ==' p '& r- > uri.data [r- > uri.len - 4] =='. ') { return NGX_DECLINED; } if (r- > uri.data [r- > uri.len - 1] == 'g' && r- > uri.data [r- > uri.len - 2] == 'p' && r- > uri.data [r- > uri.len - 3] == 'j' & r- > uri.data [r- > uri.len - 4] == '.') { return NGX_DECLINED; } if (r- > uri.data [r- > uri.len - 1] == 'o' && r- > uri.data [r- > uri.len - 2] == 'c' && r- > uri.data [r- > uri.len - 3] == 'i' & r- > uri.data [r- > uri.len - 4] == '.') { return NGX_DECLINED; }

  • .

- , C , , C: (

0

, :

  location /assets {
    root   /usr/share/nginx/html;
    location ~ .*.(js|css|html|png)+$ {
      # Use for debugging
      # add_header 'X-static-gzipping' 'on' always;
      gzip_static on;
    }
  }
0

Source: https://habr.com/ru/post/1770404/


All Articles