Files
soul-yongping/开发文档/8、部署/宝塔反向代理说明.md
2026-03-07 22:58:43 +08:00

88 lines
2.9 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# soul-api 域名 404 原因与解决
> 来源soul-api/宝塔反向代理说明.txt
## 原因
域名请求先到 Nginx若没有把请求转发到本机 8080 的 Go或站点用了 root/静态目录,就会 404。
---
## 一、先确认 Go 是否在跑(必做)
在宝塔终端或 SSH 里执行:
```
curl -s http://127.0.0.1:8080/health
```
- 若返回 `{"status":"ok"}`:说明 Go 正常,问题在 Nginx看下面第二步。
- 若连接被拒绝或超时:说明 8080 没在监听。去 宝塔 → Go项目管理 → soulApi → 服务状态,看是否「运行中」;看「项目日志」是否有报错。
---
## 二、Nginx 必须「整站走代理」,不能走 root
添加了反向代理仍 404多半是
- 站点默认有 `location / { root ...; index ...; }`,请求被当成静态文件处理,`/health` 找不到就 404
- 或反向代理只绑在了子路径(如 `/api``/``/health` 没被代理。
**做法**:让 soulapi.quwanzhi.com 的**所有路径**都走 8080不要用 root。
在宝塔:网站 → soulapi.quwanzhi.com → 设置 → 配置文件,找到该站点的 `server { ... }`,按下面两种方式之一改。
### 方式 A只保留一个 location /(推荐)
把 server 里**原来的** `location / { ... }`(含 root、index 的那段)**删掉或注释掉**,只保留下面这一段:
```nginx
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
```
保存 → 重载 Nginx或 宝塔 里点「重载配置」)。
### 方式 B整站用下面这一整段 serverHTTPS 示例)
若你希望整站只做反向代理、不混静态,可以把该站点的 server 块整体替换成下面内容(把 your_ssl_cert 等换成你实际的证书路径;没有 SSL 就只用 listen 80 那段):
```nginx
server {
listen 80;
listen 443 ssl http2;
server_name soulapi.quwanzhi.com;
# SSL 证书路径按宝塔实际填写,例如:
# ssl_certificate /www/server/panel/vhost/cert/soulapi.quwanzhi.com/fullchain.pem;
# ssl_certificate_key /www/server/panel/vhost/cert/soulapi.quwanzhi.com/privkey.pem;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_http_version 1.1;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
}
}
```
保存并重载 Nginx。
---
## 三、改完后自测
- 本机:`curl -s https://soulapi.quwanzhi.com/health`
- 或浏览器打开https://soulapi.quwanzhi.com/health
应看到:`{"status":"ok"}`
- 打开 https://soulapi.quwanzhi.com/ 应看到「部署成功」页面。