首页主机资讯Apache配置中如何优化页面加载速度

Apache配置中如何优化页面加载速度

时间2025-11-27 12:26:04发布访客分类主机资讯浏览428
导读:Apache配置优化页面加载速度 一 核心优化清单 启用压缩:使用 mod_deflate 对文本资源进行 Gzip 压缩,常用类型包含 text/html、text/css、application/javascript、applicat...

Apache配置优化页面加载速度

一 核心优化清单

  • 启用压缩:使用 mod_deflate 对文本资源进行 Gzip 压缩,常用类型包含 text/html、text/css、application/javascript、application/json;压缩级别建议设为 6(在压缩率与CPU之间折中)。若环境支持,可进一步启用 Brotli(mod_brotli) 获取更小体积。验证方式:响应头出现 Content-Encoding: gzip/br
  • 浏览器缓存:使用 mod_expires 设置强缓存,典型策略为:
    • 静态资源(如 image/jpeg、image/png、image/gif、image/x-icon)设置 1 年
    • text/css、application/javascript 设置 1 个月
    • text/html 设置较短(如 1 小时),便于内容更新。
  • 长连接复用:开启 KeepAlive,建议 KeepAlive On、MaxKeepAliveRequests 100、KeepAliveTimeout 5,降低 TCP 握手与慢启动开销。
  • 并发与进程模型:优先使用 MPM event,并按内存与并发目标调参(如 StartServers、MinSpareThreads、MaxSpareThreads、ThreadsPerChild、MaxRequestWorkers)。
  • 协议与加密:启用 HTTP/2(多路复用、头部压缩),并开启 TLS;结合 OCSP StaplingSSLSessionCache 降低握手延迟。
  • 静态资源加速:将图片、字体、视频等托管到 CDN,减少跨地域往返与源站压力。
  • 日志与开销:非必要时将 LogLevel 调整为 warn,减少磁盘 I/O 与写放大。

二 关键配置示例

  • Gzip 压缩(mod_deflate)
<
    IfModule mod_deflate.c>
    
    DeflateCompressionLevel 6
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
    AddOutputFilterByType DEFLATE application/javascript application/json application/xml
    # 对已经压缩的二进制资源不重复压缩
    SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip|gz|bz2)$ no-gzip dont-vary
<
    /IfModule>
    
  • 浏览器缓存(mod_expires)
<
    IfModule mod_expires.c>
    
    ExpiresActive On
    ExpiresByType image/jpg  "access plus 1 year"
    ExpiresByType image/jpeg "access plus 1 year"
    ExpiresByType image/png  "access plus 1 year"
    ExpiresByType image/gif  "access plus 1 year"
    ExpiresByType image/x-icon "access plus 1 year"
    ExpiresByType text/css "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
    ExpiresByType text/html "access plus 1 hour"
<
    /IfModule>
    
  • KeepAlive 长连接
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
  • HTTP/2 与 TLS/OCSP Stapling(需模块支持)
# 启用 HTTP/2(示例)
LoadModule http2_module modules/mod_http2.so
Protocols h2 http/1.1

# TLS 与 OCSP Stapling
SSLEngine on
SSLCertificateFile      /path/to/cert.crt
SSLCertificateKeyFile   /path/to/privkey.key
SSLCertificateChainFile /path/to/chain.pem

SSLOpenSSLConfCmd DHParameters "/etc/ssl/certs/dhparam.pem"
SSLUseStapling On
SSLStaplingCache "shmcb:/var/run/stapling-cache(150000)"
SSLSessionCache "shmcb:/var/run/ssl_scache(512000)"
SSLSessionCacheTimeout 300
  • 服务器端缓存(mod_cache_disk,可选)
<
    IfModule mod_cache.c>
    
  <
    IfModule mod_cache_disk.c>
    
    CacheRoot "/var/cache/apache2/mod_cache_disk"
    CacheEnable disk /
    CacheDirLevels 2
    CacheDirLength 1
    CacheDefaultExpire 3600
    CacheIgnoreHeaders Set-Cookie
  <
    /IfModule>
    
<
    /IfModule>
    

以上示例涵盖压缩、缓存、长连接、HTTP/2 与服务器端缓存的常见生产配置,可按站点类型与流量特征微调参数与路径。

三 按系统环境的要点

  • Debian/Ubuntu
    • 启用模块:a2enmod deflate expires headers http2 ssl cache cache_disk
    • MPM 调优:编辑 /etc/apache2/mods-enabled/mpm_event.conf,按内存与并发目标设置 StartServers、MinSpareThreads、MaxSpareThreads、ThreadsPerChild、MaxRequestWorkers
    • 重启:systemctl restart apache2
  • CentOS/RHEL
    • 启用模块:在 /etc/httpd/conf/httpd.conf 或相应目录确保加载 mod_deflate、mod_expires、mod_headers、mod_ssl、mod_http2
    • MPM 调优:编辑 /etc/httpd/conf.modules.d/00-mpm.conf(或对应 MPM 配置),合理设置 MaxRequestWorkers 等;
    • 重启:systemctl restart httpd

四 验证与上线流程

  • 配置校验:执行 apachectl configtest,确保语法无误;变更使用 graceful 或平滑重启减少中断。
  • 压缩验证:浏览器开发者工具或命令行查看响应头 Content-Encoding: gzip/br;也可用 curl -I -H “Accept-Encoding: gzip, br”
  • 性能压测:使用 ab(如:ab -n 1000 -c 100 https://yoursite/)、WebPageTestLighthouse 对比 TTFB、FCP、LCP、RPS 等指标。
  • 缓存验证:检查 Cache-Control、Expires 头;服务器端缓存查看 mod_cache 命中与日志。

五 进阶与注意事项

  • 压缩权衡:压缩级别 1–9,级别越高 CPU 越高;文本类资源收益明显,已压缩的 图片/视频/PDF 建议跳过压缩。
  • 缓存失效:对频繁变更资源使用 文件名哈希 或短 max-age,避免强缓存导致更新滞后。
  • 协议选择:优先 HTTP/2(多路复用、头部压缩);如启用 HTTP/2 Server Push,谨慎推送,避免浪费带宽。
  • 前端协同:配合 preload/prefetch、懒加载、关键 CSS 内联、JS defer 等手段,进一步缩短首屏时间。
  • 安全与合规:TLS 配置遵循安全基线,启用 HSTS、OCSP Stapling,并定期轮换密钥与证书。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Apache配置中如何优化页面加载速度
本文地址: https://pptw.com/jishu/757768.html
Ubuntu上PyTorch的模型保存与加载 Ubuntu Oracle并发控制机制

游客 回复需填写必要信息