温馨提示×

Ubuntu Apache2如何优化SEO

小樊
43
2025-12-24 20:55:17
栏目: 智能运维

Ubuntu Apache2 优化 SEO 的实操清单

一 基础配置与环境准备

  • 启用关键模块:URL 重写、压缩、缓存、头信息,直接作用于站点性能与抓取效率。
    • 命令:sudo a2enmod rewrite deflate expires headers cache cache_disk
    • 检查:apache2ctl -M | grep -E “rewrite|deflate|expires|headers|cache|cache_disk”
    • 重启:sudo systemctl restart apache2
  • 站点与目录基础
    • 确保虚拟主机配置正确并启用:sudo a2ensite your_site.conf;DocumentRoot 指向实际文件目录。
    • 允许 .htaccess 覆盖:在 中设置 AllowOverride All,并确认主配置包含 IncludeOptional sites-enabled/*.conf
    • 重启生效:sudo systemctl reload apache2。

二 URL 结构与规范化

  • 启用重写并配置语义化规则(示例:前端路由 SPA 或伪静态)
    • 启用模块:sudo a2enmod rewrite
    • 虚拟主机 Directory 段加入:AllowOverride All
    • 在站点根目录 .htaccess:
      RewriteEngine On
      RewriteBase /
      # 前端路由:所有非文件/目录请求指向 index.html
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteRule . /index.html [L]
      # 旧页 301 到新页
      RewriteRule ^old-page\.html$ /new-page.html [R=301,L]
      
  • 规范化与去重复
    • 统一 www/非 wwwHTTP/HTTPS:在虚拟主机 80 段做 301 到 443,或用 RedirectPermanent 统一域名。
    • 避免重复内容:为列表分页、参数排序等设置 rel=“canonical”;在 robots.txt 声明 Sitemap
    • 站点地图与 robots:根目录放置 sitemap.xml,robots.txt 加入:Sitemap: https://yourdomain.com/sitemap.xml。

三 性能优化 压缩与缓存

  • 启用 Gzip 压缩(mod_deflate)
    • 建议配置(放在 内,或对应虚拟主机/Directory):
      AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/x-javascript
      AddOutputFilterByType DEFLATE image/svg+xml application/json
      # 可选:字体
      AddOutputFilterByType DEFLATE application/font-woff application/font-woff2 application/vnd.ms-fontobject application/x-font-ttf
      # 兼容旧浏览器
      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|bz2|rar|pdf|mov|avi|mp3|mp4|webp)$ no-gzip dont-vary
      Header append Vary User-Agent env=!dont-vary
      
    • 验证:curl -I -H “Accept-Encoding: gzip” https://yourdomain.com/ | grep -i “content-encoding”
  • 客户端缓存(mod_expires)
    • 建议配置(按资源类型区分长短期缓存):
      <IfModule mod_expires.c>
        ExpiresActive On
        ExpiresByType text/html "access plus 1 week"
        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/webp "access plus 1 year"
        ExpiresByType application/font-woff2 "access plus 1 year"
        ExpiresByType application/font-woff "access plus 1 year"
        ExpiresByType application/vnd.ms-fontobject "access plus 1 year"
        ExpiresByType application/x-font-ttf "access plus 1 year"
      </IfModule>
      
  • 反向代理/网关缓存(可选,mod_cache_disk)
    • 示例(在 内):
      <IfModule mod_cache_disk.c>
        CacheRoot "/var/cache/apache2/mod_cache_disk"
        CacheEnable disk /
        CacheDirLevels 2
        CacheDirLength 1
        CacheIgnoreHeaders Set-Cookie
        CacheIgnoreNoLastMod On
        CacheDefaultExpire 3600
      </IfModule>
      
  • 进一步提速建议
    • 启用 Brotli(若安装对应模块,优先级高于 Gzip)。
    • 使用 CDN、合并/压缩静态资源、图片转 WebP/AVIF、懒加载与关键 CSS 内联。

四 安全与可抓取性

  • 全站 HTTPS
    • 使用 Certbot 获取并自动配置证书:sudo certbot --apache -d yourdomain.com -d www.yourdomain.com
    • 强制 HTTPS:在 80 虚拟主机中使用 RedirectPermanent 到 https://;或在 443 虚拟主机中设置 Strict-Transport-Security
  • 安全头与隐私
    • 推荐在虚拟主机或全局配置加入:
      Header always set X-Frame-Options "DENY"
      Header always set X-Content-Type-Options "nosniff"
      Header always set X-XSS-Protection "1; mode=block"
      Header always set Referrer-Policy "strict-origin-when-cross-origin"
      # 可选:仅 HTTPS 时启用
      Header always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
      
  • 日志与监控
    • 分析访问与性能:使用 GoAccess 实时分析 access.log;结合监控(如 Prometheus/Grafana)观察响应时延与错误率。

五 验证与上线检查

  • 配置语法与生效
    • 语法检查:sudo apache2ctl configtest
    • 平滑重载:sudo systemctl reload apache2
  • SEO 与性能核对清单
    • 站点地图与 robots:访问 /robots.txt/sitemap.xml 返回 200;Sitemap 提交到搜索引擎。
    • HTTPS 与跳转:访问 http:// 自动 301 到 https://;证书有效且链完整。
    • 压缩生效:响应头含 Content-Encoding: gzip(或 br)。
    • 缓存策略:静态资源响应头含 Cache-ControlExpires,过期时间符合预期。
    • URL 规范化:旧 URL 301 到新 URL;分页/参数页有 canonical
    • 可抓取性:robots.txt 不屏蔽关键目录;返回 200/404 状态码符合预期;无大量重复内容。

0