文章标题
小标题
正文内容。
随着 AI 搜索、生成式搜索和智能问答引擎的快速发展,网站速度已经不再只是传统 SEO 的加分项,而是影响内容能否被 AI 系统高效抓取、理解、引用和推荐的重要基础能力。
过去我们谈网站优化,更多关注关键词、外链、内容质量、TDK、结构化数据等因素。但在 AI 搜索时代,搜索引擎和 AI 爬虫会更倾向于访问速度快、结构清晰、稳定性高、可抓取性强的网站。如果一个网站打开缓慢、服务器响应时间过长、页面资源臃肿,即使内容质量不错,也可能降低抓取效率,影响索引频率和内容曝光。
本文将系统讲解:AI 搜索时代为什么网站速度更重要、如何检测网站速度、如何从服务器、Nginx、缓存、图片、前端资源、数据库、CDN 等方面提升网站速度,并附上完整可执行命令。
AI 搜索并不是简单地返回十个蓝色链接,而是会对网页内容进行抓取、解析、摘要、向量化、理解和再组织。这个过程中,网站速度会影响以下几个关键环节。
AI 搜索系统需要大规模抓取网页内容,如果网站响应慢,爬虫在单位时间内能抓取的页面数量就会减少。
例如:
对于内容型网站、电商网站、资讯站、企业官网来说,抓取效率直接影响内容被 AI 搜索发现和引用的概率。
AI 搜索虽然强调内容理解,但最终仍然要服务用户。如果用户从 AI 搜索结果进入你的网站,页面打开太慢,很可能立即关闭。
这会导致:
网站速度越快,用户越容易继续浏览,AI 搜索系统也更可能将你的网站视为优质来源。
Google 等搜索系统非常重视页面体验指标,主要包括:
| 指标 | 含义 | 推荐值 |
|---|---|---|
| LCP | 最大内容绘制时间 | 小于 2.5 秒 |
| INP | 交互响应延迟 | 小于 200ms |
| CLS | 页面视觉稳定性 | 小于 0.1 |
| TTFB | 首字节时间 | 尽量低于 800ms |
这些指标不仅影响传统搜索排名,也会影响 AI 系统对网站质量的评估。
AI 搜索通常会构建自己的内容索引库。如果网站速度慢、抓取失败、资源加载异常,就可能导致页面无法完整进入 AI 数据库。
尤其是以下问题会影响 AI 抓取:
所以,提高网站速度,本质上也是在提高网站对 AI 搜索的友好度。
网站速度优化不是单点工作,而是一个系统工程。通常可以按照以下路径进行:
下面进入实操部分。
优化之前,必须先知道慢在哪里。
curl -o /dev/null -s -w "DNS解析: %{time_namelookup}s\n连接时间: %{time_connect}s\nTLS握手: %{time_appconnect}s\n首字节时间TTFB: %{time_starttransfer}s\n总耗时: %{time_total}s\n" https://example.com
说明:
time_namelookup:DNS 解析耗时;time_connect:TCP 连接耗时;time_appconnect:HTTPS 握手耗时;time_starttransfer:服务器首字节返回时间;time_total:完整请求耗时。如果 TTFB 很高,说明服务器、后端程序或数据库可能存在问题。
如果你已经安装 Node.js,可以使用 Lighthouse:
npm install -g lighthouse
运行测试:
lighthouse https://example.com --view
生成 HTML 报告:
lighthouse https://example.com --output html --output-path ./lighthouse-report.html
如果是在服务器环境执行,也可以使用无界面模式:
lighthouse https://example.com --chrome-flags="--headless" --output html --output-path ./report.html
可以直接访问:
https://pagespeed.web.dev/
输入网站 URL 后查看:
访问:
https://www.webpagetest.org/
建议选择接近目标用户的测试节点,例如:
网站速度的基础是服务器。如果服务器 CPU、内存、磁盘、网络性能不足,前端再优化也有限。
top
或者:
htop
如果没有安装 htop:
sudo apt update
sudo apt install htop -y
CentOS / Rocky Linux:
sudo yum install htop -y
查看 CPU、内存占用:
free -h
查看磁盘空间:
df -h
查看磁盘 IO:
iostat -x 1
如果没有 iostat:
sudo apt install sysstat -y
CentOS:
sudo yum install sysstat -y
ping example.com
测试路由:
traceroute example.com
如果没有 traceroute:
sudo apt install traceroute -y
CentOS:
sudo yum install traceroute -y
如果出现以下情况,应考虑升级服务器:
建议配置:
| 网站类型 | 推荐配置 |
|---|---|
| 企业官网 | 2核2G 起 |
| 博客/内容站 | 2核4G 起 |
| 电商网站 | 4核8G 起 |
| 高流量网站 | 8核16G 以上 |
| 图片/下载站 | 配合对象存储和 CDN |
Nginx 是目前最常见的网站服务器之一。合理配置 Nginx,可以显著提升网站速度。
sudo nginx -t
重载 Nginx:
sudo systemctl reload nginx
重启 Nginx:
sudo systemctl restart nginx
查看状态:
sudo systemctl status nginx
编辑配置文件:
sudo nano /etc/nginx/nginx.conf
在 http 区块中添加或修改:
gzip on;
gzip_comp_level 5;
gzip_min_length 1024;
gzip_vary on;
gzip_proxied any;
gzip_types
text/plain
text/css
text/xml
text/javascript
application/javascript
application/json
application/xml
application/rss+xml
image/svg+xml;
测试配置:
sudo nginx -t
重载:
sudo systemctl reload nginx
检测是否开启成功:
curl -H "Accept-Encoding: gzip" -I https://example.com
如果看到:
Content-Encoding: gzip
说明已生效。
Brotli 通常比 Gzip 压缩率更高,适合 CSS、JS、HTML 等文本资源。
Ubuntu 安装 Brotli 模块:
sudo apt update
sudo apt install nginx nginx-module-brotli -y
如果系统源没有该模块,可根据发行版选择第三方源或使用 OpenResty。
配置示例:
brotli on;
brotli_comp_level 5;
brotli_static on;
brotli_types
text/plain
text/css
application/javascript
application/json
image/svg+xml
application/xml;
检测:
curl -H "Accept-Encoding: br" -I https://example.com
如果返回:
Content-Encoding: br
说明 Brotli 生效。
在网站 server 区块添加:
location ~* \.(jpg|jpeg|png|gif|webp|avif|ico|css|js|svg|woff|woff2|ttf|eot)$ {
expires 30d;
add_header Cache-Control "public, max-age=2592000, immutable";
}
对于 HTML 页面,不建议缓存太久:
location ~* \.(html)$ {
expires 10m;
add_header Cache-Control "public, max-age=600";
}
测试:
curl -I https://example.com/style.css
查看是否有:
Cache-Control
Expires
确保 SSL 已配置,然后在 Nginx server 配置中加入:
listen 443 ssl http2;
完整示例:
server {
listen 443 ssl http2;
server_name example.com www.example.com;
ssl_certificate /etc/letsencrypt/live/example.com/fullchain.pem;
ssl_certificate_key /etc/letsencrypt/live/example.com/privkey.pem;
root /var/www/example.com;
index index.html index.php;
}
测试:
curl -I --http2 https://example.com
在 nginx.conf 的 http 区块加入:
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
types_hash_max_size 2048;
server_tokens off;
测试并重载:
sudo nginx -t
sudo systemctl reload nginx
如果你使用 Apache,可以进行以下优化。
sudo a2enmod deflate
sudo systemctl restart apache2
配置:
AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css
AddOutputFilterByType DEFLATE application/javascript application/json
AddOutputFilterByType DEFLATE image/svg+xml
sudo a2enmod expires
sudo systemctl restart apache2
配置:
ExpiresActive On
ExpiresByType image/jpg "access plus 30 days"
ExpiresByType image/jpeg "access plus 30 days"
ExpiresByType image/png "access plus 30 days"
ExpiresByType image/webp "access plus 30 days"
ExpiresByType text/css "access plus 30 days"
ExpiresByType application/javascript "access plus 30 days"
sudo a2enmod http2
sudo systemctl restart apache2
虚拟主机配置:
Protocols h2 http/1.1
很多网站慢,不是服务器问题,而是图片太大。一张未压缩的 Banner 图可能有 3MB,而压缩后可能只有 200KB。
Ubuntu:
sudo apt update
sudo apt install imagemagick jpegoptim optipng webp -y
CentOS:
sudo yum install epel-release -y
sudo yum install ImageMagick jpegoptim optipng libwebp-tools -y
find /var/www/example.com -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -exec jpegoptim --strip-all --max=80 {} \;
说明:
--strip-all:移除图片元数据;--max=80:质量控制在 80;find /var/www/example.com -type f -iname "*.png" -exec optipng -o2 {} \;
find /var/www/example.com -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) -exec sh -c 'cwebp -q 80 "$1" -o "${1%.*}.webp"' _ {} \;
如果安装了 avifenc:
sudo apt install libavif-bin -y
批量转换:
find /var/www/example.com -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) -exec sh -c 'avifenc --min 25 --max 35 "$1" "${1%.*}.avif"' _ {} \;
可以配置根据浏览器支持返回 WebP:
map $http_accept $webp_suffix {
default "";
"~*webp" ".webp";
}
server {
location ~* \.(png|jpg|jpeg)$ {
try_files $uri$webp_suffix $uri =404;
expires 30d;
add_header Cache-Control "public, max-age=2592000, immutable";
}
}
重载:
sudo nginx -t
sudo systemctl reload nginx
AI 搜索更喜欢结构清晰、HTML 内容可直接读取的网站。如果页面大量依赖 JS 渲染,AI 爬虫可能抓不到完整内容。
安装工具:
npm install -g clean-css-cli terser
压缩 CSS:
cleancss -o style.min.css style.css
压缩 JS:
terser app.js -o app.min.js -c -m
批量压缩 JS:
find ./assets/js -name "*.js" ! -name "*.min.js" -exec sh -c 'terser "$1" -o "${1%.js}.min.js" -c -m' _ {} \;
批量压缩 CSS:
find ./assets/css -name "*.css" ! -name "*.min.css" -exec sh -c 'cleancss -o "${1%.css}.min.css" "$1"' _ {} \;
HTML 示例:
对于不影响首屏的脚本,可以使用:
区别:
defer:HTML 解析完成后按顺序执行;async:下载完成立即执行,顺序不固定。
首屏关键图片不要懒加载,否则会影响 LCP。
如果你的网站使用 WordPress、Discuz、Laravel、ThinkPHP、Django、Typecho 等程序,数据库性能会直接影响页面响应速度。
登录 MySQL:
mysql -u root -p
查看慢查询状态:
SHOW VARIABLES LIKE 'slow_query_log';
SHOW VARIABLES LIKE 'long_query_time';
开启慢查询:
SET GLOBAL slow_query_log = 'ON';
SET GLOBAL long_query_time = 1;
查看慢查询日志路径:
SHOW VARIABLES LIKE 'slow_query_log_file';
编辑 MySQL 配置:
sudo nano /etc/mysql/mysql.conf.d/mysqld.cnf
添加:
slow_query_log = 1
slow_query_log_file = /var/log/mysql/slow.log
long_query_time = 1
log_queries_not_using_indexes = 1
重启 MySQL:
sudo systemctl restart mysql
查看日志:
sudo tail -f /var/log/mysql/slow.log
mysqlcheck -u root -p --optimize --all-databases
修复数据库:
mysqlcheck -u root -p --repair --all-databases
分析数据库:
mysqlcheck -u root -p --analyze --all-databases
SHOW STATUS LIKE 'Threads_connected';
SHOW VARIABLES LIKE 'max_connections';
如果连接数经常打满,需要优化程序、加缓存或提高数据库配置。
缓存是提升网站速度最有效的方式之一。尤其是 WordPress、内容站、企业站,大量页面可以缓存成静态 HTML。
创建缓存目录:
sudo mkdir -p /var/cache/nginx/fastcgi
sudo chown -R www-data:www-data /var/cache/nginx/fastcgi
在 http 区块添加:
fastcgi_cache_path /var/cache/nginx/fastcgi levels=1:2 keys_zone=WORDPRESS:100m inactive=60m;
fastcgi_cache_key "$scheme$request_method$host$request_uri";
fastcgi_cache_use_stale error timeout invalid_header http_500 http_503;
在 PHP location 中添加:
location ~ \.php$ {
include snippets/fastcgi-php.conf;
fastcgi_pass unix:/run/php/php8.2-fpm.sock;
fastcgi_cache WORDPRESS;
fastcgi_cache_valid 200 301 302 60m;
fastcgi_cache_valid 404 10m;
fastcgi_cache_bypass $http_cookie;
fastcgi_no_cache $http_cookie;
add_header X-FastCGI-Cache $upstream_cache_status;
}
测试:
sudo nginx -t
sudo systemctl reload nginx
查看缓存状态:
curl -I https://example.com
如果看到:
X-FastCGI-Cache: HIT
说明缓存命中。
sudo rm -rf /var/cache/nginx/fastcgi/*
sudo systemctl reload nginx
安装 Redis:
sudo apt update
sudo apt install redis-server -y
启动 Redis:
sudo systemctl enable redis-server
sudo systemctl start redis-server
查看状态:
sudo systemctl status redis-server
测试:
redis-cli ping
正常返回:
PONG
安装 PHP Redis 扩展:
sudo apt install php-redis -y
sudo systemctl restart php8.2-fpm
如果是 WordPress,可以安装 Redis Object Cache 插件,然后在后台启用。
如果你使用 PHP 程序,PHP-FPM 配置会影响并发能力和响应时间。
php -v
查看服务:
systemctl status php8.2-fpm
sudo nano /etc/php/8.2/fpm/pool.d/www.conf
推荐配置示例:
pm = dynamic
pm.max_children = 30
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 10
pm.max_requests = 500
说明:
pm.max_children:最大 PHP 进程数;pm.max_requests:每个进程处理多少请求后重启,防止内存泄露;重启:
sudo systemctl restart php8.2-fpm
编辑配置:
sudo nano /etc/php/8.2/fpm/php.ini
添加或修改:
opcache.enable=1
opcache.memory_consumption=128
opcache.interned_strings_buffer=16
opcache.max_accelerated_files=10000
opcache.revalidate_freq=60
opcache.fast_shutdown=1
重启 PHP-FPM:
sudo systemctl restart php8.2-fpm
检查:
php -i | grep opcache
如果用户分布在不同地区,CDN 是必须考虑的方案。CDN 可以把图片、CSS、JS、HTML 缓存在离用户更近的节点。
适合 CDN 的资源:
不适合长时间 CDN 缓存的资源:
| 资源类型 | 建议缓存时间 |
|---|---|
| 图片 | 30 天至 1 年 |
| CSS/JS | 7 天至 1 年 |
| 字体 | 30 天至 1 年 |
| HTML | 5 分钟至 1 小时 |
| API | 根据业务设置 |
curl -I https://cdn.example.com/assets/app.js
常见命中响应头:
CF-Cache-Status: HIT
X-Cache: HIT
Age: 12345
如果是 MISS,说明暂未命中或缓存规则未生效。
除了纯技术性能,还要考虑 AI 爬虫对页面内容的读取方式。
不要让文章正文完全依赖 JavaScript 后渲染。建议:
推荐结构:
文章标题
小标题
正文内容。
这样更利于 AI 搜索理解页面主题和层级。
文章页可以添加 Article Schema:
生成 sitemap 后提交给搜索引擎。
示例 sitemap.xml:
https://example.com/ai-search-speed.html
2025-01-01
weekly
0.8
curl https://example.com/robots.txt
推荐示例:
User-agent: *
Allow: /
Sitemap: https://example.com/sitemap.xml
不要误写成:
User-agent: *
Disallow: /
这会禁止所有爬虫抓取。
以下是一套适用于 Ubuntu + Nginx + PHP + MySQL 网站的常用优化命令。
sudo apt update && sudo apt upgrade -y
sudo apt install htop sysstat curl wget unzip git traceroute net-tools -y
sudo apt install imagemagick jpegoptim optipng webp libavif-bin -y
find /var/www/example.com -type f \( -iname "*.jpg" -o -iname "*.jpeg" \) -exec jpegoptim --strip-all --max=80 {} \;
find /var/www/example.com -type f -iname "*.png" -exec optipng -o2 {} \;
find /var/www/example.com -type f \( -iname "*.jpg" -o -iname "*.jpeg" -o -iname "*.png" \) -exec sh -c 'cwebp -q 80 "$1" -o "${1%.*}.webp"' _ {} \;
sudo nginx -t && sudo systemctl reload nginx
curl -o /dev/null -s -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" https://example.com
curl -H "Accept-Encoding: gzip" -I https://example.com
curl -I --http2 https://example.com
sudo apt install redis-server php-redis -y
sudo systemctl enable redis-server
sudo systemctl start redis-server
sudo systemctl restart php8.2-fpm
redis-cli ping
mysqlcheck -u root -p --optimize --all-databases
mysqlcheck -u root -p --analyze --all-databases
npm install -g lighthouse
lighthouse https://example.com --chrome-flags="--headless" --output html --output-path ./report.html
最后给出一份实用检查表,可以逐项排查。
| 优化项 | 是否完成 |
|---|---|
| 服务器 CPU、内存、磁盘正常 | □ |
| TTFB 小于 800ms | □ |
| 开启 Gzip 或 Brotli | □ |
| 开启 HTTP/2 或 HTTP/3 | □ |
| 图片已压缩 | □ |
| 使用 WebP 或 AVIF | □ |
| CSS/JS 已压缩 | □ |
| 非关键 JS 已 defer/async | □ |
| 首屏图片已优化 | □ |
| 浏览器缓存已配置 | □ |
| CDN 已配置 | □ |
| 数据库慢查询已排查 | □ |
| 页面缓存已开启 | □ |
| Redis 或对象缓存已启用 | □ |
| robots.txt 正常 | □ |
| sitemap.xml 已提交 | □ |
| 重要内容可在 HTML 中直接读取 | □ |
| 结构化数据已添加 | □ |
| Lighthouse 报告已测试 | □ |
AI 搜索时代,网站速度已经从“用户体验优化”升级为“内容可见性优化”。一个速度快、结构清晰、资源轻量、服务器稳定的网站,更容易被 AI 爬虫抓取,更容易被搜索系统理解,也更容易在 AI 搜索结果中获得展示机会。
提高网站速度可以从以下几个方向入手:
网站速度优化不是一次性工作,而是持续迭代的过程。建议每次上线新功能、新模板、新插件后,都重新测试页面性能,避免网站逐渐变慢。对于希望在 AI 搜索时代获得更多流量的网站来说,速度优化已经是基础建设,也是长期竞争力的一部分。