1、FRP介绍
frp是一个快速的反向代理服务,可以把NAT或防火墙后面的本地服务暴露到公网上。
frp目前支持TCP、UDP、HTTP以及HTTPS等协议,通过域名可以将请求转发到内网的服务上。
1.1 frp的主要功能和优点有:
- 简单易用,无需复杂的配置就可以直接暴露内网服务。
- 连接速度快,利用长连接池可以显著提高访问速度。
- 支持多种协议,包括TCP、UDP、HTTP、HTTPS等。
- 安全可靠,支持参数加密、身份验证等功能。
- 跨平台支持,客户端和服务器支持常见的操作系统。
- 轻量高效,资源占用低,不会明显降低主机性能。
- 社区活跃,版本迭代快速。
FRP可以用于内部网络穿透、远程桌面或远程服务访问等场景。它可以轻松地通过公网访问部署在公司、家庭内网环境中的服务,如数据库服务器、Web应用等,在保证安全性的同时,获得高性能的访问体验。
1.2 FRP 主要由两部分组成:
FRPC 客户端(frp client)
- 运行在公司内网机器上
- 接收来自公网的访问请求
- 将请求转发给内网服务
FRPS 服务端(frp server)
- 部署在具有公网 IP 的机器上
- 暴露服务的公网入口
- 接收公网访问请求
- 通过加密隧道转发到 FRPC
2、准备工作
- 一台有公网IP的服务器
- 一台内网主机
- 下载FRP最新版本(https://github.com/fatedier/frp/releases)
- FRP帮助文档(https://gofrp.org/zh-cn/docs/)
3、配置文件
解压下载后的frp压缩包,里面默认包含了服务器端软件和配置文件(frps、frps.toml);客户端软件和配置文件(frpc、frpc.toml)。
3.1 frps.toml配置(服务端配置文件)
1
2
3
4
5
6
7
8
9
10
11
|
bindPort = 7000 # 服务端监听端口
vhostHTTPPort = 80 # 用于反向代理HTTP主机时使用
vhostHTTPSPort = 443 # 用于反向代理HTTPS主机时使用
auth.method = "token" # 鉴权方式
auth.token = "password" # 客户端连接的token
# 后台管理面板配置
webServer.port = 7500 # 后台面板端口号
webServer.addr = "0.0.0.0" # 后台管理地址
webServer.user = "admin" # 后台管理员账号
webServer.password = "pwd" # 后台管理员密码
|
3.2 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
|
serverAddr = "x.x.x.x"
serverPort = 7000 # 公网服务端通信端口
auth.method = 'token' # 客户端访问验证方式
auth.token = "public" # 令牌,与公网服务端保持一致
[[proxies]]
name = "test-http"
type = "tcp"
localIP = "127.0.0.1" # 需要暴露的服务的IP
localPort = 9000 # 将本地9000端口的服务暴露在公网的6060端口
remotePort = 6060 # 暴露服务的公网入口
[[proxies]]
name = "ssh"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 6000
[[proxies]]
name = "web" # (名字随意,不要重复)
type = "http"
localPort = xx # (改为服务实际端口)
customDomains = ["www.yourdomain.com"]
[[proxies]]
name = "test_htts2http"
type = "https"
customDomains = ["test.yourdomain.com"] # (改为需要绑定的域名)
[proxies.plugin]
type = "https2http"
localAddr = "127.0.0.1:xx" # (改为服务实际端口)
# HTTPS 证书相关的配置
crtPath = "./server.crt"
keyPath = "./server.key"
hostHeaderRewrite = "127.0.0.1"
requestHeaders.set.x-from-where = "frp"
|
注意: 访问的端口一定要开放
4、启动 frps 和 frpc
4.1 服务端启动
命令行启动(测试用)
添加服务启动(日常使用)
新建服务文件frps.service
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[Unit]
Description=FRP Service
After=network.target
[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.toml
[Install]
WantedBy=multi-user.target
|
添加服务
1
|
sudo mv frps.service /etc/systemd/system/frps.service
|
注意:文件的路径替换成自己项目的实际路径
服务管理相关命令
1
2
3
4
|
sudo systemctl enable frps
sudo systemctl restart frps
sudo systemctl start frps
sudo systemctl status frps
|
4.2 客户端启动
命令行启动(测试用)
添加服务启动(日常使用)
新建服务文件frpc.service
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[Unit]
Description=FRP Cilent
After=network.target
[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/frp/frpc -c /usr/local/frp/frpc.toml
[Install]
WantedBy=multi-user.target
|
添加服务
1
|
sudo mv frpc.service /etc/systemd/system/frpc.service
|
注意:文件的路径替换成自己项目的实际路径
服务管理相关命令
1
2
3
4
|
sudo systemctl enable frpc
sudo systemctl restart frpc
sudo systemctl start frpc
sudo systemctl status frpc
|
5、绑定域名
域名绑定时,添加A记录,IP填写公网服务器IP。