frp的使用

2025-08-27 liunx frp, frps, frpc

frp(Fast Reverse Proxy)现在简直是内网穿透界的“大佬级神器”了!全程国产,GitHub 上星星都快赶上银河了——9万多颗⭐,社区活跃得像开了挂。我第一次碰它的时候,直接被它的操作简洁暴击:几行配置文件,分分钟就能穿透内网,爽到飞起!

frp的工作原理详解

frp 玩得就是客户端–服务端模式,原理简单到飞起:你只需要一台有公网 IP 的“中转大佬”服务器,它叫 frps(s = server),然后在你的小内网机上跑个 frpc(c = client)就行了。内网和公网之间的穿透,全靠它们默契配合,一切尽在掌控之中。

frp 的核心玩法其实很简单:它靠 客户端(frpc)+ 服务端(frps) 配合,把内网服务“搬到”公网,让你随时随地能访问。原理拆开来看,分几个步骤:

  1. 服务端 frps:
    • 这是你的公网“大佬”,拥有公网 IP,负责接收所有来自内网客户端的连接请求。
    • 相当于内网和公网之间的“中转站”,只要它在线,你的内网就能被公网访问。
  2. 客户端 frpc:
    • 运行在内网机器上,它偷偷地把内网服务(比如 80 端口的 Web 或 3306 的数据库)打包发给 frps。
    • 它就像内网的“小喵星人”,悄悄地把消息送到公网服务器,然后你就能远程访问。
  3. 连接建立:
    • frpc 主动向 frps 建立隧道连接,frps 记录这个隧道。
    • 当外部有人访问 frps 的某个端口时,frps 就把请求通过之前建立好的隧道,转发到内网的 frpc,最后由内网服务响应。
  4. 数据转发:
    • 数据在 frps 和 frpc 之间像火箭一样飞来飞去,完成请求–响应的全过程。
    • 所有操作对外部访问者完全透明,好像内网服务本身就挂在公网一样。

准备环境

部署 frp,其实就是让 内网小机公网大佬服务器握手言和,全程几步搞定:

  • 一台 有公网 IP 的服务器(frps 所在),Linux 系统最佳。
  • 一台 内网机器(frpc 所在),可以是你的家用电脑、办公机或虚拟机。
  • 下载对应平台的 frp 压缩包:frp Releases

配置服务端

下载解压 frp

1
2
3
wget https://github.com/fatedier/frp/releases/download/v0.64.0/frp_0.64.0_linux_amd64.tar.gz
tar zxvf frp_0.64.0_linux_amd64.tar.gz
cd frp_0.64.0_linux_amd64/

服务端配置文件 frps.toml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# FRPS 绑定端口,用于接收 frpc 客户端的连接请求
bindPort = 7000

# vhost_http 映射的 HTTP 端口,外部访问内网 HTTP 服务时使用
vhostHTTPPort = 80

# vhost_https 映射的 HTTPS 端口,外部访问内网 HTTPS 服务时使用
vhostHTTPSPort = 443

# Dashboard 面板监听地址,这里 0.0.0.0 表示监听所有网卡
webServer.addr = "0.0.0.0"

# Dashboard 面板端口
webServer.port = 7500

# Dashboard 登录用户名
webServer.user = "admin"

# Dashboard 登录密码
webServer.password = "your_password"

# 客户端认证方式,这里使用 token 验证
auth.method = "token"

# 认证 token,客户端连接时必须提供一致的 token
auth.token = "your_secret_token"

# 日志配置
# 日志保存路径
log.to = "/var/log/frps.log"

# 日志等级,info 表示记录一般信息和状态
log.level = "info"

# 日志最大保存天数,超过天数会自动清理
log.maxDays = 3

# 连接池配置
# 每个客户端到服务器的最大 TCP 连接池数量
transport.maxPoolCount = 5

# 单个客户端允许映射的远程端口数量,0 表示不限制
transport.maxPortsPerClient = 0

这套配置就像给你的公网大佬服务器开了一条安全、高效的内网高速通道:

  • 大门端口(7000)守好入口
  • HTTP/HTTPS 正常上线
  • 面板监控一目了然
  • Token 保证只有你的小伙伴能进
  • 日志记录战况
  • 隧道跑道多,映射端口无限,内网服务畅通无阻

创建systemd服务文件 /etc/systemd/system/frps.service

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/bin/frps -c /etc/frp/frps.toml
LimitNOFILE=1048576

[Install]
WantedBy=multi-user.target

启动服务

1
2
3
4
5
6
sudo mkdir -p /etc/frp
sudo cp frps /usr/local/bin/frps
sudo cp frps.toml /etc/frp/frps.toml
sudo systemctl daemon-reload
sudo systemctl enable frps
sudo systemctl start frps

配置客户端 frpc

客户端配置才是真正的百变小精灵!我通常会根据不同场景搞出一堆配置文件——映射 Web、数据库、SSH,甚至搞几个专属隧道,各司其职,灵活到飞起。内网服务一键上公网,分分钟开挂模式上线,让你随便切换、随便玩!

下载解压

1
2
3
wget https://github.com/fatedier/frp/releases/download/v0.64.0/frp_0.64.0_linux_amd64.tar.gz
tar zxvf frp_0.64.0_linux_amd64.tar.gz
cd frp_0.64.0_linux_amd64/

客户端配置文件 frpc.toml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
serverAddr = "your_server_ip"
serverPort = 7000
auth.method = "token"
auth.token = "your_secret_token"

# SSH服务穿透
[[proxies]]
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000

# Web服务穿透
[[proxies]]
name = "web"
type = "http"
localPort = 8080
customDomains = ["your_domain.com"]

# HTTPS服务穿透
[[proxies]]
name = "web-https"
type = "https"
localPort = 8443
customDomains = ["your_domain.com"]

# 文件服务器
[[proxies]]
name = "file-server"
type = "tcp"
localIP = "127.0.0.1"
localPort = 9000
remotePort = 9000

# 数据库访问(谨慎使用)
[[proxies]]
name = "mysql"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3306
remotePort = 3306

启动客户端

1
./frpc -c frpc.toml

frp 高级玩法与生产实践

frp 不只是“内网穿透神器”,玩转它的高级特性,就能把内网服务玩得花里胡哨又稳如老狗。下面带你解锁一波骚操作。

HTTP 基本认证

给你的 Web 服务加个密码锁,防止被人随便摸进来。

1
2
3
4
5
6
7
[[proxies]]
name = "web-auth"
type = "http"
localPort = 8080
customDomains = ["admin.your_domain.com"]
httpUser = "admin"
httpPassword = "password"

访问时需要输入 admin/password 才能进,简单粗暴的小门神。


负载均衡

一台不够?上多台!frp 支持同一域名后端挂多机器,自动分流请求。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 服务器1
[[proxies]]
name = "web1"
type = "http"
localPort = 8080
customDomains = ["app.your_domain.com"]
group = "web"
groupKey = "123456"

# 服务器2
[[proxies]]
name = "web2"
type = "http"
localPort = 8080
customDomains = ["app.your_domain.com"]
group = "web"
groupKey = "123456"

两台机器组成一个“web 小分队”,frp 帮你分摊压力。


带宽限制

怕有人把你的带宽吸干?限速走起。

1
2
3
4
5
6
[[proxies]]
name = "limited-web"
type = "http"
localPort = 8080
customDomains = ["slow.your_domain.com"]
transport.bandwidthLimit = "1MB"

每秒最多 1MB,不卡你机,但也别想吃撑。


健康检查

没人想把流量打到挂掉的服务上。frp 自带体检功能。

1
2
3
4
5
6
7
8
9
10
[[proxies]]
name = "web-health"
type = "http"
localPort = 8080
customDomains = ["health.your_domain.com"]
healthCheck.type = "http"
healthCheck.timeoutSeconds = 3
healthCheck.maxFailed = 3
healthCheck.intervalSeconds = 10
healthCheck.path = "/health"

定时访问 /health,失败次数超限就踢出集群,自动止损。


监控与日志

生产环境少不了监控和日志分析,frp 提供了三板斧:

Web Dashboard

访问 http://your_server_ip:7500,能看到:

  • 当前客户端列表
  • 每个代理的流量统计
  • 连接状态和错误信息
  • 实时请求日志

Prometheus 监控

frps.toml 中启用:

1
2
webServer.pprofEnable = true
enablePrometheus = true

然后访问 http://your_server_ip:7500/metrics 获取指标。

日志分析

frp 日志信息相当详细:

1
2
3
2024-01-15 10:30:15 [I] [service.go:349] frps tcp listen on 0.0.0.0:7000
2024-01-15 10:30:20 [I] [control.go:464] [ssh] new proxy connection from [192.168.1.100:54321]
2024-01-15 10:30:25 [W] [control.go:123] [ssh] connection timeout, retry in 5 seconds

配合 ELKGrafana Loki,简直如虎添翼。


性能优化

高并发场景下,调一调配置,性能能飞一截。

连接池优化

1
2
3
transport.poolCount = 10
transport.tcpMux = true
transport.tcpMuxKeepaliveInterval = 60

连接复用,多路并发,省资源又提速。

启用压缩

1
2
3
4
5
6
[[proxies]]
name = "web-compressed"
type = "http"
localPort = 8080
customDomains = ["compressed.your_domain.com"]
transport.useCompression = true

适合文字内容多的服务,压一压省带宽。

TCP 保活

1
transport.tcpKeepalive = 7200

网络抖动多的环境下,能让连接更皮实。


安全加固

别让你的隧道成了后门,安全必须拉满。

TLS 加密

1
2
3
transport.tls.enable = true
transport.tls.certFile = "/path/to/cert.pem"
transport.tls.keyFile = "/path/to/key.pem"

客户端和服务端之间全程加密,别人抓包也看不懂。

IP/端口限制

1
2
3
4
5
6
7
8
allowPorts = "6000-6010,7000,9000-9010"

[[proxies]]
name = "ssh-restricted"
type = "tcp"
localPort = 22
remotePort = 6000
allowUsers = ["user1", "user2"]

只开放必要端口,且只让指定用户用。

防火墙加固

1
2
3
4
5
ufw allow 7000/tcp
ufw allow 80/tcp
ufw allow 443/tcp
ufw allow 7500/tcp from your_admin_ip
ufw enable

👉 最小化暴露面,最大化安全感。


常见问题与解决方案

连接频繁断开

调节心跳:

1
2
transport.heartbeatInterval = 30
transport.heartbeatTimeout = 90

端口被占用

1
2
netstat -tlnp | grep :7000
ss -tlnp | grep :7000

DNS 解析异常

1
2
nslookup your_domain.com
dig your_domain.com

性能怀疑瓶颈

iperf3 测试网络:

1
2
iperf3 -s -p 5201   # 服务端
iperf3 -c server_ip -p 5201 -t 60 # 客户端