1. 为什么选择frp作为内网穿透方案
在当今的互联网环境中,内网穿透已经成为开发者、运维人员和企业IT团队的刚需。frp(Fast Reverse Proxy)作为一款开源的内网穿透工具,凭借其轻量级、高性能和易配置的特点,在众多解决方案中脱颖而出。
frp的工作原理是通过在公网服务器(frps)和内网机器(frpc)之间建立安全通道,将内网服务暴露到公网。与同类工具相比,frp具有几个显著优势:
- 多协议支持:支持TCP、UDP、HTTP、HTTPS等多种协议,满足不同场景需求
- 高性能转发:采用Golang编写,转发效率高,资源占用低
- 配置灵活:支持丰富的参数配置,可精细控制转发行为
- 安全可靠:支持TLS加密通信,保障数据传输安全
最新v0.65+版本全面转向TOML格式配置文件,相比之前的INI格式,TOML提供了更清晰的结构和更丰富的类型支持,特别适合复杂配置场景。这也是我们推荐使用新版的重要原因。
需要模型API调用? 免费领10W Token,多模型网关一键接入 Claude、DeepSeek 等主流模型。
2. 环境准备与基础架构规划
2.1 服务器与网络需求
在开始部署前,需要准备以下资源:
-
公网服务器:
- 建议配置:1核CPU/1GB内存/20GB硬盘(基础测试足够)
- 必须条件:固定公网IP或域名(DDNS也可)
- 推荐系统:Ubuntu 20.04+/CentOS 7+
-
内网机器:
- 能运行frpc即可,无特殊配置要求
- 需要穿透的服务需提前部署好
-
网络要求:
- 公网服务器需开放相应端口(默认7000)
- 防火墙需放行frp通信端口
2.2 版本选择与下载
从frp GitHub仓库下载最新稳定版(当前推荐v0.65+):
bash复制# 公网服务器下载
wget https://github.com/fatedier/frp/releases/download/v0.65.0/frp_0.65.0_linux_amd64.tar.gz
tar -zxvf frp_0.65.0_linux_amd64.tar.gz
cd frp_0.65.0_linux_amd64
# 内网机器下载对应版本
# Windows用户下载frp_0.65.0_windows_amd64.zip
注意:服务器端(frps)和客户端(frpc)版本必须一致,否则可能出现兼容性问题。
3. 服务端(frps)配置详解
3.1 基础TOML配置
创建frps.toml配置文件:
toml复制# frps.toml 基础配置
bindAddr = "0.0.0.0"
bindPort = 7000
kcpBindPort = 7000
vhostHTTPPort = 80
vhostHTTPSPort = 443
auth.method = "token"
auth.token = "your_strong_password_here"
# 仪表板配置
webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "admin"
webServer.password = "admin_dashboard_password"
# 日志配置
log.to = "console"
log.level = "info"
log.maxDays = 3
关键参数说明:
bindPort:frp控制通道端口,客户端通过此端口连接auth.token:客户端连接认证令牌,建议使用强密码webServer:管理仪表板配置,可监控连接状态
3.2 高级安全配置
为增强安全性,建议添加以下配置:
toml复制# 安全增强配置
transport.tls.enable = true
transport.tls.certFile = "/path/to/server.crt"
transport.tls.keyFile = "/path/to/server.key"
transport.tls.trustedCaFile = "/path/to/ca.crt"
# 连接限制
maxPoolCount = 100
maxPortsPerClient = 50
tcpMux = true
TLS证书可通过Let's Encrypt免费获取,或使用自签名证书:
bash复制# 生成自签名证书示例
openssl req -x509 -newkey rsa:4096 -nodes -out server.crt -keyout server.key -days 365
3.3 服务启动与管理
使用systemd管理frps服务:
bash复制# 创建systemd服务文件
sudo tee /etc/systemd/system/frps.service <<EOF
[Unit]
Description=Frp Server Service
After=network.target
[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/path/to/frps -c /path/to/frps.toml
[Install]
WantedBy=multi-user.target
EOF
# 启动服务
sudo systemctl daemon-reload
sudo systemctl enable frps
sudo systemctl start frps
验证服务状态:
bash复制systemctl status frps
netstat -tulnp | grep frps
4. 客户端(frpc)配置实战
4.1 基础穿透配置
创建frpc.toml配置文件:
toml复制# frpc.toml 基础配置
serverAddr = "your_server_ip_or_domain"
serverPort = 7000
auth.method = "token"
auth.token = "your_strong_password_here"
[[proxies]]
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000
[[proxies]]
name = "web"
type = "http"
localIP = "127.0.0.1"
localPort = 8080
customDomains = ["your.domain.com"]
4.2 多场景配置示例
场景1:SSH穿透
toml复制[[proxies]]
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000
useEncryption = true
useCompression = true
连接方式:
bash复制ssh -oPort=6000 username@your_server_ip
场景2:HTTP/HTTPS服务穿透
toml复制[[proxies]]
name = "web-https"
type = "https"
localIP = "127.0.0.1"
localPort = 443
customDomains = ["your.domain.com"]
[[proxies]]
name = "web-http"
type = "http"
localIP = "127.0.0.1"
localPort = 80
customDomains = ["your.domain.com"]
场景3:UDP服务穿透(如DNS)
toml复制[[proxies]]
name = "dns"
type = "udp"
localIP = "127.0.0.1"
localPort = 53
remotePort = 6001
4.3 客户端启动与管理
Linux系统启动:
bash复制nohup ./frpc -c frpc.toml > frpc.log 2>&1 &
Windows系统可通过创建计划任务实现开机自启:
- 创建
start_frpc.bat:
bat复制@echo off
frpc.exe -c frpc.toml
- 使用任务计划程序设置为开机启动
5. 高级功能与性能优化
5.1 负载均衡配置
frp支持多客户端负载均衡:
toml复制[[proxies]]
name = "lb-web"
type = "tcp"
localIP = "127.0.0.1"
localPort = 80
remotePort = 8080
group = "web_group"
groupKey = "group_password"
loadBalancer.group = "web_group"
loadBalancer.mode = "roundrobin" # 轮询模式
5.2 连接池优化
提升高并发场景性能:
toml复制transport.protocol = "kcp" # 使用KCP协议提升弱网环境性能
transport.tcpMux = true # 启用连接多路复用
transport.poolCount = 10 # 连接池大小
5.3 健康检查配置
toml复制[[proxies]]
name = "health-check"
type = "tcp"
localIP = "127.0.0.1"
localPort = 8080
remotePort = 8000
healthCheck.type = "tcp"
healthCheck.timeoutSeconds = 3
healthCheck.maxFailed = 3
healthCheck.intervalSeconds = 10
6. 常见问题排查与解决方案
6.1 连接失败排查步骤
-
检查基础网络连通性:
bash复制
telnet your_server_ip 7000 -
验证服务端日志:
bash复制
journalctl -u frps -f -
检查客户端配置:
- 确认serverAddr和serverPort正确
- 验证auth.token与服务端一致
-
防火墙检查:
bash复制sudo ufw status sudo iptables -L -n
6.2 性能问题优化
症状:传输速度慢,延迟高
解决方案:
- 启用压缩:
toml复制useCompression = true - 切换传输协议:
toml复制transport.protocol = "kcp" - 调整TCP参数:
toml复制transport.tcpKeepAlive = 60
6.3 安全性加固建议
- 定期更换认证token
- 限制访问IP:
toml复制# 服务端配置 allowPorts = ["10000-20000"] - 启用TLS加密:
toml复制transport.tls.enable = true - 禁用不必要的协议:
toml复制disableProtocols = ["kcp", "quic"]
7. 监控与维护最佳实践
7.1 使用Prometheus监控
frp内置Prometheus指标端点:
toml复制# frps.toml
metrics.enable = true
metrics.port = 9000
配置Prometheus抓取:
yaml复制scrape_configs:
- job_name: 'frp'
static_configs:
- targets: ['frp_server:9000']
7.2 日志分析与告警
配置logrotate管理日志:
bash复制# /etc/logrotate.d/frp
/path/to/frp.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 644 root root
postrotate
systemctl restart frps
endscript
}
7.3 版本升级策略
- 先升级服务端,保持向后兼容
- 分批升级客户端,监控稳定性
- 重要变更:
- v0.65.0:全面转向TOML配置
- v0.60.0:增强TLS支持
- v0.55.0:改进负载均衡
升级步骤:
bash复制# 1. 备份配置
cp frps.toml frps.toml.bak
# 2. 下载新版本
wget https://github.com/fatedier/frp/releases/download/v0.65.0/frp_0.65.0_linux_amd64.tar.gz
# 3. 替换二进制文件
systemctl stop frps
tar -zxvf frp_0.65.0_linux_amd64.tar.gz
cp frp_0.65.0_linux_amd64/frps /usr/local/bin/
# 4. 启动服务
systemctl start frps
在实际生产环境中,frp的稳定性和性能表现令人满意。特别是在v0.65+版本中,TOML配置格式的引入使得管理复杂规则变得更加清晰。一个实用的技巧是为不同环境(开发、测试、生产)维护不同的配置文件,通过环境变量切换:
bash复制#!/bin/bash
ENV=${1:-"dev"}
frpc -c frpc_${ENV}.toml
