lovart3/FRPC访问问题说明.md
2025-12-17 20:15:18 +08:00

171 lines
3.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.

# FRPC 访问问题说明
## 问题描述
访问 `http://156.225.27.52:5245/` 时:
- 浏览器地址栏变成 `156.225.27.52`(端口号消失)
- 返回 404 Not Found(Nginx)
## 问题分析
### 可能的原因
1. **FRPC 配置类型问题**
- 之前将配置改为 HTTP 类型,但 HTTP 类型需要通过 FRPS 的 HTTP 端口访问
- TCP 类型更适合直接端口访问
2. **Nginx 拦截请求**
- Nginx 的 `default_server` 可能拦截了未匹配的请求
- 5245 端口应该由 FRPS 监听,而不是 Nginx
3. **FRPS 服务端配置**
- FRPS 可能没有正确配置 5245 端口的 TCP 穿透
- 需要检查 FRPS 服务端的配置
4. **浏览器行为**
- 某些浏览器在遇到重定向时会去掉端口号
- 需要确保应用正确处理端口
## 解决方案
### 方案 1: 使用 TCP 类型(推荐)
TCP 类型穿透更简单直接,适合直接端口访问:
```ini
[ai-chatbot]
name = ai-chatbot
type = tcp
local_ip = 127.0.0.1
local_port = 3001
remote_port = 5245
```
**访问方式**: `http://156.225.27.52:5245/`
### 方案 2: 使用 HTTP 类型(需要 FRPS 支持)
如果使用 HTTP 类型,需要:
1. FRPS 配置 HTTP 端口(通常是 80 或 8080)
2. 通过 FRPS 的 HTTP 端口 + custom_domains 访问
3. 配置示例:
```ini
[ai-chatbot]
name = ai-chatbot
type = http
local_ip = 127.0.0.1
local_port = 3001
custom_domains = 156.225.27.52
```
**访问方式**: `http://156.225.27.52:FRPS_HTTP_PORT/`(不是 5245)
### 方案 3: 使用 Nginx 反向代理
如果 FRPS 不支持 HTTP 类型,可以:
1. 保持 FRPC 使用 TCP 类型
2. 配置 Nginx 反向代理到本地 3001 端口
3. 通过 Nginx 的 80 端口访问
## 修复步骤
### 自动修复
运行修复脚本:
```bash
./fix-frpc-access.sh
```
### 手动修复
1. **SSH 连接到服务器**
```bash
ssh -p 5204 root@156.225.27.52
```
2. **检查 FRPC 配置**
```bash
cat /etc/frp/frpc.ini | grep -A 6 'ai-chatbot'
```
3. **修复配置(使用 TCP 类型)**
```bash
# 备份配置
cp /etc/frp/frpc.ini /etc/frp/frpc.ini.backup
# 删除旧的 ai-chatbot 配置
sed -i '/\[ai-chatbot\]/,+6d' /etc/frp/frpc.ini
# 添加正确的 TCP 配置
cat >> /etc/frp/frpc.ini << 'EOF'
[ai-chatbot]
name = ai-chatbot
type = tcp
local_ip = 127.0.0.1
local_port = 3001
remote_port = 5245
EOF
```
4. **重启 FRPC 服务**
```bash
systemctl restart frpc.service
```
5. **检查服务状态**
```bash
systemctl status frpc.service
journalctl -u frpc.service -n 50 | grep ai-chatbot
```
6. **测试本地应用**
```bash
curl http://localhost:3001
```
## 检查清单
- [ ] FRPC 服务运行正常
- [ ] FRPC 配置使用 TCP 类型
- [ ] 应用运行在 3001 端口
- [ ] FRPS 服务端配置了 5245 端口
- [ ] 防火墙开放了 5245 端口
- [ ] 本地应用可以访问(curl http://localhost:3001)
## 常见问题
### Q: 为什么访问时端口号会消失?
A: 这可能是浏览器的行为,或者应用返回了重定向。检查应用的重定向逻辑,确保保留端口号。
### Q: 为什么返回 Nginx 404?
A: 说明请求被 Nginx 拦截了。检查:
1. FRPS 是否正确监听 5245 端口
2. Nginx 是否配置了 default_server 拦截所有请求
3. FRPC 配置是否正确
### Q: TCP 和 HTTP 类型有什么区别?
A:
- **TCP**: 直接端口转发,访问时使用 `http://IP:PORT`
- **HTTP**: 通过 FRPS 的 HTTP 端口访问,需要配置 custom_domains
### Q: 如何确认 FRPS 配置?
A: FRPS 配置通常在 FRP 服务端服务器上,需要检查:
1. FRPS 配置文件中的 `allow_ports` 是否包含 5245
2. FRPS 服务是否正常运行
3. FRPS 日志中是否有 ai-chatbot 的连接记录
## 下一步
1. 运行修复脚本或手动修复配置
2. 确认 FRPS 服务端配置正确
3. 测试访问 `http://156.225.27.52:5245/`
4. 如果仍有问题,检查 FRPS 服务端配置