FRP 内网穿透服务超详细部署与配置指南
FRP 内网穿透服务超详细部署与配置指南—目录
一、服务端(Frps)深度配置
1.1 服务端安全加固
1.1.1 防火墙配置
# 开放必要端口(以Ubuntu为例)
sudo ufw allow 7000/tcp # FRP服务端端口
sudo ufw allow 7500/tcp # 监控面板端口
sudo ufw allow 80/tcp # HTTP服务(可选)
sudo ufw allow 443/tcp # HTTPS服务(可选)
sudo ufw reload
1.1.2 强制TLS加密
修改 frps.ini
:
[common]
tls_only = true # 强制所有连接使用TLS
tls_cert_file = /opt/frp/cert/fullchain.pem
tls_key_file = /opt/frp/cert/privkey.pem
1.2 多域名与HTTPS支持
1.2.1 使用Let’s Encrypt自动申请证书
# 安装Certbot
sudo apt install certbot
# 申请证书(需域名已解析)
sudo certbot certonly --standalone -d frp.example.com
# 证书路径示例:
# /etc/letsencrypt/live/frp.example.com/fullchain.pem
# /etc/letsencrypt/live/frp.example.com/privkey.pem
1.2.2 配置HTTP/HTTPS代理
[common]
vhost_http_port = 80
vhost_https_port = 443
# 指定不同域名的证书
[http:web1]
type = http
custom_domains = web1.example.com
plugin = https2http
plugin_local_addr = 127.0.0.1:8080
plugin_crt_path = /etc/letsencrypt/live/web1.example.com/fullchain.pem
plugin_key_path = /etc/letsencrypt/live/web1.example.com/privkey.pem
[http:web2]
type = http
custom_domains = web2.example.com
plugin = https2http
plugin_local_addr = 127.0.0.1:8081
二、客户端(Frpc)高级功能
2.1 多协议穿透配置
2.1.1 UDP穿透(游戏服务器/实时视频流)
[udp_video]
type = udp
local_ip = 192.168.1.5
local_port = 6000
remote_port = 6000
2.1.2 安全TCP(STCP)配置
服务端配置:
[common]
allow_ports = 7000-8000 # 限制可分配端口范围
客户端A(服务提供方):
[secret_ssh]
type = stcp
sk = my_shared_key
local_ip = 127.0.0.1
local_port = 22
客户端B(访问方):
[visitor_ssh]
type = stcp
role = visitor
server_name = secret_ssh
sk = my_shared_key
bind_addr = 127.0.0.1
bind_port = 6000
2.2 负载均衡与高可用
2.2.1 多服务端配置
[common]
server_addr = frp1.example.com,frp2.example.com # 多个服务端地址
server_port = 7000
2.2.2 客户端自动重连
[common]
login_fail_exit = false # 连接失败不退出
max_pool_count = 10 # 增加连接池容量
三、监控与运维
3.1 Prometheus监控集成
3.1.1 服务端启用Metrics
[common]
enable_prometheus = true
prometheus_port = 7400
3.1.2 Grafana仪表盘配置
- 下载FRP仪表盘模板:Grafana Dashboard ID 13123
- 配置Prometheus数据源指向
http://frp-server:7400
3.2 日志分析与告警
3.2.1 ELK日志收集
[common]
log_file = /var/log/frps.log
log_max_days = 30
使用Filebeat将日志发送到Elasticsearch:
# Filebeat配置示例
filebeat.inputs:
- type: log
paths:
- /var/log/frps.log
四、企业级场景实战
4.1 跨地域多节点部署
4.1.1 架构设计
+-----------------+
| Global LB |
+--------+--------+
|
+----------+----------+
| | |
+--------+-----+ +--+-------+ ++--------+
| FRP节点(美西)| | FRP节点(法兰克福) | FRP节点(新加坡)|
+-------------+ +-------------+ +-------------+
4.1.2 客户端智能路由
[common]
server_addr = us-west.example.com
# 备用节点
server_addr = fr-fr.example.com, sg.example.com
4.2 K8s服务穿透
4.2.1 Sidecar模式部署
# Kubernetes Deployment示例
apiVersion: apps/v1
kind: Deployment
metadata:
name: web-app
spec:
replicas: 1
template:
spec:
containers:
- name: web
image: nginx
ports:
- containerPort: 80
- name: frpc
image: snowdreamtech/frpc
volumeMounts:
- name: frpc-config
mountPath: /etc/frp
volumes:
- name: frpc-config
configMap:
name: frpc-config
五、安全加固手册
5.1 网络层防护
5.1.1 IP白名单限制
[common]
allow_ports = 6000-7000 # 限制可映射端口
privilege_allow_ips = 192.168.1.0/24 # 仅允许指定IP注册
5.1.2 端口随机化策略
[ssh_random]
type = tcp
local_port = 22
remote_port = 0 # 服务端自动分配随机端口
5.2 应用层防护
5.2.1 双向TLS认证
生成客户端证书:
openssl req -newkey rsa:2048 -nodes -keyout client.key -x509 -days 365 -out client.crt
服务端配置:
[common]
tls_trusted_ca_file = /opt/frp/ca.crt # 信任的CA证书
客户端配置:
[common]
tls_cert_file = /path/to/client.crt
tls_key_file = /path/to/client.key
六、性能调优指南
6.1 内核参数优化
# 增加TCP连接数限制
echo "net.core.somaxconn = 65535" >> /etc/sysctl.conf
echo "net.ipv4.tcp_max_syn_backlog = 65535" >> /etc/sysctl.conf
sysctl -p
6.2 FRP参数调优
[common]
max_pool_count = 1000 # 最大连接池数量
tcp_mux = true # 启用多路复用
heartbeat_timeout = 90 # 心跳超时时间(秒)
七、故障排查大全
7.1 连接问题诊断
# 检查端口连通性
telnet frp.example.com 7000
# 抓包分析(服务端)
tcpdump -i eth0 port 7000 -w frp.pcap
# 客户端调试模式
./frpc -c frpc.ini --log-level=debug
7.2 常见错误解决方案
错误现象 | 解决方法 |
---|---|
token mismatch | 检查服务端与客户端token是否一致 |
no such service | 确认客户端配置的服务名称是否已注册 |
connection timeout | 检查防火墙规则,确认UDP端口是否开放 |
八、自动化部署脚本
8.1 Ansible部署FRP集群
# frp.yml
- hosts: frp_servers
tasks:
- name: Download FRP
get_url:
url: "https://github.com/fatedier/frp/releases/download/v{{ frp_version }}/frp_{{ frp_version }}_linux_amd64.tar.gz"
dest: /tmp/frp.tar.gz
- name: Extract FRP
unarchive:
src: /tmp/frp.tar.gz
dest: /opt/
remote_src: yes
8.2 Terraform云服务器部署
# frp.tf
resource "aws_instance" "frp_server" {
ami = "ami-0c55b159cbfafe1f0"
instance_type = "t3.micro"
security_groups = [aws_security_group.frp.name]
}
resource "aws_security_group" "frp" {
ingress {
from_port = 7000
to_port = 7000
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
}
九、扩展学习资源
9.1 深入理解NAT穿透
• 书籍:《计算机网络:自顶向下方法》(第7章)
• 论文:《STUN: Session Traversal Utilities for NAT》(RFC 5389)
9.2 FRP源码研读
• 核心模块:
• cmd/frpc
:客户端主逻辑
• pkg/config
:配置解析器
• pkg/util/net
:网络层实现
通过本指南,您将掌握FRP从基础到企业级应用的完整知识体系,无论是家庭NAS穿透、跨国企业组网,还是云原生环境集成,均可游刃有余。