开元旗牌

锐安盾-网站安全加速服务

中国站

联系我们

400-002-9968

博客 > 如何开启gzip压缩

如何开启gzip压缩

  • 标签:
  • plesk
  • gzip
  • nginx
  • apache

浏览量:8903次评论:0次

作者:Christina时间:2018-09-06 04:55:00

下面主要介绍如何开启gzip压缩,分为两种情况:一个是如何在Apache中开启GZIP压缩,适用于Plesk for LinuxPlesk for Windows两个版本;一个是如何在nginx上开启GZIP压缩,这只适用于Plesk for Linux版本。

如何在Apache中开启GZIP压缩?
适用于Plesk for LinuxPlesk for Windows

具体步骤如下:
1. 首先务必在apache中开启mod_deflate模块:

    # httpd -M | grep deflate
    deflate_module (shared)

2. 在 /etc/httpd/conf.d中创建一个deflate.conf文件:

    # vi /etc/httpd/conf.d/deflate.conf

3. 将下面内容添加到文件中:

# Compress HTML, CSS, JavaScript, Text, XML and fonts AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
AddOutputFilterByType DEFLATE application/x-font
AddOutputFilterByType DEFLATE application/x-font-opentype
AddOutputFilterByType DEFLATE application/x-font-otf
AddOutputFilterByType DEFLATE application/x-font-truetype
AddOutputFilterByType DEFLATE application/x-font-ttf
AddOutputFilterByType DEFLATE application/x-javascript
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE font/opentype
AddOutputFilterByType DEFLATE font/otf
AddOutputFilterByType DEFLATE font/ttf
AddOutputFilterByType DEFLATE image/svg+xml
AddOutputFilterByType DEFLATE image/x-icon
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/javascript
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/xml
# Remove browser bugs (only needed for really old browsers)
BrowserMatch ^Mozilla/4 gzip-only-text/html BrowserMatch ^Mozilla/4.0[678] no-gzip BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent


4. 如果服务器有Apache 2.4 (httpd-2.4),还需开启下面的模块:

    # grep -ir "mod_filter.so" /etc/httpd/conf.modules.d/00-base.conf
    LoadModule filter_module modules/mod_filter.so

5. 重启apache:

    # systemctl restart httpd

注意:在Debian和Ubuntu中,该服务名为apache2而不是httpd,调用所有模块使用的是apache2ctl工具,且deflate.conf文件的路径是/etc/apache2/mods-available/ 而不是 /etc/httpd/conf.d/


如何在nginx上开启gzip压缩?
只适用于Plesk for Linux版本

ngx_http_gzip_module 模块是一个过滤器,可以使用gzip方式压缩响应。通常能够帮助减少传输数据大小达一半甚至一半以上。
那么,如何为某个域名或在Plesk服务器上全局开启nginx gzip压缩呢?具体方法步骤如下。

在Plesk web界面中为单个订阅开启该服务
1. 登录Plesk

2. 转入 域名 > example.com > Apache 与 nginx 设置 > 其它nginx 指令

3. 为nginx设置下面的指令:
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp image/svg+xml;
gzip_vary on;

注意:这只是一个示例,还可以在gzip_types部分添加其它文件类型,例如application/javascript、application/js等等。我们可以在服务器上直接进入/etc/nginx/mime.types 文件中查看所有可用的文件类型。

在Plesk界面中全局开启该服务
全局使用SSH连接
1. 使用SSH连接服务器。

2. 使用下面的命令创建一个文本文件:

    # touch /etc/nginx/conf.d/gzip.conf

3. 在任何文本编辑器中打开文件,例如使用vi:

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

4. 在文件中插入下面的内容:
gzip on;
gzip_disable "MSIE [1-6]\.(?!.*SV1)";
gzip_proxied any;
gzip_types text/plain text/css application/javascript application/x-javascript text/xml application/xml application/xml+rss text/javascript image/x-icon image/bmp image/svg+xml;
gzip_vary on;

注意:这只是一个示例,还可以在gzip_types 部分添加其它文件类型,例如application/javascript、application/js等等。我们可以在服务器上直接进入/etc/nginx/mime.types文件查看所有可用的文件类型。

5. 测试配置,若有错误请修正:

    # nginx -t

6. 重新加载 nginx 配置:

    # service nginx reload

我的评论

还未登录?

微信扫码沟通
微信扫码沟通

微信扫码沟通

售前支持
代理合作
售后支持
return head