ubuntu

如何通过Apache配置优化图片加载速度

小樊
39
2025-11-21 09:05:59
栏目: 编程语言

Apache配置优化图片加载速度

一 核心配置清单

二 可直接使用的配置片段

<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/x-javascript
    AddOutputFilterByType DEFLATE image/webp
    # 可选:压缩级别 1-9,9 最高(更耗 CPU)
    DeflateCompressionLevel 6
</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/webp "access plus 1 year"
    ExpiresByType text/css  "access plus 1 month"
    ExpiresByType application/javascript "access plus 1 month"
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch "\.(jpg|jpeg|png|gif|webp)$">
        Header set Cache-Control "public, max-age=31536000, immutable"
    </FilesMatch>
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5

# 在支持的 Apache 2.4.17+ 上启用 HTTP/2(确保已安装 mod_http2)
Protocols h2 http/1.1
<IfModule mod_cache.c>
  <IfModule mod_cache_disk.c>
    CacheRoot   /var/cache/apache2/mod_cache_disk
    CacheEnable  disk  /
    CacheDirLevels 2
    CacheDirLength 1
    CacheIgnoreHeaders Set-Cookie
    CacheIgnoreNoLastMod On
    CacheMaxExpire 86400
    CacheDefaultExpire 3600
  </IfModule>
</IfModule>
FileETag none

以上片段可按需合并到**/etc/apache2/apache2.conf**、站点配置或相应的mods-enabled配置中。

三 系统与平台要点

四 验证与注意事项

0
看了该问题的人还看了