ubuntu

Ubuntu Apache如何优化图片加载速度

小樊
36
2025-08-08 00:04:02
栏目: 智能运维

在Ubuntu上优化Apache服务器的图片加载速度,可从以下方面入手:

  1. 启用压缩:使用mod_deflate模块压缩图片,减少传输体积。

    sudo a2enmod deflate
    

    在配置文件中添加:

    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE image/jpeg image/png image/gif
    </IfModule>
    

    重启Apache:sudo systemctl restart apache2

  2. 设置缓存策略:通过mod_expires模块延长图片缓存时间。

    sudo a2enmod 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"
    </IfModule>
    

    重启Apache。

  3. 启用HTTP/2:提升传输效率(需服务器支持)。

    sudo a2enmod http2
    

    配置文件中添加:

    Protocols h2 http/1.1
    

    重启Apache。

  4. 优化图片本身:使用工具(如imagemin)压缩图片大小,或转换为更高效的格式(如WebP)。

  5. 使用CDN:将图片托管到CDN,利用全球节点加速访问。

  6. 启用KeepAlive:减少TCP连接开销。
    编辑配置文件:

    KeepAlive On
    MaxKeepAliveRequests 100
    KeepAliveTimeout 5
    

    重启Apache。

0
看了该问题的人还看了