CentOS Apache2 的 SEO 优化实操清单
一 基础配置与环境准备
- 启用关键模块:在 CentOS/RHEL 上,Apache 配置目录通常为 /etc/httpd/conf/httpd.conf。确保启用 mod_ssl、mod_headers、mod_deflate、mod_expires、mod_rewrite、mod_http2(如可用)。示例(取消注释或新增):
- LoadModule ssl_module modules/mod_ssl.so
- LoadModule headers_module modules/mod_headers.so
- LoadModule deflate_module modules/mod_deflate.so
- LoadModule expires_module modules/mod_expires.so
- LoadModule rewrite_module modules/mod_rewrite.so
- LoadModule http2_module modules/mod_http2.so
- 启用 HTTPS/TLS:使用 Let’s Encrypt 获取免费证书并自动配置 Apache:
- 安装 certbot:sudo yum install certbot python2-certbot-apache
- 获取并安装证书:sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
- 启用 HTTP/2:在 443 虚拟主机中设置协议:
- Listen 443 http2
- 在 VirtualHost 443 中加入:Protocols h2 http/1.1
- 重启生效:sudo systemctl restart httpd
- 说明:Debian/Ubuntu 使用 a2enmod 的方式启用模块,路径与命令不同;本文以 CentOS 为主。
二 性能与速度优化
- 启用 Gzip 压缩(mod_deflate):压缩文本、样式、脚本等可显著降低传输体积;对已经压缩的 JPEG/PNG/PDF 等不必再压缩,避免浪费 CPU。
-
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json application/xml
# 可选:字体
AddOutputFilterByType DEFLATE application/font-woff application/font-woff2 application/vnd.ms-fontobject application/x-font-ttf
# 避免对图片/压缩包等再压缩
SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|pdf|zip|gz|bz2)$ no-gzip dont-vary
# 可选:压缩级别 1-9,默认通常为 6
DeflateCompressionLevel 6
- 配置 浏览器缓存(mod_expires):为静态资源设置长期缓存,提升回访速度并减少重复抓取。
-
ExpiresActive On
ExpiresByType text/css "access plus 1 year"
ExpiresByType application/javascript "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/svg+xml "access plus 1 year"
ExpiresByType font/woff2 "access plus 1 year"
ExpiresByType text/html "access plus 1 hour"
- 启用 HTTP/2:多路复用与头部压缩可改善首包与并发性能,建议与 HTTPS 同时启用。
- 可选 页面级缓存(mod_cache/mod_cache_disk):对可缓存的页面或静态资源启用磁盘缓存,减轻后端压力(按需启用)。
-
CacheEnable disk /
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheDirLevels 2
CacheDirLength 1
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
CacheDefaultExpire 3600
- 建议:静态资源使用 Cache-Control: public, max-age 与 ETag/Last-Modified 组合;动态内容谨慎设置长缓存。
三 URL 与站点结构优化
- 启用 URL 重写(mod_rewrite):构建简洁、可读的 URL,并统一入口(如前端控制器)。
- 通用前端控制器示例:
- RewriteEngine On
- RewriteCond %{REQUEST_FILENAME} !-f
- RewriteCond %{REQUEST_FILENAME} !-d
- RewriteRule ^(.*)$ /index.php [L]
- 规范化域名与协议:将 非 www 跳转 www(或反之),并使用 HTTPS 301 永久重定向,集中权重与链接资产。
- 将 example.com → www.example.com(放在 :80 VirtualHost 中):
- RewriteEngine On
- RewriteCond %{HTTP_HOST} !^www. [NC]
- RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
- 将 HTTP → HTTPS(放在 :80 VirtualHost 中):
- RewriteEngine On
- RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
- 建议:URL 保持简短、包含关键词、使用连字符分隔;避免 sessionID、无意义参数。
四 安全与可抓取性头信息
- 设置安全响应头,提升安全与可信度(有助于搜索与用户体验):
-
Header set X-Content-Type-Options "nosniff"
Header set X-Frame-Options "SAMEORIGIN"
Header set X-XSS-Protection "1; mode=block"
# 可选:内容安全策略(CSP),按站点实际策略调整
Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' 'unsafe-eval'; style-src 'self' 'unsafe-inline'; img-src 'self' data:; font-src 'self' data:"
# 可选:Referrer-Policy
Header set Referrer-Policy "strict-origin-when-cross-origin"
- 避免 软 404 与链断裂:为不存在资源返回 404 页面,定期清理无效链接;对移动端、可访问性(如图片 alt)与结构化数据(Schema.org)做持续优化。
五 验证与监控
- 配置检查与重载:
- 语法检查:sudo httpd -t
- 热重载:sudo systemctl reload httpd(或 restart)
- 验证要点:
- HTTPS/HTTP2:浏览器开发者工具 → Network,查看 Protocol 是否为 h2,响应是否为 200/301/302 等正确状态码。
- Gzip:响应头 Content-Encoding: gzip;可用 curl -I 或 GTmetrix/WebPageTest 检测。
- 缓存:响应头出现 Cache-Control: max-age=、Expires、ETag/Last-Modified 等。
- 重定向:访问 http:// 与 non-www 应 301 到目标 https://www。
- 日志与监控:
- 实时查看:tail -f /var/log/httpd/access_log /var/log/httpd/error_log
- 建议结合 监控告警 与 定期巡检,持续优化抓取与性能。