Follow

Enable gzip compression for Nginx

  • Applies to: All DV
    • Difficulty: Medium
    • Time Needed: 20
    • Tools Required: SSH, vi knowledge

Overview

Nginx is a nimble web server that has become very popular in recent years. This article will show you how to turn gzip compression on, which can significantly increase the delivery speed of your sites. Nginx is the default http server for all Plesk versions 11+, and may be easily installed on cPanel servers as well. We'll be editing the nginx.conf file from the command line using a text editor, so you'll need to be at least a little familiar with SSH. These gzip edits are similar to mod_deflate, which is an Apache web server module that you may be familiar with. If you want to know how to enable mod_deflate for Apache servers, navigate to this article

NOTE:

  • Compressing files will increase load on your server, but it is a small tradeoff considering your client's connection times will decrease significantly.
  • This will not exclude users with older browsers that cannot handle compressed content. The browser negotiates with the server before any file is transferred, and if the browser does not have the capability to handle compressed content, the server delivers the files uncompressed.

Requirements

READ ME FIRST

This article is provided as a courtesy. Installing, configuring, and troubleshooting third-party applications is outside the scope of support provided by (mt) Media Temple. Please take a moment to review the Statement of Support.

Instructions

1. Start by verifying that you don't already have gzip compression enabled. The easiest way to do this is to navigate here and enter your domain.
 
 
 
  • If you are unsure of what type of web server you are using, open up a terminal window and use the curl command.
 curl -I www.YourDomainHere.com 

 

Your output should be similar to this:

CT-152541-bash-4.2# curl -I www.example.com
HTTP/1.1 200 OK
Server: nginx
Date: Fri, 09 Feb 2016 12:55:33 GMT
Content-Type: text/html
Content-Length: 5937
Connection: keep-alive
X-Accel-Version: 0.01
Last-Modified: Wed, 25 Jan 2016 13:43:03 GMT
ETag: "1730-52a50fb2994d1"
Accept-Ranges: bytes
X-Powered-By: PleskLin
MS-Author-Via: DAV

  • Your Server should be Nginx.
 
If you're using cPanel and would like to install Nginx, you may use the script below. There is currently no native support for Nginx on cPanel. Proceed at your own risk. This script is subject to change and is provided by nginxcp.com.
cd /usr/local/src
wget http://nginxcp.com/latest/nginxadmin.tar
tar xf nginxadmin.tar
cd publicnginx
./nginxinstaller install
/etc/init.d/httpd restart
  • Now verify that your server is Nginx by using the curl command from above.

If you choose to install Nginx on cPanel, you may run into conflicts with the existing Apache configuration. You can verify that Apache and Nginx are not both operating on the same port by logging into WHM and navigating to Server Configuration >> Tweak Settings. Use the search field to find Apache non-SSL IP/port and verify that it is set to 8080 (or another preferred port number) and not to port 80. If you find that gzip fails to load properly after rebooting your server, add the necessary gzip lines from step 2 to /usr/local/apache/conf/includes/post_virtualhost_global.conf instead of nginx.conf. Please remember that these steps for installation and configuration are subject to change and are not supported by Media Temple directly, but are provided as a courtesy.   

2. Let's turn gzip on by editing the nginx.conf file. Begin by connection to your server via SSH and opening the nginx.conf file with a text editor. If you are unfamiliar with how to use the vim editing tool, a helpful resource can be found here. Nano, vi, or any other preferred editor will also work fine. 

vim /etc/nginx/nginx.conf

Within the http section of nginx.conf, add or edit the following lines: 

http {
 gzip on;
 gzip_disable "MSIE [1-6]\.(?!.*SV1)";
 gzip_vary on;
 gzip_types text/plain text/css text/javascript application/javascript application/x-javascript;
}

 

It is likely that the gzip lines already exist in your nginx.conf file, but are either commented out using a # or have different values assigned to them. If the lines are commented out, you may either uncomment them and match the values, or simply add the new gzip values to the file. 

3. Restart Nginx and verify that gzip is working properly.

 

service nginx restart

 

  • Verify that gzip compression is running properly by navigating here and entering your domain. 

 

Your server should now be configured to use gzip compression. If you've added the proper lines to nginx.conf and restarted the Nginx service but gzip compression is still not working, try rebooting your entire server. As always, feel free to contact our great 24/7 support team for additional assistance. 

 

Resources

 

Was this article helpful?
0 out of 0 found this helpful

Comments