linux

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

小樊
37
2025-11-27 02:23:51
栏目: 编程语言

Apache配置优化页面加载速度

一 核心优化清单

二 关键配置示例

<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>
<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 On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
# 启用 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
<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 与服务器端缓存的常见生产配置,可按站点类型与流量特征微调参数与路径。

三 按系统环境的要点

四 验证与上线流程

五 进阶与注意事项

0
看了该问题的人还看了