4

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?

  • 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
  • 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 1 '18 at 5: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 2:42
3

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.

  • 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 1 '18 at 4: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 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 1 '18 at 4: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 1 '18 at 7:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.