CentOS Apache配置提升网站排名的实操清单
一 核心原则与优先级
二 快速落地步骤
三 性能与缓存配置示例
<IfModule mod_deflate.c>
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css application/javascript application/json
DeflateCompressionLevel 5
BrowserMatch ^Mozilla/4 gzip-only-text/html
BrowserMatch ^Mozilla/4\.0[678] no-gzip
BrowserMatch bMSIE !no-gzip !gzip-only-text/html
Header append Vary User-Agent
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType text/html "access plus 1 hour"
ExpiresByType text/css "access plus 1 month"
ExpiresByType application/javascript "access plus 1 month"
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 image/x-icon "access plus 1 year"
</IfModule>
<IfModule mod_headers.c>
# HTML不缓存或短缓存,便于内容更新
Header set Cache-Control "max-age=3600, public"
# 静态资源可长期缓存且带指纹/版本
<FilesMatch "\.(css|js|jpg|jpeg|png|gif|svg|ico)$">
Header set Cache-Control "max-age=31536000, public, immutable"
</FilesMatch>
</IfModule>
KeepAlive On
MaxKeepAliveRequests 100
KeepAliveTimeout 5
<IfModule mod_http2.c>
Protocols h2 http/1.1
</IfModule>
<IfModule mod_cache.c>
<IfModule mod_cache_disk.c>
CacheEnable disk /
CacheRoot "/var/cache/apache2/mod_cache_disk"
CacheDirLevels 2
CacheDirLength 1
CacheIgnoreHeaders Set-Cookie
CacheIgnoreNoLastMod On
CacheDefaultExpire 3600
</IfModule>
</IfModule>
以上配置覆盖压缩、缓存、连接与HTTP/2等关键路径,兼顾性能与可维护性。
四 SEO与安全头与URL结构
<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 always set Strict-Transport-Security "max-age=63072000; includeSubDomains; preload"
# 如全站HTTPS且无自托管字体,可启用CSP;有第三方资源需按需放宽
# Header set Content-Security-Policy "default-src 'self'; script-src 'self' 'unsafe-inline' https://cdn.example.com; style-src 'self' 'unsafe-inline' https://cdn.example.com; img-src 'self' data: https:;"
</IfModule>
ServerTokens Prod
ServerSignature Off
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [L]
</IfModule>
上述头信息与URL策略有助于爬虫识别、降低风险并提升抓取效率。
五 验证与上线检查