以下是优化Apache2配置以提升网站内容性能的关键措施:
mod_deflate
,对文本类文件(HTML/CSS/JS)进行Gzip压缩,减少传输体积。sudo a2enmod deflate
sudo systemctl restart apache2
mod_cache
+mod_cache_disk
缓存静态内容(如图片、CSS),设置缓存目录及过期时间。sudo a2enmod cache cache_disk
sudo systemctl restart apache2
mod_expires
设置静态资源过期时间(如图片缓存1年),减少重复请求。<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpeg "access plus 1 year"
ExpiresByType text/css "access plus 30 days"
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 2
event
或worker
模式(适合高并发),调整进程/线程参数。<IfModule mpm_event_module>
StartServers 4
MinSpareThreads 25
MaxRequestWorkers 150
</IfModule>
autoindex
、status
),减少内存占用。sudo a2dismod autoindex status
sudo systemctl restart apache2
sudo a2enmod ssl http2
sudo systemctl restart apache2
mod_rewrite
优化URL结构,移除冗余参数,提升搜索引擎友好度。<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^old-page$ /new-page [R=301,L]
</IfModule>
mod_status
查看服务器负载,定位性能瓶颈。<Location "/server-status">
SetHandler server-status
Require local
</Location>
注:修改配置后需重启Apache服务生效,建议先在测试环境验证配置正确性。