动态渲染每次请求都要查库、跑模板。静态化之后,访客拿到的是已经生成好的 HTML 文件,由 Nginx 直接返回,不经过 PHP,响应更快、抗并发更强,也不怕数据库临时抖动。
代价是内容变化后需要重新生成对应页面 —— 这一步系统已经自动化了。
php think static:build all # 全站生成
php think static:build category 12 # 只生成某个栏目
php think static:build content 88 # 只生成某条内容
php think static:build all --domain=https://www.example.com
--domain 用于指定对外域名,只影响生成时写入的绝对链接,不会写库。
生成结果落在 public/html/,按语言分目录:
public/html/index.html
public/html/about/index.html
public/html/de/about/index.html
public/html/sitemap.xml
Nginx 侧只要保证「静态文件优先」即可,标准写法:
location / {
try_files $uri $uri/ /index.php$is_args$args;
}
这样 /de/about/ 会先命中 public/html/de/about/index.html,命中不了才回落到 PHP 动态渲染。
在系统设置里打开静态化开关后:
以下改动不会自动触发,需要手动跑一次 static:build all:
最简单的方法:临时改一下静态文件里的某个字,刷新前台看是否变化。
也可以看访问日志——静态命中时不会出现 index.php;或者在地址后加 ?__dynamic=1 强制走动态渲染做对比。