Nginx – Does Nginx compress files for every request? (with gzip_on)

gzipnginx

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?

Best Answer

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.