在 Debian 上给 Apache 开启压缩(通常是 gzip / deflate),一般步骤如下:
Apache 在 Debian 下通常用 a2enmod 管理模块。
开启压缩相关模块:
sudo a2enmod deflate
sudo a2enmod headers # 可选,但推荐
编辑 Apache 配置(推荐新建一个配置文件,方便管理):
sudo nano /etc/apache2/conf-available/deflate.conf
内容示例(常用配置):
<IfModule mod_deflate.c>
# 压缩文本类内容
AddOutputFilterByType DEFLATE text/html text/plain text/xml
AddOutputFilterByType DEFLATE text/css text/javascript application/javascript
AddOutputFilterByType DEFLATE application/json application/xml
# 浏览器兼容性
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
# 不压缩已压缩文件
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|zip|gz|tgz|rar|bz2|mp3|mp4|avi|mov)$ no-gzip dont-vary
</IfModule>
启用配置:
sudo a2enconf deflate
sudo systemctl restart apache2
curl -I -H "Accept-Encoding: gzip" http://your-domain.com/
看到:
Content-Encoding: gzip
说明成功。
打开开发者工具 → Network → 查看响应头是否有:
Content-Encoding: gzip
在 /etc/apache2/sites-available/xxx.conf 的 <VirtualHost> 里加:
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/css application/javascript
</IfModule>
确保 PHP 没有自己输出压缩(如 zlib.output_compression),否则会冲突。
如果你愿意,可以告诉我:
我可以给你更精准的配置。