Applicable to:
- Plesk for Linux
Question
How to enable/disable gzip compression in nginx on a Plesk server?
Answer
Note: Since Plesk Obsidian 18.0.33 it is possible to use BROTLI compression which is considered to be better compression than GZIP.
For a domain in Plesk
-
Go to Domains > example.com > Hosting & DNS > Apache & nginx Settings.
-
Add the following directives to the Additional nginx directives field:
CONFIG_TEXT: gzip on;
gzip_disable "MSIE [1-6]\\.(?!.*SV1)";
gzip_proxied any;
gzip_comp_level 5;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/rss+xml text/javascript image/x-icon image/bmp image/svg+xml;
gzip_vary on;Note: This is an example. If needed, add other file types in the
gzip_types
section, e.g.application/javascript
,application/js
, etc. The full list of available types can be found on a server in the file/etc/nginx/mime.types
. -
Apply the changes.
-
Go to Domains > example.com > Hosting & DNS > Apache & nginx Settings.
Note: To disable gzip compression for a domain/subdomain that does not exist in Plesk (e.g. hostname), create it first.
-
Add the following directive to the Additional nginx directives field:
CONFIG_TEXT: gzip off;
-
Click Apply to save the changes.
For all domains via nginx global configuration
-
Connect to a Plesk server via SSH.
-
Create the
gzip.conf
file inside nginx conf.d directory:# touch /etc/nginx/conf.d/gzip.conf
-
Open the file in any text editor. In this example, we are using the vi editor:
# vi /etc/nginx/conf.d/gzip.conf
-
Insert the following content in it:
CONFIG_TEXT: gzip on;
gzip_disable "MSIE [1-6]\\.(?!.*SV1)";
gzip_proxied any;
gzip_comp_level 5;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/rss+xml text/javascript image/x-icon image/bmp image/svg+xml;
gzip_vary on;Note: This is an example. If needed, add other file types in the
gzip_types
section, e.g.application/javascript
,application/js
, etc. The full list of available types can be found on a server in the file/etc/nginx/mime.types
. -
Save the changes and close the file.
-
Test nginx configuration:
# nginx -t
-
Reload
nginx
configuration to apply the changes:# service nginx reload
-
Now gzip compression is enabled for all domains on the server.
To verify that gzip compression is enabled, use the command below. When gzip is enabled you will see 'Content-Encoding: gzip' in the output:
Note: If the website has a www redirection, change the website name to www.example.com.
-
If the website is using HTTP connection:
# curl -s -H "Accept-Encoding: gzip" -I http://example.com | grep -i Content-Encoding
Content-Encoding: gzip -
If the website is using HTTPS connection:
# curl -s -H "Accept-Encoding: gzip" -I https://example.com --insecure | grep -i Content-Encoding
Content-Encoding: gzip
-
-
Connect to a server via SSH.
- Search for the line in "gzip on" via all nginx config files:
# grep -Ri "gzip on" /etc/nginx/
-
To disable gzip compression, open the corresponding file in a text editor and change
gzip on
togzip off
. -
Save the changes and close the file.
-
Restart nginx to apply the changes:
# service nginx restart
Additional Information
To learn more about gzip compression module and gzip types, visit the following nginx documentation page:
To enable gzip compression in Apache, visit this KB article.
Comments
20 comments
Thanks a lot! Great 'n' easy tutorial.
Isn't text/html most important ? It is missing from the code.
@adrianTNT
Hello Adrian! According to nginx official documentation, for the option gzip_types the mime-type text/html type is always compressed by default, so there is no need to specify it explicitly.
You can check right here:
https://nginx.org/en/docs/http/ngx_http_gzip_module.html
Is there any disadvantages to enable gzip globally? We often have reqests if we can enable gzip - there i want to insert this globally.
But will this harm any Websites in whatever way? Or is failproved ?
@Lenor,
When compressing is enabled, additional server RAM & CPU resources are utilized to compress websites content on the fly. If there are many websites on the server, the load may increase dramatically.
@Ivan uff. We have 300 Websites on a Server. Theres 64GB RAM.
Any example how i can determine if this will work or not?
@Lenor If you mean how enabling gzip compression affect server performance - the only way is to monitor the server performance. Establish some performance baseline before enabling and see how the baseline will be changed after enabling gzip.
If you want to check whether gzip compression works you may use curl command. When gzip enabled you will see in the output 'Content-Encoding: gzip'. You can check individual files instead of pages to ensure they have been gzipped as well.
# curl -s -H "Accept-Encoding: gzip" -I http://domain.tld | grep Content-Encoding
Content-Encoding: gzip
Hi there,
I need help on this one,
I have a Plesk Server but the Additional nginx Directives is not showing at all even from the admin account!
Plesk Info:
Version 17.8.11 Update #39
Hello @Berat,
Most probably this is because Nginx is not installed or disabled on your server.
This article should help:
https://support.plesk.com/hc/en-us/articles/213944825-How-to-install-and-enable-nginx-reverse-proxy-on-a-Plesk-for-Linux-server
I think the mime type for RSS should be
application/rss+xml
instead ofapplication/xml
+rss
?@Steve Yates
Thank you for pointing it out! I have changed the article accordingly.
Hi Plesk Team,
somewhere in Plesk Forums I got the information to set the serverwide nginx gzip configuration in the http section of /etc/nginx/nginx.conf. Is it correct or is it better like in your example for some reasons? I use ONLY NGINX (no Apache / no Proxy) for webhosting.
http {
......
gzip_static on;
gzip on;
gzip_proxied any;
gzip_http_version 1.1;
gzip_min_length 100;
gzip_vary on;
gzip_comp_level 6;
gzip_buffers 16 8k;
gzip_types text/plain text/css application/x-javascript text/xml application/xml text/javascript image/x-icon image/bmp image/svg+xml application/javascript application/js application/xhtml+xml application/rss+xml application/x-font-ttf application/vnd.ms-fontobject font/opentype font/ttf font/eot font/otf image/jpg image/jpeg image/png image/gif application/json;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
......
}
Hello,
Yes, you can define all the settings in main configuration file and they will work for all hosted websites. There are no known reasons to not to do it.
However defining additional directives in separate file (in article it is /etc/nginx/conf.d/gzip.conf) is like a best practice for nginx.
When you enable the gzip option in nginx, do you need to enable it in apache as well, or do you have to choose between the two? I'm saying this because I think it's usual to have nginx as an apache proxy.
Hi @Nerque,
Since Nginx is used as an Apache proxy, gzip compression should be enabled for Nginx and disabled for Apache. In this configuration, static files are served by Nginx.
Information on how to enable compression for Apache can be found here:
https://support.plesk.com/hc/en-us/articles/115000716649
Hello I can't find the option you are talking about in Enabling gzip compression for a domain in Plesk
There is no such option available of the name Additional nginx directives where I can put the code
Hello @Mannan Raimagia,
The menu is not available for non-admin users. You logged in as a customer, so you cannot manage additional nginx directives. I will add the explanation in the article to make it complete.
Now, for you to add the additonal nginx directives, contact your Plesk provider who sent you access details to Plesk. Your provider, as Plesk administrator, can manage additional nginx directives.
When using that configuration in Plesk, I get the following error (Screenshot attached):
"Invalid INI syntax detected on line 2: 'gzip_disable `MSIE [1-6].(?!.*SV1)`;'"
I thought it was related to the double quote, tried single and ` but got the same error. Could you please assist? thank you.
Hi Motaz Elshazly
It looks like you trying to add additional nginx directives to the field in PHP Settings instead of nginx settings.
The correct field is at Domains > example.com > Hosting & DNS > Apache & nginx Settings > Additional nginx directives.
Worked a treat for me. Thanks.
Please sign in to leave a comment.