Zac Fukuda
056

Nginx gzip on Ubuntu

Environment

  • OS: Ubuntu 22.04.3
  • Nginx: 1.18.0

Configuration

Take a look at the content of main configuration file nginx.confwith cat:

sudo cat /etc/nginx/nginx.conf

You find the following configuration:

	##
	# Gzip Settings
	##

	gzip on;

	# gzip_vary on;
	# gzip_proxied any;
	# gzip_comp_level 6;
	# 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;

As you see, gzip is enabled.

You can edit nginx.conf. I think, however, it is better to keep nginx.conf as much original as possible, and to make a dedicated configuration file for gzip.

sudo vi /etc/nginx/conf.d/gzip.conf

And the content of gzip.conf shall be:

gzip_disable "msie6";
gzip_vary on;
gzip_proxied any;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_http_version 1.1;
gzip_min_length 256;
gzip_types
	application/javascript
	application/json
	application/ld+json
	application/manifest+json
	application/rss+xml
	application/x-javascript
	application/xhtml+xml
	application/xml
	application/xml+rss
	font/opentype
	font/otf
	image/svg+xml
	text/css
	text/javascript
	text/plain
	text/xml;

Please find the feature of each directive at Module ngx_http_gzip_module.

Only common MINE types for web application are listed in gzip_types.

Test Config

Run Nginx test command:

sudo nginx -t

If the configuration is OK, the output will be:

nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart Nginx

Restart Nginx so that the modification is effective:

sudo service nginx restart

Ensure if you can access to your web application.

Resources