在Ubuntu上優化Apache服務器的圖片加載速度,可從以下方面入手:
啟用壓縮:使用mod_deflate
模塊壓縮圖片,減少傳輸體積。
sudo a2enmod deflate
在配置文件中添加:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE image/jpeg image/png image/gif
</IfModule>
重啟Apache:sudo systemctl restart apache2
。
設置緩存策略:通過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。
啟用HTTP/2:提升傳輸效率(需服務器支持)。
sudo a2enmod http2
配置文件中添加:
Protocols h2 http/1.1
重啟Apache。
優化圖片本身:使用工具(如imagemin
)壓縮圖片大小,或轉換為更高效的格式(如WebP)。
使用CDN:將圖片托管到CDN,利用全球節點加速訪問。
啟用KeepAlive:減少TCP連接開銷。
編輯配置文件:
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
重啟Apache。