How can I check that the nginx gzip_static module is working?

后端 未结 6 832
遇见更好的自我
遇见更好的自我 2021-01-30 00:58

How can I check that nginx is serving the .gz version of static files, if they exist?

I compiled nginx with the gzip static module, but I don\'t see any mention of the .

相关标签:
6条回答
  • 2021-01-30 01:25

    You can use Chrome Dev Tools via the Network tab if you enable the Content-Encoding column (right-click on the columns to enable/disable specific content in the table):

    Chrome Dev Tools with Content-Encoding column enabled screenshot

    0 讨论(0)
  • I would disable automatic compression and log gzip_ratio:

    http {
       gzip off;
       gzip_static on;
    
       log_format log_with_ratio "... $gzip_ratio ...";
    
       server {
          access_log /var/log/nginx/file.log log_with_ratio;
       }
    }
    

    Note, that you may override gzip, gzip_static and access_log per server and location levels.

    0 讨论(0)
  • 2021-01-30 01:27

    Change the content of the non-gzipped file. And then touch both files (simultaneously—that is: in the same instantiation of touch). If when you load the file in an browser (cache-wiped) you get the non-changed file, then nginx served the static-cached-gzipped file.

    An easy way to avoid “did I just fetch the cache?” worries is to fetch from the command-line with curl since curl doesn’t cache.

    0 讨论(0)
  • 2021-01-30 01:28

    There is some hint I've noticed regarding ETag response header.

    If static file is served by nginx, then header looks like this: ETag: "135a-BQhu6KL71dyeCXcVZme6ug", however, when nginx is compressing the response (through gzip module) it looks like this: ETag: W/"135a-BQhu6KL71dyeCXcVZme6ug" (notice W/).

    You can use this as well as Content-Encoding: gzip to distinguish plain static files, pre-compressed static files, and files compressed on the fly.

    0 讨论(0)
  • 2021-01-30 01:29

    I usually use Chrome Dev tools and look at the file sizes for the files in question.

    0 讨论(0)
  • 2021-01-30 01:45

    Use strace. First, you need to detect PID of nginx process:

    # ps ax | grep nginx
    25043 ?        Ss     0:00 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
    25044 ?        S      0:02 nginx: worker process
    

    Ok, so 25044 is the worker process. Now, we trace it:

    # strace -p 25044 2>&1 | grep gz
    open("/var/www/css/ymax.css.gz", O_RDONLY|O_NONBLOCK) = 438
    open("/var/www/css/patches/patch_my_layout.css.gz", O_RDONLY|O_NONBLOCK) = -1 ENOENT (No such file or directory)
    open("/var/www/yaml/core/iehacks.css.gz", O_RDONLY|O_NONBLOCK) = -1 ENOENT (No such file or directory)
    open("/var/www/js/koznazna5.js.gz", O_RDONLY|O_NONBLOCK) = -1 ENOENT (No such file or directory)
    open("/var/www/css/ymax.css.gz", O_RDONLY|O_NONBLOCK) = 216
    

    As you can see, it is trying to find .gz versions of files.

    0 讨论(0)
提交回复
热议问题