温馨提示×

如何在CentOS中提升Apache2的SEO效果

小樊
33
2025-12-27 07:23:36
栏目: 智能运维

CentOS 上提升 Apache2 的 SEO 效果

一 基础与性能优化

  • 启用关键模块:在 /etc/httpd/conf/httpd.conf/etc/httpd/conf.modules.d/ 中确保启用 mod_ssl、mod_headers、mod_deflate、mod_expires、mod_rewrite、mod_http2
  • 启用 HTTPS 与 HTTP/2:使用 Let’s Encrypt 获取证书并自动配置 Apache。
    • 安装 certbot: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
      • Protocols h2 http/1.1
    • 重启生效:sudo systemctl restart httpd
  • 启用 Gzip 压缩(mod_deflate):压缩文本、样式、脚本等体积,避免对 JPEG/PNG/PDF 等已压缩资源再压缩。
    • 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
  • 可选页面级缓存(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
  • 连接与开销优化:
    • KeepAlive On
    • KeepAliveTimeout 15
    • MaxKeepAliveRequests 100
    • HostnameLookups Off
    • 若不需要 .htaccess 控制,在 中设置 AllowOverride None 以减少文件系统检查开销。

二 URL 与站点结构优化

  • 启用 URL 重写(mod_rewrite):构建简洁、可读的 URL,并统一入口(如前端控制器)。
    • RewriteEngine On
    • RewriteCond %{REQUEST_FILENAME} !-f
    • RewriteCond %{REQUEST_FILENAME} !-d
    • RewriteRule ^(.*)$ /index.php [L]
  • 规范化域名与协议:将 HTTP→HTTPS非 www→www(或反之)使用 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 中):
      • RewriteRule ^(.*)$ https://%{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=ExpiresETag/Last-Modified 等。
    • 重定向:访问 http://non-www301 到目标 https://www
  • 日志与监控:
    • 实时查看:tail -f /var/log/httpd/access_log /var/log/httpd/error_log
    • 建议结合监控告警与定期巡检,持续优化抓取与性能。

五 可直接使用的配置片段

  • 80 端口虚拟主机(HTTP→HTTPS 与 www 规范化)
<VirtualHost *:80>
    ServerName yourdomain.com
    ServerAlias www.yourdomain.com

    RewriteEngine On
    # 非 www → www
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R=301,L]
    # HTTP → HTTPS
    RewriteRule ^(.*)$ https://%{HTTP_HOST}$1 [R=301,L]
</VirtualHost>
  • 443 端口虚拟主机(HTTPS + HTTP/2 + 压缩 + 缓存 + 安全头)
<VirtualHost *:443>
    SSLEngine on
    SSLCertificateFile    /etc/letsencrypt/live/yourdomain.com/fullchain.pem
    SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.com/privkey.pem

    Protocols h2 http/1.1

    # Gzip 压缩
    <IfModule mod_deflate.c>
        AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css \
            application/javascript application/json application/xml application/rss+xml \
            image/svg+xml image/x-icon font/woff2 font/woff font/ttf
        SetEnvIfNoCase Request_URI \.(?:gif|jpe?g|png|pdf|zip|gz|bz2)$ no-gzip dont-vary
        DeflateCompressionLevel 6
    </IfModule>

    # 浏览器缓存
    <IfModule mod_expires.c>
        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 font/woff "access plus 1 year"
        ExpiresByType font/ttf "access plus 1 year"
        ExpiresByType text/html "access plus 1 hour"
    </IfModule>

    # 安全与可抓取性头
    <IfModule mod_headers.c>
        Header set X-Content-Type-Options "nosniff"
        Header set X-Frame-Options "SAMEORIGIN"
        Header set X-XSS-Protection "1; mode=block"
        # 可选:按站点策略调整
        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:"
        Header set Referrer-Policy "strict-origin-when-cross-origin"
    </IfModule>

    # 前端控制器(单入口)
    <Directory /var/www/html>
        AllowOverride All
        Require all granted
    </Directory>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /index.php [L]
</VirtualHost>

0