3.1 KiB
3.1 KiB
宝塔环境 Nginx SEO 配置说明
1. 已生成的配置文件
server/nginx-www.tianyuandb.com.conf:天远数据站点(tianyuandb.com)完整 server 配置,含 SEO 爬虫检测与静态模板,与src/composables/useSEO.js路由一致。server/nginx-www.xingfucha.cn.conf:幸福查站点示例,结构相同,含historyQuery、/agent/promote、/agent/invitation等路由。
按实际域名二选一或复制后改 server_name、root、证书路径。
2. 部署前准备:上传 SEO 模板
在服务器上要有 SEO 静态 HTML 目录,路径与站点 root 一致,例如:
- 天远数据:
/www/wwwroot/www.tianyuandb.com/seo-templates/ - 幸福查:
/www/wwwroot/www.xingfucha.cn/seo-templates/
操作步骤:
- 本地生成模板(与 useSEO.js 同步):
cd server npm run generate - 将本地的
tydata-webview-v2/public/seo-templates/整个目录上传到服务器对应站点的seo-templates/目录。 目录下应有:index.html、historyQuery.html、agent.html、agent-promote.html、agent-invitation.html、help.html、help-guide.html、example.html、service.html、inquire-*.html等。
3. 在宝塔里修改 Nginx 配置
-
登录宝塔 → 网站 → 找到对应站点(如 www.tianyuandb.com)→ 设置 → 配置文件。
-
应用 SEO:
- 方式 A:打开
server/nginx-www.tianyuandb.com.conf(或 xingfucha 版),复制从# ========== SEO 爬虫检测到# ========== SEO 配置结束的整段,以及location /seo-templates/和location /两块,合并进当前 server(保留 SSL、include、api、日志等)。 - 方式 B:用示例的整个 server 块替换当前站点 server 块(先备份),并修改
server_name、root、证书路径。
- 方式 A:打开
-
保存后 重载 Nginx:
nginx -t && nginx -s reload。
4. 配置要点说明
- 爬虫判断:按
User-Agent识别百度、Google、必应、360、搜狗、头条、社交媒体等爬虫,爬虫访问时走 SEO 逻辑。 - 路由与模板:
$uri与$seo_file对应关系已在配置中写好(如/→index.html,/historyQuery→historyQuery.html,/agent/promote→agent-promote.html 等),与public/seo-templates下文件名一致。 - 内部 location:
/seo-templates/为 internal,仅由location /内 rewrite 使用,爬虫看到的仍是正常 URL,内容来自seo-templates下的静态 HTML。
5. 验证
将下面域名换成你的站点后执行:
# 天远数据示例
curl -sI -A "Baiduspider/2.0" https://www.tianyuandb.com/
curl -s -A "Baiduspider/2.0" https://www.tianyuandb.com/ | grep -o '<title>.*</title>'
若看到正确标题和响应头 X-SEOMiddleware: nginx-prerendered,说明 SEO 已生效。
若得到 404:按 server/如何检测.md 中「爬虫请求返回 404 时排查」检查:是否已接入 SEO 配置、seo-templates 目录与文件是否存在、error 日志。
更多检测:server/如何检测.md、npm run test(需先启动本地服务)。