在本文中,我们配置了已经安装在CentOS 7上的Nginx,以使用gzip压缩为客户端提供服务,以减小发送给网站访问者的内容的大小。我们可以将网站配置为更快地加载,具体取决于网络浏览器下载的所有文件的大小,我们可以通过减少从网站传输的文件的大小并更快地加载来完成此操作,这也降低了成本我们为带宽使用付费。gzip是一个流行的数据压缩包,我们将使用gzip配置Nginx来压缩服务器上的文件以更快地加载文件。
在这里,我们将在默认的Nginx目录中创建一些演示文件,以测试gzip压缩。然后,Nginx将检查文件的内容,它仅确定MiME类型并确定文件的用途。
由于Nginx不会压缩非常小的文件,因此我们将创建大小约为1 KB的文件,以验证Nginx是否在适当的地方使用了压缩。我们使用truncate命令在默认的Nginx目录中创建一个名为demo.html的1 KB文件。
扩展名表示它是HTML
$ sudo truncate -s 1k /usr/share/nginx/html/demo.html
我们还将使用相同的命令创建更多演示文件:一个.jpg图像文件,一个.css样式表和一个.js JavaScript文件。
$ sudo truncate -s 1k /usr/share/nginx/html/demo.jpg $ sudo truncate -s 1k /usr/share/nginx/html/demo.css $ sudo truncate -s 1k /usr/share/nginx/html/demo.js
现在,我们将重新安装刚刚创建的文件,以检查Nginx在压缩方面的表现。我们将检查带有压缩的demo.html。
以下是使用gzip压缩从Nginx服务器请求文件的命令,我们还可以使用Accept-Encoding:gzip进行卷曲以测试压缩。
$ curl -H "Accept-Encoding: gzip" -I http://localhost/demo.html Output: HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Fri, 11 July 2016 18:53:06 GMT Content-Type: text/html Content-Length: 1024 Last-Modified: Fri, 11 July 2016 18:48:02 GMT Connection: keep-alive ETag: "56eg2be82-400" Accept-Ranges: bytes
出于默认原因,在Nginx配置中默认禁用gzip压缩,原因是我们在上面的输出中没有看到Content-Encoding:gzip。现在,我们将以与Nginx压缩图像相同的方式测试名为demo.jpg的图像。
$ curl -H "Accept-Encoding: gzip" -I http://localhost/demo.jpg Output: HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Fri, 11 July 2016 18:58:43 GMT Content-Type: image/jpeg Content-Length: 1024 Last-Modified: Fri, 11 July 2016 18:48:35 GMT Connection: keep-alive ETag: "563e2be85-400" Accept-Ranges: bytes
这里的输出也没有压缩
我们还将测试CSS样式表
$ curl -H "Accept-Encoding: gzip" -I http://localhost/demo.css Output: Nginx response headers for CSS file HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Fri, 11 July 2016 16:59:34 GMT Content-Type: text/css Content-Length: 1024 Last-Modified: Fri, 11 July 2016 12:48:55 GMT Connection: keep-alive ETag: "56e2be85-400" Accept-Ranges: bytes
默认情况下,当我们安装Nginx时,会安装gzip压缩模块,但未在Nginx gzip模块中启用它。我们将在/etc/nginx/conf.d目录中创建一个配置文件,该文件在启动Nginx服务时会自动加载
$ sudo vi /etc/nginx/conf.d/gzip.conf Files contents: ## # `gzip` compresstion enableing Settings # # gzip on; gzip_disable "msie6"; gzip_vary on; gzip_proxied any; gzip_comp_level 6; gzip_http_version 1.1; gzip_min_length 256; gzip_types text/plain text/css application/json application/x-javascript text/xml application/xml application/xml+rss text/javascript application/javascript application/vnd.ms-fontobject application/x-font-ttf font/opentype image/svg+xml image/x-icon;
配置设置,在上面的配置文件中使用。
gzip on - directive enables the Gzip compression. gzip_disable "msie6" - excludes Internet Explorer 6 from the browsers that will receive compressed files, because IE6 does not support gzip at all. gzip_vary and gzip_proxied - settings make sure that proxy servers between the browser and the server will recognize compression correctly. gzip_comp_level 6 - sets how much files will be compressed. The higher the number, the higher the compression level and the resources usage. 6 is a reasonable middle ground. gzip_http_version 1.1 - is used to limit gzip compression to browsers supporting the HTTP/1.1 protocol. If the browser does not support it, there is a good chance it does not support gzip either. gzip_min_length 256 - tells Nginx not to compress files smaller than 256 bytes. Very small files barely benefit from compression. gzip_types lists all - of the MIME types that will be compressed. In this case, the list includes HTML pages, CSS stylesheets, Javascript and JSON files, XML files, icons, SVG images, and web fonts.
$ sudo systemctl restart nginx
我们将使用命令提示符下的curl命令测试.html文件,以检查压缩是否已启用
$ curl -H "Accept-Encoding: gzip" -I http://localhost/demo.html Output: Nginx response headers HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Fri, 11 July 2016 17:19:46 GMT Content-Type: text/html Last-Modified: Fri, 11 Mar 2016 17:38:22 GMT Connection: keep-alive Vary: Accept-Encoding Content-Encoding: gzip
我们还可以对所有其余的.jpg,.css和.js文件进行测试
$ curl -H "Accept-Encoding: gzip" -I http://localhost/demo.jpg Output: Nginx response headers HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Fri, 11 July 2016 17:19:46 GMT Content-Type: image/jpeg Last-Modified: Fri, 11 Mar 2016 17:38:22 GMT Connection: keep-alive Vary: Accept-Encoding Content-Encoding: gzip
$ curl -H "Accept-Encoding: gzip" -I http://localhost/demo.css Output: Nginx response headers HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Fri, 11 July 2016 17:19:46 GMT Content-Type: text/css Last-Modified: Fri, 11 Mar 2016 17:38:22 GMT Connection: keep-alive Vary: Accept-Encoding Content-Encoding: gzip
$ curl -H "Accept-Encoding: gzip" -I http://localhost/demo.js Output: Nginx response headers HTTP/1.1 200 OK Server: nginx/1.6.3 Date: Fri, 11 July 2016 17:19:46 GMT Content-Type: text/js Last-Modified: Fri, 11 Mar 2016 17:38:22 GMT Connection: keep-alive Vary: Accept-Encoding Content-Encoding: gzip
通过在配置文件中进行简单的更改即可将gzip压缩模块添加到Nginx Web浏览器,我们可以使网站快速加载,这也将有利于带宽消耗,并使访问者可以在浏览器中获得有限的带宽。网站更快。