CentOS 上 Apache2 的 SEO 优化策略
一 基础与 HTTPS
- 启用关键模块:确保加载 mod_ssl、mod_headers、mod_deflate、mod_expires、mod_rewrite、mod_http2(如可用)。在 /etc/httpd/conf/httpd.conf 或 /etc/httpd/conf.modules.d/*.conf 中取消注释或新增相应 LoadModule 行。
- 全站 HTTPS 与 HTTP/2:使用 Let’s Encrypt 获取免费证书并自动配置 Apache。
- 安装与签发:sudo yum install certbot python2-certbot-apache(或 python3-certbot-apache);sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
- 启用 HTTP/2:在 443 虚拟主机中设置:Listen 443 http2;并在 VirtualHost 中加入:Protocols h2 http/1.1
- 强制跳转:在 :80 VirtualHost 中统一 301 到目标 https://(见下文重写规则)。
- 说明:Debian/Ubuntu 使用 a2enmod,路径与命令不同;本文以 CentOS/RHEL 为主。
二 性能与速度优化
- Gzip 压缩(mod_deflate):压缩文本、样式、脚本等以降低传输体积;对 JPEG/PNG/PDF/压缩包 等不再压缩以避免浪费 CPU。
- 示例:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json application/xml
SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png|pdf|zip|gz|bz2)$ no-gzip dont-vary
DeflateCompressionLevel 6(1–9,默认通常为 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 与站点结构优化
- 启用重写(mod_rewrite):构建简洁、可读的 URL,并统一入口(如前端控制器)。
- 通用前端控制器示例:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
- 规范化域名与协议:将 非 www → www(或反之) 与 HTTP → HTTPS 使用 301 永久重定向 集中权重与链接资产。
- 示例(放在 :80 VirtualHost 中):
HTTP → HTTPS
RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
non-www → www(按需二选一)
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
- 建议:URL 保持简短、包含关键词、使用连字符分隔;避免 sessionID、无意义参数。
- 规范链接与站点地图:在页面使用 统一收录入口;提供 XML Sitemap 并在 robots.txt 中声明:Sitemap: https://yourdomain.com/sitemap.xml。
四 安全与可抓取性头信息
- 安全响应头:提升安全与可信度(有助于搜索与用户体验)。
- 示例:
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;建议结合监控告警与定期巡检,持续优化抓取与性能。