How to enable GZip for CSS and JavaScript files on Nginx

Below are the steps to enable gzip for CSS and JavaScript files on Nginx.

Run the following command on your Ubuntu’s terminal to open nginx.conf file:

sudo nano /etc/nginx/nginx.conf

Scroll donw to Gzip Settings section in that file:

Add the following line in that section:

gzip_types text/plain text/css application/json application/javascript text/xml application/xml application/xml+rss text/javascript;

Hit CTRL + O and CTRL + ENTER to save the file.

Run the following command to restart the nginx :

sudo service nginx restart

When you access the JS ans CSS files they will be gzipped served from the server. You can verify the response headers:

content-encoding: gzip
content-type: application/javascript
date: Tue, 14 Jun 2022 09:55:10 GMT
etag: W/"62a760ac-c3b"
last-modified: Mon, 13 Jun 2022 16:07:08 GMT
server: nginx/1.15.0 (Ubuntu)

content-encoding: gzip line in the response is the proof that the content is gzipped now.