Apache网站速度优化实操指南
一 核心网络与连接优化
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
LoadModule http2_module modules/mod_http2.soSSLEngine on 后添加 Protocols h2 http/1.1<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
DeflateCompressionLevel 6
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|pdf)$ no-gzip dont-vary
</IfModule>
二 缓存策略分层落地
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType image/png "access plus 1 year"
ExpiresByType image/gif "access plus 1 year"
ExpiresByType text/html "access plus 1 hour"
</IfModule>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheDirLevels 2
CacheDirLength 1
CacheDefaultExpire 3600
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
</IfModule>
</IfModule>
三 MPM并发模型选择与调优
<IfModule mpm_prefork_module>
StartServers 5
MinSpareServers 5
MaxSpareServers 10
ServerLimit 256
MaxRequestWorkers 256
MaxConnectionsPerChild 250
</IfModule>
四 静态资源与内容分发优化
<Directory "/var/www/html">
Options FollowSymLinks
AllowOverride None
Require all granted
</Directory>
五 验证与持续监控