Apache配置优化网站响应时间的实用清单
一 核心网络与连接优化
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
验证:响应头出现 Connection: keep-alive。Timeout 10
LoadModule http2_module modules/mod_http2.so
# 在 443 虚拟主机中确保 SSLEngine on
说明:HTTP/2 能显著改善多资源页面的首屏与整体加载速度。二 选择并优化MPM并发模型
<IfModule mpm_event_module>
StartServers 2
MinSpareThreads 25
MaxSpareThreads 75
ThreadLimit 64
ThreadsPerChild 25
MaxRequestWorkers 150
MaxConnectionsPerChild 0
</IfModule>
调优要点:
三 内容传输与缓存策略
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript
# 可选:使用 mod_filter 更灵活(2.4+)
# SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip|gz|bz2)$ 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>
CacheRoot /var/cache/apache2/mod_cache_disk
CacheEnable disk /
CacheDirLevels 2
CacheDirLength 1
</IfModule>
</IfModule>
四 启用HTTPS与传输层优化
<IfModule mod_ssl.c>
SSLSessionCache shmcb:/var/run/ssl_scache(512000)
SSLSessionCacheTimeout 300
</IfModule>
SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"
SSLUseStapling On
SSLStaplingCache "shmcb:/var/run/stapling-cache(150000)"
五 监控验证与迭代
httpd -t 或 apache2ctl configtest;灰度/分步重启;变更后持续观察。