frp配置

预先准备

请先去官网下载最新的release版本:GitHub - fatedier/frp: A fast reverse proxy to help you expose a local server behind a NAT or firewall to the internet.

下面我介绍的均是Linux下的配置方法。

frpc

systemd service

由于frpc用的较多,所以先记录frpc的配置。如果你是从头开始配置,请先查看frps的配置:frps
首先解压并重命名你刚刚下载的 压缩包:

1
2
3
4
tar -xvf frp_0.63.0_linux_amd64.tar.gz
# 重命名
mv frp_0.63.0_linux_amd64.tar.gz frp
cd frp

然后为frpc创建一个系统任务,创建一个名为frpc.service的文件,写入以下内容:

注意,你需要修改下面的ExecStart路径为你的frp解压路径。

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

[Service]
Type=idle
User=root
DynamicUser=yes
Restart=on-failure
RestartSec=60s
ExecStart=/root/frp/frpc -c /root/frp/frpc.toml

[Install]
WantedBy=multi-user.target

然后创建一个软链接到你的systemd路径中,比如/etc/systemd/user/或者/etc/systemd/system/,根据你的权限和喜好来选择位置。

1
ln -s /root/frp/frpc.service /etc/systemd/user/frpc.service

然后重载并设置frpc为自动启动:

1
2
3
4
5
6
# 重载systemd文件
sudo systemctl daemon-reload
# 设置frpc自启动
sudo systemctl enable frpc
# 启动frpc
sudo systemctl start frpc

然后你可以通过查看sudo systemctl status frpc来检查frpc的状态。当然目前你还没有配置frpc.toml,你应该是无法正常启动frpc的。接下来我们来配置你的frpc.toml

frpc.toml

首先你需要配置你的frps的服务器:

1
2
3
4
serverAddr = "<your frps ip>"
serverPort = 7000
auth.method = "token"
auth.token = "<your token>

所有的配置文件都需要这个部分来指明frps服务器。

frpc可以实现各种强大的转发,具体的配置请参考文档 | frp。这里提供我经常使用的几种转发:

下面的内容都是修改frpc.toml文件。

http

对于http,frp会提供方向代理的功能(强制),所以你必须需要域名解析才能实现这一操作。你需要将域名解析到你的frps服务器,再通过frpc设置对应的customDomains来实现域名到frpc端口服务的转发。

1
2
3
4
5
6
[[proxies]]
name = "name-http"
type = "http"
localIP = "localhost"
localPort = 80
customDomains = ["<your domain>"]

tcp

下面是一个转发22端口(ssh服务)的tcp隧道,指定了frps的端口为6000,那么你可以可以通过访问frps的6000端口来访问到frpc的22端口。其他任意的tcp服务都可以这么转发。

1
2
3
4
5
6
[[proxies]]
name = "name_ssh"
type = "tcp"
localIP = "localhost"
localPort = 22
remotePort = 6000

https

https的实现需要搭配http服务转发来实现,用到了proxies.plugin也就是frp的插件。

首先需要设置插件:主要是需要修改crtPathkeyPath为你SSL证书的位置

1
2
3
4
5
6
7
[proxies.plugin]
type = "https2http"
localAddr = "127.0.0.1:80"
crtPath = "/etc/nginx/ssl/cert.pem"
keyPath = "/etc/nginx/ssl/key.pem"
hostHeaderRewrite = "127.0.0.1"
requestHeaders.set.x-from-where = "frp"

然后是设置https:这里需要设置你的域名

1
2
3
4
[[proxies]]
name = "name-https"
type = "https"
customDomains = ["<your domain>"]

最后是设置http服务:这里需要根据实际的设置,预留一个http访问的端口,由于我没有使用443作为https

1
2
3
4
5
6
[[proxies]]
name = "hexo-http"
type = "http"
localIP = "localhost"
localPort = 81
customDomains = ["<your domain>"]

可参考的 nginx配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
server{
listen 81;
server_name localhost;
rewrite ^(.*)$ https://$host$1 permanent;
}
server {
listen 80 ;
location / {
root /root/hexo/public/;
}
index index.html index.htm index.nginx-debian.html;
server_name hexo.zhywyt.me;
}

frps

对于frps需要配置的不止是这个配置文件,更多的是你的服务器的端口策略。

frps会用到的端口有下面这些:

  • 7000 与frpc通信端口
  • 7500 frps面板端口
  • 80 http服务端口
  • 443 https服务端口
  • <any tcp port you want> 任何一个你想使用的 tcp端口

而你的frps配置文件如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bindPort = 7000
vhostHTTPPort = 80
vhostHTTPSPort = 443

webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "<user name>"
webServer.password = "<password>"

enablePrometheus = true
auth.token = "<your token>"

allowPorts = [
{ start = 6000, end = 7000}
]

类似的,你也需要为frps创建service:

1
2
3
4
5
6
7
8
9
10
11
12
13
[Unit]
# 服务名称,可自定义
Description = frp server
After = network.target syslog.target
Wants = network.target

[Service]
Type = simple
# 启动frps的命令,需修改为您的frps的安装路径
ExecStart = /home/zhywyt/frp/frps -c /home/zhywyt/frp/frps.toml

[Install]
WantedBy = multi-user.target

frp配置
http://hexo.zhywzs.top/posts/frpsetting/
作者
zhywzs
发布于
2024年9月18日
更新于
2025年7月8日
许可协议