10

this is my very first question and please excuse my poor English.

I was doing research about how can i improve my page speed and i found out about nginx gzip settings.

Below are my gzip settings from nginx.conf

gzip on;
gzip_disable "msie6";

gzip_vary on;
gzip_proxied expired no-cache no-store private auth;
gzip_comp_level 5;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript application/vnd.    ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon
gunzip on;
gzip_static on;

I assume with these settings, nginx will serve .gz compressed file instead of original one if it exists. If not nginx will compress them when user requests them. Correct me if i'm wrong, am i on the right track?

My question is: Does Nginx compress original files every time new user requests it? Or does it serve cached/saved version of those compressed files?

Example: I have these files in my static folder.

/static/css/main.css
/static/css/main.css.gz
/static/js/main.js
/static/js/main.js.gz
/static/html/index.html

When userA requests index.html file Nginx will compress the file on the fly, But what if userB requests the same index.html file, does Nginx compress the file again or will it serve cached/saved version from somewhere?

Andrew Schulman
  • 8,561
  • 21
  • 31
  • 47
Shinebayar G
  • 275
  • 4
  • 9
  • I believe that Nginx compresses on the fly for each request. I think there is a way to gzip in advance and serve that compressed version, but I can't recall if it's built in, a plugin, or a configuration method. – Tim Apr 30 '18 at 19:35
  • 1
    Thank you for explanation, it seems you're right. I'm currently using this script to gzip files `find /path/to/static/files -type f -regextype posix-extended -regex '.*\.(htm|css|html|js)' -exec gzip -k -5 {} \;` – Shinebayar G May 01 '18 at 05:00
  • Always keep the orig and pre-compressed files with same modification timestamp. After gzip, do `touch /static/js/main.js /static/js/main.js.gz`. – Ethan Collins Aug 31 '18 at 02:42

1 Answers1

7

Yes Nginx will compress index.html each time it is requested since there is no index.html.gz file.

To be honest gzip is not very processor intensive these days and gzipping on the fly (and then unzipping in the browser) is often the norm. It’s something web browsers are very good at.

So unless you are getting huge volumes of traffic you’ll probably not notice any performance or CPU load impact due to on the fly gzipping for most web files.

Barry Pollard
  • 4,461
  • 14
  • 26
  • Thank you very much for explanation. I noticed CPU usage increase when i refresh the page multiple times. I guess this is it. – Shinebayar G May 01 '18 at 04:49
  • Quick question, do you think my configuration is correct? About serving precompressed .gz files when available and serve noncompressed files on the fly. The reason I'm asking is in [this](http://nginx.org/en/docs/http/ngx_http_gzip_static_module.html) page it said it's not build by default. But i just installed nginx by default using `sudo apt-get install nginx` and i guess it worked. Also I'm not sure about what does `gunzip_on`. – Shinebayar G May 01 '18 at 04:56
  • It’s probably a bit simplest to say it’s gzip causing that CPU increase. Try turning it off and repeating your test. The easiest test to see if precompressed files are being used would be to temporarily remove the uncompressed version and see if it’s delivered correctly still (which must mean it’s using the precompressed version). `Gunzip on` is used to allow `gzip_disable` to be used - which you have configured for IE 6. – Barry Pollard May 01 '18 at 07:53