FRP 配置

FRP 配置

最新Frp https://github.com/fatedier/frp/releases/tag/v0.37.0


重要提示
1.检查FRP可执行文件 读取权限 无法运行 进行777设置
2.检查服务器防火墙端口开放规则
3.无法进行SYETEMD配置运行就使用老方案./frps -c frps.ini进行运行
4.此文档测试服务器为Debian 9

systemd配置

v0.25.2版本开始,frp提供了一份默认的systemd配置,也就是说我们可以通过systemd来管理frp了。

进入被解压的frp目录,会看到有一个systemd目录。里面有四个文件frpc.service[email protected]frps.service[email protected]

其中frps开头的文件用于服务端,frpc开头的文件用于客户端。带@符号的文件适用于一台服务器上多用户使用frp,这里我们主要以单用户使用来讲解。

第一步,服务端和客户端分别复制frps.servicefrpc.service/etc/systemd/system/目录下。

第二步,在被解压的frp目录中将frpsfrpc应用程序分别复制到服务端和客户端的/usr/bin/目录下。

第三步,在被解压的frp目录中将frps.inifrpc.ini配置文件分别复制到服务端和客户端的/etc/frp目录下,如果/etc/frp目录不存在,请手动创建mkdir /etc/frp

第四步,重新加载systemd配置:systemctl daemon-reload

第五步,设置开机自启动。在服务端使用systemctl enable frps,在客户端使用systemctl enable frpc

经过上面五个步骤,systemd的相关配置已经完成,但还没有正常启动,在完成相关配置后,我们再来启动它。

服务端配置

编辑/etc/frp/frps.ini文件。

# [common] is integral section
[common]
# 如果有多个IP,可以选择绑定到不同的ip上
bind_addr = 0.0.0.0
bind_port = 7000
# udp port to help make udp hole to penetrate nat
bind_udp_port = 7001

# 虚拟主机配置,不能和系统中已监听的端口冲突。http和https可以设置成同一个
vhost_http_port = 8080
vhost_https_port = 4433

# 服务端web面板
dashboard_addr = 0.0.0.0
dashboard_port = 7500
# 设置用户名密码,默认都是admin,请注意做修改
dashboard_user = admin
dashboard_pwd = admin

# 普罗米修斯运维服务,go语言相关监控,可以关闭
enable_prometheus = false

# 设置日志文件地址
log_file = /var/log/frps.log
# trace, debug, info, warn, error
log_level = info
log_max_days = 3
# disable log colors when log_file is console, default is false
disable_log_color = false
# DetailedErrorsToClient defines whether to send the specific error (with debug info) to frpc. By default, this value is true.
detailed_errors_to_client = true


# 最新版本支持的验证方式比较多,这里还是选用token模式
# AuthenticationMethod specifies what authentication method to use authenticate frpc with frps.
# If "token" is specified - token will be read into login message.
authentication_method = token
# AuthenticateHeartBeats specifies whether to include authentication token in heartbeats sent to frps. By default, this value is false.
authenticate_heartbeats = false
# AuthenticateNewWorkConns specifies whether to include authentication token in new work connections sent to frps. By default, this value is false.
authenticate_new_work_conns = false
# auth token 相当于密码,请注意保护
token = my_token

# 允许配置绑定的端口
# only allow frpc to bind ports you list, if you set nothing, there won't be any limit
allow_ports = 2000-3000,3001,3003,4000-50000

# pool_count in each proxy will change to max_pool_count if they exceed the maximum value
max_pool_count = 5

# max ports can be used for each client, default value is 0 means no limit
max_ports_per_client = 0

# TlsOnly specifies whether to only accept TLS-encrypted connections. By default, the value is false.
tls_only = false

# 子域名配置,取决于你使用的dns服务器是否支持泛域名解析,如果不支持,请不要填写。
# 如果填写了域名,客户端只需要填写子域名即可。例如客户端填写 client1,那么访问域名就是 client1.frps.com
subdomain_host = frps.com

# if tcp stream multiplexing is used, default is true
tcp_mux = true

# 定制404页面
# custom_404_page = /path/to/404.html

配置完成后,重启frps服务systemctl restart frps。如果没有报错,则说明配置是正确的。

如果配置了管理页面,也可以打开看看能否正常访问。

客户端配置

# [common] is integral section
[common]
# 服务端地址
server_addr = your_server_ip_or_domain
server_port = 7000

# 日志配置
log_file = /var/log/frpc.log
# trace, debug, info, warn, error
log_level = info
log_max_days = 3
# disable log colors when log_file is console, default is false
disable_log_color = false

# for authentication
token = mytoken

# 客户端配置web页面,配置后可以在web页面修改本地配置
admin_addr = 0.0.0.0
admin_port = 7400
admin_user = admin
admin_pwd = admin
# Admin assets directory. By default, these assets are bundled with frpc.
# assets_dir = ./static

# connections will be established in advance, default value is zero
pool_count = 5

# if tcp stream multiplexing is used, default is true, it must be same with frps
tcp_mux = true

# 客户端名称
user = my_client_name

# decide if exit program when first login failed, otherwise continuous relogin to frps
# default is true
login_fail_exit = true

# communication protocol used to connect to server
# now it supports tcp and kcp and websocket, default is tcp
protocol = tcp

# if tls_enable is true, frpc will connect frps by tls
tls_enable = false


# 下面的配置根据需要自行选择保留

[my_http_web]
type = http
local_ip = 127.0.0.1
local_port = 8080
use_encryption = false
use_compression = true
## 注意,如果服务端没有设置subdomain_host,那么subdomain就不会生效,也不需要填
subdomain = web01
## 可以额外设置定制域名,域名间以逗号分隔。
#custom_domains = web02.yourdomain.com
## http basic 认证,可以不用
http_user = admin
http_pwd = admin

[my_https_web]
type = https
local_ip = 127.0.0.1
local_port = 8081
use_encryption = false
use_compression = true
## 注意,如果服务端没有设置subdomain_host,那么subdomain就不会生效,也不需要填
subdomain = web02
## 可以额外设置定制域名,域名间以逗号分隔。
#custom_domains = web02.yourdomain.com


# SSH访问内网机器
[local-ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
use_encryption = false
use_compression = true

# 使用http访问https web 服务
[my_http2https_web]
type = http
## 注意,如果服务端没有设置subdomain_host,那么subdomain就不会生效,也不需要填
subdomain = web03
## 可以额外设置定制域名,域名间以逗号分隔。
#custom_domains = web02.yourdomain.com
plugin = http2https
plugin_local_addr = 127.0.0.1:443
plugin_host_header_rewrite = 127.0.0.1

nginx代理frp

有些服务器上装有nginx,nginx默认对外提供80端口。那怎么才能让frp的http服务也能共用80端口?
以上边frps的配置为例。我们使用的vhost_http_port是8080,subdomain_host是frps.com。那么nginx的配置应该是:

erver{
        listen 80;
        server_name *.frps.com;
        location / {
                proxy_pass http://127.0.0.1:8080;
                proxy_set_header Host $host;
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_hide_header X-Powered-By;
        }
        access_log off;
  }

这样,你就能使用正常的80端口来访问本地服务了。


Frps.ini

[common]
bind_port = 7000
# 虚拟主机配置,不能和系统中已监听的端口冲突。http和https可以设置成同一个↓
vhost_http_port = 8080
vhost_https_port = 4433
# 服务端web面板↓
dashboard_addr = 789cn.xyz
dashboard_port = 7500
# 设置用户名密码,默认都是admin,请注意做修改↓
dashboard_user = admin
dashboard_pwd = ****

进入目录

cd frp

运行FRPS

./frps -c frps.ini


Debian打开关闭防火墙

Debian原来用的是UFW防火墙,之前没接触过这种类型防火墙,这里记录一下简单的使用规则,后期在使用过程中慢慢完善UFW防火墙的使用操作方法;

安装ufw

apt-get install ufw

查看防火墙现有规则:

ufw status

开启/关闭防火墙:

ufw enable //开启
ufw disable //关闭

开启指定tcp或者udp端口:

ufw allow 22/tcp

同时开启tcp与udp端口:

ufw allow 445

删除53端口:

ufw delete allow 53

拒绝指定tcp或者udp端口:

allow/deny 20/tcp
allow/deny 20/udp

突出显示的输出表示网络接口名称。 它们通常被命名为eth0或enp3s2 。

因此,如果您的服务器具有名为eth0的公共网络接口,则可以使用以下命令允许HTTP流量(端口80 ):

sudo ufw allow in on eth0 to any port 80

这样做将允许您的服务器从公共互联网接收HTTP请求。
或者,如果您希望MySQL数据库服务器(端口3306 )监听专用网络接口eth1上的连接,例如,您可以使用此命令:

sudo ufw allow in on eth1 to any port 3306

这将允许专用网络上的其他服务器连接到MySQL数据库。