如何在Apache2中设置Gzip
Gzip是一种用于压缩数据的文件格式,可以有效地减少传输数据的大小,从而提高网站的加载速度,在Apache2中设置Gzip,需要进行以下几个步骤:
1、安装Gzip模块
在Debian/Ubuntu系统中,使用以下命令安装Gzip模块:
sudo apt-get install libapache2-mod-gzip
在CentOS/RHEL系统中,使用以下命令安装Gzip模块:
sudo yum install httpd-mod-gzip
2、启用Gzip模块
在Apache2的配置文件(通常位于/etc/apache2/apache2.conf
或/etc/httpd/httpd.conf
)中,找到以下行:
LoadModule accel_exp "libexec/apache2/mod_accelerator.so" LoadModule filter_module "libexec/apache2/mod_filter.so" LoadModule rewrite_module "libexec/apache2/mod_rewrite.so" LoadModule vhost_alias_module "libexec/apache2/mod_vhost_alias.so" LoadModule vhost_ssl_module "libexec/apache2/mod_vhost_ssl.so" LoadModule proxy_module "libexec/apache2/mod_proxy.so" LoadModule proxy_uwsgi_module "libexec/apache2/mod_proxy_uwsgi.so" LoadModule uwsgi_module "libexec/apache2/mod_uwsgi.so" LoadModule scgi_module "libexec/apache2/mod_scgi.so" LoadModule fastcgi_module "libexec/apache2/mod_fastcgi.so" LoadModule action_module "libexec/apache2/mod_action.so" LoadModule storage_sync_module "libexec/apache2/mod_storage_sync.so" LoadModule aliases_module "libexec/apache2/mod_aliases.so" LoadModule authz_host_module "libexec/apache2/mod_authz_host.so" LoadModule status_module "libexec/apache2/mod_status.so" LoadModule request_module "libexec/apache2/mod_request.so" LoadModule log_config_module "libexec/apache2/mod_log_config.so" LoadModule logio_module "libexec/apache2/mod_logio.so" LoadModule include_module "libexec/apache2/mod_include.so" LoadModule envvars_module "libexec/apache2/mod_envvars.so" LoadModule setenvif_module "libexec/apache2/mod_setenvif.so" LoadModule headers_module "libexec/apache2/mod_headers.so" LoadModule deflate_module "libexec/apache2/mod_deflate.so"
在这些行下面添加一行,启用Gzip模块:
SetEnvIfNoCase Request_URI .(?i:gz|gzip)$ no-gzip dont-vary
3、配置Gzip压缩级别和缓存策略
打开Apache2的配置文件(通常位于/etc/apache2/apache2.conf
或/etc/httpd/httpd.conf
),在文件末尾添加以下内容:
<IfModule mod_deflate.c> AddOutputFilterByType DEFLATE text/plain text/* application/javascript application/x-javascript text/xml application/* xml application/*+xml text/* html;q=0.9 image/*;q=0.8 image/*;q=0.5 text/*;q=0.5 video/*;q=0.5 audio/*;q=0.4 font/*;q=0.4 style/*;q=0.4 image/*;q=0.3 connection /*;q=0.1 default type text/html image/png imagejpeg imagegif imagesvg application/json application/*+json text/*+json application/*+xml application/*+xml text/*+xml application/*+x-javascript;q=0.9 text/*+x-javascript;q=0.8 text/*+xml;q=0.8 text/*+xml;q=0.7 application/*+xml;q=0.6 application/*+xml;q=0.5 x-javascript*; q=0.5 x-javascript*/\*;q=0\.5 image/*;\* q=0\.5 image/*/\*;q=0\.5 video/*;\* q=0\.5 video/*/*;q=0\.5 audio/*;\* q=0.5 audio/\*;q=0\.5 css */p a { ADD YOUR LOGIC HERE } </IfModule> <!-End of module deflate --> <IfModule mod_headers.c> Header set Accept-Encoding "gzip, deflate" </IfModule> <IfModule mod_headers.c> Header always set Content-Encoding gzip </IfModule> <IfModule mod_headers.c> Header set Vary Accept-Encoding </IfModule> <IfModule mod_mime.c> AddOutputFilterByType TEXT/HTML text/plain text/xml application/xhtml+xml application/xml xml text/css application/x-javascript;q=0.9 image/webp image/apng image/* image/*;q=0.9 video/* video/*;q=0.9 audio/* audio/*;q=0.9 application/* application/*+xml application/*+json application/*+x-javascript;q=0.9 text/* text/*+xml text/*+x-javascript;q=0.9 image/* image/*+x-javascript;q=0.9 video/* video/*+x-javascript;q=0.9 audio/* audio/*+x-javascript;q=0.9 css */p a { ADD YOUR LOGIC HERE } </IfModule> <IfModule mod_cached.c> CacheEnable disk smaxage=3600 </IfModule> <IfModule mod_cached.c> CacheDisable diskEviction </IfModule> <IfModule mod_cached.c> CacheSetDefaultUseMMap off </IfModule> <IfModule mod_cached.c> CacheSetMaxSize 1g MAXIMUM CACHE SIZE FOR ALL THE THIRD PARTY PROVIDERS (IN MB) </IfModule> <IfModule mod_cached.c> CacheSetMinSize 1m MINIMUM CACHE SIZE FOR ALL THE THIRD PARTY PROVIDERS (IN MB) </IfModule> <IfModule mod_cached.c> CacheSetSMaxAge 3600 MAXIMUM TIME BEFORE A CACHE IS STALE (IN SEC) </IfModule> <IfModule mod_cached.c> CacheSetSMaxAgeExpires 3600 MAXIMUM TIME BEFORE A CACHE IS STALE (IN SEC) AND IT IS EVICTED FROM THE DISK CACHE (IN SEC) </IfModule> <IfModule mod_cached.c> CacheSetDisableMMAP off DISABLE MMAP TO PREVENT SLOWING OF THE CACHED PAGES WHEN USING A HIGH RESOLUTION DISPLAY OR IF YOU WANT TO ENABLE IT IN THE LIVE MODE (DISABLED BY DEFAULT) </IfModule> <IfModule mod_cached.c> CacheSetVaryOnCookie off DISABLE VARY ON COOKIE (ENABLED BY DEFAULT) </IfModule> <IfModule mod_headers.c> Header add Vary User-Agent accept-encoding </IfModule> <If module="deflate"> <IfModule deflate.c> <IfModule deflatejs mimetype="text/javascript"> <If module="sethandlerbyextension"> SetHandler byExtension deflatejs </If> </IfModul> </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </If > </EndOfFileModSectioN> "
原创文章,作者:K-seo,如若转载,请注明出处:https://www.kdun.cn/ask/211709.html