跳转至

Frp 网络穿透工具(v0.27.0)

概述

FRP (Fast Reverse Proxy) 是一个高性能的反向代理应用,可以帮助你将内网服务暴露到公网。

安装

Bash
# 解压到指定目录
tar -zxvf frp_0.27.0_linux_amd64.tar.gz -C /opt/
cd /opt
mv frp_0.27.0_linux_amd64 frp

# 查看目录结构
tree frp/
frp/
├── frpc                 # 客户端程序
├── frpc_full.ini        # 客户端完整配置示例
├── frpc.ini             # 客户端简化配置
├── frps                 # 服务端程序
├── frps_full.ini        # 服务端完整配置示例
├── frps.ini             # 服务端简化配置
├── LICENSE              # 开源协议
└── systemd/             # Systemd 服务文件
    ├── frpc.service
    ├── frpc@.service
    ├── frps.service
    └── frps@.service

基本概念

术语 说明
frps FRP 服务端,运行在具有公网 IP 的服务器上
frpc FRP 客户端,运行在内网需要穿透的服务器上
proxy 代理配置,定义要暴露的内网服务
token 认证令牌,用于服务端和客户端之间的认证

服务端配置(frps.ini)

基础配置参数

INI
[common]
参数 必填 默认值 说明 示例
bind_addr 0.0.0.0 服务端监听地址 bind_addr = 0.0.0.0
bind_port 7000 服务端与客户端通信的 TCP 端口 bind_port = 7000
kcp_bind_port - KCP 协议监听的 UDP 端口,可与 bind_port 相同 kcp_bind_port = 7000
quic_bind_port - QUIC 协议监听的 UDP 端口(v0.15+) quic_bind_port = 7001

说明

  • bindAddr:默认监听所有网卡,生产环境可指定具体 IP
  • kcpBindPort:KCP 适合高延迟、易丢包网络,但多消耗 10-20% 带宽
  • quicBindPort:QUIC 协议,基于 UDP 的低延迟传输

代理端口配置

参数 必填 默认值 说明 示例
vhost_http_port - HTTP 反向代理监听端口 vhost_http_port = 80
vhost_https_port - HTTPS 反向代理监听端口 vhost_https_port = 443
tcp_mux true 是否启用 TCP 多路复用 tcp_mux = true

说明

  • vhost_http_port:启用后,可通过域名访问内网 HTTP 服务
  • vhost_https_port:启用后,可通过域名访问内网 HTTPS 服务
  • tcp_mux:启用后,多个连接可复用同一个 TCP 连接,提高性能

Dashboard 配置(Web 控制台)

参数 必填 默认值 说明 示例
dashboard_addr 0.0.0.0 Dashboard 监听地址 dashboard_addr = 0.0.0.0
dashboard_port - Dashboard 监听端口 dashboard_port = 7500
dashboard_user admin Dashboard 登录用户名 dashboard_user = admin
dashboard_pwd admin Dashboard 登录密码 dashboard_pwd = yourpassword
assets_dir - 静态资源目录(自定义 Dashboard 界面) assets_dir = ./static

说明

  • Dashboard 用于查看 frp 运行状态、代理信息等
  • 生产环境建议修改默认用户名密码
  • 可通过 Nginx 反向代理 + SSL 保护 Dashboard

日志配置

参数 必填 默认值 说明 示例
log_file console 日志文件路径,console 表示输出到终端 log_file = ./frps.log
log_level info 日志级别:trace, debug, info, warn, error log_level = info
log_max_days - 日志保留天数 log_max_days = 3
disable_log_color false 是否禁用日志颜色 disable_log_color = false

日志级别说明

  • trace:最详细,包含所有调试信息
  • debug:详细调试信息
  • info:一般信息(推荐)
  • warn:警告信息
  • error:错误信息

认证配置

参数 必填 默认值 说明 示例
token 推荐 - 客户端连接所需的认证令牌 token = your_token_here
authentication_timeout 900 认证超时时间(秒) authentication_timeout = 900
authenticate_heartbeats false 是否对心跳进行认证 authenticate_heartbeats = false

说明

  • token:服务端和客户端必须配置相同的 token 才能连接
  • authentication_timeout:连接建立后,超过此时间未认证则断开
  • authenticate_heartbeats:启用后,每次心跳都需要认证,更安全但性能略低

连接限制配置

参数 必填 默认值 说明 示例
max_pool_count 5 每个代理的连接池最大连接数 max_pool_count = 5
max_ports_per_client 0 每个客户端可使用的最大端口数,0 表示无限制 max_ports_per_client = 0
allow_ports - 允许客户端使用的端口(不配置则允许所有) allow_ports = 445,5001-5100
privilege_mode false 是否启用特权模式 privilege_mode = false

allow_ports 格式说明

INI
1
2
3
4
5
6
7
8
# 单个端口
allow_ports = 445

# 端口范围
allow_ports = 5001-5100

# 组合
allow_ports = 445,5001-5100,8080

privilege_mode 特权模式

  • 启用后,客户端可以动态创建端口映射
  • 需要配置 privilege_token
  • 生产环境慎用

子域名配置

参数 必填 默认值 说明 示例
subdomain_host - 子域名主机名 subdomain_host = example.com

说明

  • 启用后,客户端可使用 xxx.example.com 形式的子域名
  • 需要配置 DNS 泛解析 *.example.com 到服务器 IP

端口复用配置

参数 必填 默认值 说明 示例
tcp_mux true 是否启用 TCP 多路复用 tcp_mux = true
tcp_mux_keepalive_interval 60 TCP 多路复用保活间隔(秒) tcp_mux_keepalive_interval = 60

完整服务端配置示例

INI
[common]
# === 基础配置 ===
bind_addr = 0.0.0.0
bind_port = 7000
kcp_bind_port = 7000

# === 代理端口 ===
vhost_http_port = 80
vhost_https_port = 443
tcp_mux = true

# === Dashboard ===
dashboard_addr = 0.0.0.0
dashboard_port = 7500
dashboard_user = admin
dashboard_pwd = yourpassword

# === 日志 ===
log_file = ./frps.log
log_level = info
log_max_days = 3

# === 认证 ===
token = your_token_here
authentication_timeout = 900

# === 连接限制 ===
max_pool_count = 5
max_ports_per_client = 0
allow_ports = 445,5001-5100

# === 子域名 ===
subdomain_host = example.com

客户端配置(frpc.ini)

基础配置参数

INI
[common]
参数 必填 默认值 说明 示例
server_addr - 服务端地址(公网 IP 或域名) server_addr = x.x.x.x
server_port - 服务端 bind_port 端口 server_port = 7000
nat_hole_stun_server - STUN 服务器地址(用于 NAT 穿透) nat_hole_stun_server = stun.easyvoip.com:3478

认证配置

参数 必填 默认值 说明 示例
token 推荐 - 与服务端一致的认证令牌 token = your_token_here
login_fail_exit true 登录失败后是否退出 login_fail_exit = false
protocol tcp 与服务端通信的协议:tcp, kcp, quic protocol = kcp

说明

  • login_fail_exit = false:登录失败后持续重试,适合移动网络
  • protocol = kcp:在高延迟、易丢包网络下性能更好

日志配置

参数 必填 默认值 说明 示例
log_file console 日志文件路径 log_file = ./frpc.log
log_level info 日志级别:trace, debug, info, warn, error log_level = info
log_max_days - 日志保留天数 log_max_days = 3
disable_log_color false 是否禁用日志颜色 disable_log_color = false

Admin API 配置(热重载)

参数 必填 默认值 说明 示例
admin_addr 127.0.0.1 Admin API 监听地址 admin_addr = 127.0.0.1
admin_port - Admin API 监听端口 admin_port = 7400
admin_user - Admin API 用户名 admin_user = admin
admin_pwd - Admin API 密码 admin_pwd = admin

说明

  • 启用后可通过 HTTP API 管理 frpc(如热重载配置)
  • API 端点:http://127.0.0.1:7400/api/reload

代理配置

参数 必填 默认值 说明 示例
pool_count 1 连接池大小(预建立连接数) pool_count = 1
tcp_mux true 是否启用 TCP 多路复用 tcp_mux = true
heartbeat_interval 30 心跳间隔(秒) heartbeat_interval = 30
heartbeat_timeout 90 心跳超时时间(秒) heartbeat_timeout = 90

完整客户端配置示例

INI
[common]
# === 基础配置 ===
server_addr = x.x.x.x
server_port = 7000

# === 认证 ===
token = your_token_here
login_fail_exit = false
protocol = tcp

# === 日志 ===
log_file = ./frpc.log
log_level = info
log_max_days = 3

# === Admin API ===
admin_addr = 127.0.0.1
admin_port = 7400
admin_user = admin
admin_pwd = admin

# === 代理配置 ===
pool_count = 1
tcp_mux = true
heartbeat_interval = 30
heartbeat_timeout = 90

常用代理配置

TCP 端口映射

场景:将内网 SSH 服务暴露到公网

INI
1
2
3
4
5
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
参数 说明 示例
type 代理类型 tcp
local_ip 内网服务地址 127.0.0.1
local_port 内网服务端口 22
remote_port 服务端暴露的公网端口 6000

使用方式: ssh -p 6000 user@x.x.x.x

UDP 服务

场景:将内网 DNS 服务暴露到公网

INI
1
2
3
4
5
[dns]
type = udp
local_ip = 8.8.8.8
local_port = 53
remote_port = 6001
参数 说明 示例
type 代理类型 udp
local_ip 内网服务地址 8.8.8.8
local_port 内网服务端口 53
remote_port 服务端暴露的公网端口 6001

HTTP 反向代理

场景:将内网 Web 服务通过域名暴露到公网

INI
1
2
3
4
5
[web]
type = http
local_ip = 127.0.0.1
local_port = 80
custom_domains = www.yourdomain.com
参数 说明 示例
type 代理类型 http
local_ip 内网服务地址 127.0.0.1
local_port 内网服务端口 80
custom_domains 自定义域名(多个用逗号分隔) www.yourdomain.com

使用方式: 访问 http://www.yourdomain.com

HTTPS 反向代理

场景:将内网 HTTPS 服务通过域名暴露到公网

INI
1
2
3
4
5
[web-https]
type = https
local_ip = 127.0.0.1
local_port = 443
custom_domains = www.yourdomain.com
参数 说明 示例
type 代理类型 https
local_ip 内网服务地址 127.0.0.1
local_port 内网服务端口 443
custom_domains 自定义域名 www.yourdomain.com

使用方式: 访问 <https://www.yourdomain.com>

子域名配置

场景:使用子域名访问不同服务

INI
1
2
3
4
5
6
7
8
9
# 服务端配置
subdomain_host = example.com

# 客户端配置
[web]
type = http
local_ip = 127.0.0.1
local_port = 80
subdomain = blog

使用方式: 访问 http://blog.example.com

端口批量映射

场景:批量映射多个端口

INI
1
2
3
4
5
[range:ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22-25,30,35
remote_port = 6000-6003,6005,6010

说明

  • range: 前缀表示批量映射
  • local_portremote_port 数量必须一致

负载均衡

场景:多个后端服务负载均衡

INI
[web]
type = http
local_ip = 127.0.0.1
local_port = 80
custom_domains = www.yourdomain.com
locations = /,/news,/images

# 健康检查
health_check_type = http
health_check_url = /status
health_check_interval_s = 10
health_check_max_failed = 3
health_check_timeout_s = 2
参数 说明 示例
locations URL 路由(多个用逗号分隔) /,/news,/images`
health_check_type 健康检查类型:tcp, http http
health_check_url 健康检查 URL /status
health_check_interval_s 检查间隔(秒) 10
health_check_max_failed 最大失败次数 3
health_check_timeout_s 超时时间(秒) 2

插件功能

场景:使用内置插件

INI
# HTTP 代理插件
[plugin_http_proxy]
type = tcp
remote_port = 6002
plugin = http_proxy
plugin_http_user = abc
plugin_http_passwd = abc

# Unix Domain Socket 插件
[plugin_unix_socket]
type = tcp
remote_port = 6003
plugin = unix_domain_socket
plugin_unix_path = /var/run/docker.sock

可用插件

  • http_proxy:HTTP 代理
  • socks5:SOCKS5 代理
  • unix_domain_socket:Unix 域套接字
  • http2https:HTTP 转 HTTPS
  • https2http:HTTPS 转 HTTP

启动与管理

启动命令

Bash
# 服务端启动
./frps -c ./frps.ini

# 后台启动
nohup ./frps -c frps.ini > /dev/null 2>&1 &

# 客户端启动
./frpc -c ./frpc.ini

# 后台启动
nohup ./frpc -c frpc.ini > /dev/null 2>&1 &

配置验证

Bash
1
2
3
4
5
# 验证服务端配置
./frps -c ./frps.ini --verify

# 验证客户端配置
./frpc -c ./frpc.ini --verify

热重载配置

Bash
1
2
3
4
5
# 通过 Admin API 重载
curl http://127.0.0.1:7400/api/reload

# 查看状态
curl http://127.0.0.1:7400/api/status

Systemd 服务配置

frps.service(服务端)

INI
[Unit]
Description=Frp Server Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/opt/frp/frps -c /opt/frp/frps.ini

[Install]
WantedBy=multi-user.target

frpc.service(客户端)

INI
[Unit]
Description=Frp Client Service
After=network.target

[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/opt/frp/frpc -c /opt/frp/frpc.ini

[Install]
WantedBy=multi-user.target

启用服务:

Bash
# 复制服务文件
cp frps.service /etc/systemd/system/
cp frpc.service /etc/systemd/system/

# 重载 systemd
systemctl daemon-reload

# 启用并启动
systemctl enable frps
systemctl start frps

systemctl enable frpc
systemctl start frpc

# 查看状态
systemctl status frps
systemctl status frpc

故障排查

检查连接状态

Bash
1
2
3
4
5
# 查看 Dashboard
curl http://127.0.0.1:7500

# 查看代理状态
curl http://127.0.0.1:7400/api/status

查看日志

Bash
1
2
3
4
5
6
7
# 实时日志
tail -f ./frps.log
tail -f ./frpc.log

# 最近错误
grep -i "error" ./frps.log
grep -i "error" ./frpc.log

测试连通性

Bash
1
2
3
4
5
# 测试服务端端口
telnet x.x.x.x 7000

# 测试代理
curl -I http://www.yourdomain.com

常见问题

问题 原因 解决
连接被拒绝 防火墙阻止 检查安全组规则
token 认证失败 token 不一致 检查服务端和客户端配置
域名无法访问 DNS 未解析 检查 DNS 配置
端口无法使用 端口被占用 检查端口占用情况