一、frp的作用
- 利用处于内网或防火墙后的机器,对外网环境提供 http 或 https 服务
- 对于 http, https 服务支持基于域名的虚拟主机,支持自定义域名绑定,使多个域名可以共用一个80端口
- 利用处于内网或防火墙后的机器,对外网环境提供 tcp 和 udp 服务,例如在家里通过 ssh 访问处于公司内网环境内的主机
- 本次实例为:远程访问开发板web服务或ssh,如树莓派,NanoPi等
二、配置说明
1、实现功能
(1)外网通过ssh访问内网NanoPi
(2)自定义绑定域名访问内网NanoPi的web服务
2、配置前准备
(1)公网服务器1台
(2)内网服务器1台(NanoPi开发板)
(3)公网服务器绑定域名1个(实现穿透ssh功能不需要公网服务器绑定域名,穿透web服务功能必须需要公网服务器绑定域名)
(4)内网服务器(NanoPi)部署一个web服务,本次为LAMP环境
三、安装frp
(1)公网服务器安装服务端
1 2 3 |
wget https://github.com/fatedier/frp/releases/download/v0.16.1/frp_0.16.1_linux_amd64.tar.gz tar -zxvf frp_0.16.1_linux_amd64.tar.gz cd frp_0.16.1_linux_amd64 |
(2)配置公网服务
1 2 3 4 5 6 7 8 9 10 11 |
vim frps.ini [common] #与客户端绑定的进行通信的端口 bind_port = 10086 #访问客户端web服务自定义的端口号 vhost_http_port = 10087 前台启动 ./frps -c frps.ini 后台启动 ./frps -c frps.ini >> frps.log 2>&1 & |
(3)内网NanoPi安装客户端
1 2 3 |
wget https://github.com/fatedier/frp/releases/download/v0.16.1/frp_0.16.1_linux_arm.tar.gz tar -zxvf frp_0.16.1_linux_arm.tar.gz cd frp_0.16.1_linux_arm |
(4)配置内网服务
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 |
vim frpc.ini [common] #公网服务器ip server_addr = xx.xx.xx.xx #与服务端bind_port一致 server_port = 10086 #公网通过ssh访问内部服务器 [ssh] #连接协议 type = tcp #内网服务器ip local_ip = 192.168.1.117 #ssh默认端口号 local_port = 22 #自定义的访问内部ssh端口号 remote_port = 10088 #公网访问内部web服务器以http方式 [web] #访问协议 type = http #内网web服务的端口号 local_port = 80 #所绑定的公网服务器域名,一级、二级域名都可以 custom_domains = lostphp.com 前台启动 ./frpc -c ./frpc.ini 后台启动 ./frpc -c ./frpc.ini >> frpc.log 2>&1 & |
四、访问方式
(1)外网ssh访问内网NanoPi服务器
直接用XShell等工具或ssh命令访问 地址:公网ip:10088,输入开发板的用户名密码即可
(2)自定义绑定域名访问内网web服务
test.lostphp.com:10087
五、配置文说明
(1)服务端frps.ini
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
[common] #frp服务器监听地址,如果是IPV6地址必须用中括号包围 bind_addr = 0.0.0.0 #frp服务器监听端口 bind_port = 7000 #kcp的udp监听端口,如果不设那就不启用 #kcp_bind_port = 7000 #指定使用的协议,默认tcp,可选kcp #protocol = kcp #如果要使用vitual host,就必须设置 #vhost_http_port = 80 #vhost_https_port = 443 #Web后台监听端口 dashboard_port = 7500 #Web后台的用户名和密码 dashboard_user = admin dashboard_pwd = admin #Web后台的静态资源目录,调试用的,一般不设 #assets_dir = ./static #日志输出,可以设置为具体的日志文件或者console log_file = /var/log/frps.log #日志记录等级,有trace, debug, info, warn, error log_level = info #日志保留时间 log_max_days = 3 #启用特权模式,从v0.10.0版本开始默认启用特权模式,且目前只能使用特权模式 #privilege_mode = true #特权模式Token,请尽量长点且复杂 privilege_token = 12345678 #特权模式允许分配的端口范围 privilege_allow_ports = 2000-3000,3001,3003,4000-50000 #心跳超时 #heartbeat_timeout = 90 #每个代理可以设置的连接池上限 #max_pool_count = 5 #认证超时时间 #authentication_timeout = 900 #如果配置了这个,当你的模式为http或https时,就能设置子域名subdomain #subdomain_host = frps.com #是否启用tcp多路复用,默认就是true,不用管 #tcp_mux = true |
(2)客户端frpc.ini
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
[common] #frp服务器地址 server_addr = 1.2.3.4 #frp服务器端口 server_port = 7000 #特权模式Token privilege_token = 12345678 #转发SSH [ssh] type = tcp #可以指定为其它IP,默认是本地 #local_ip = 127.0.0.1 local_port = 22 remote_port = 6000 #启用加密 use_encryption = true #启用压缩 use_compression = true #转发Web [web] type = http local_port = 80 custom_domains = www.lostphp.com #修改header中的host #host_header_rewrite = dev.lostphp.com #启用简单HTTP认证 #http_user = abc #http_pwd = abc #在服务端配置了subdomain_host的情况下用于自定义二级域名 #subdomain = test #在存在多个相同域名的情况下通过请求的URL路由到不同的配置 #locations = /news,/about #转发DNS请求 [dns] type = udp local_ip = 8.8.8.8 local_port = 53 remote_port = 6000 #转发Unix域套接字(这儿是Docker) [unix_domain_socket] type = tcp remote_port = 6000 plugin = unix_domain_socket plugin_unix_path = /var/run/docker.sock #HTTP代理 [http_proxy] type = tcp remote_port = 6000 plugin = http_proxy #配置http代理的简单认证 #plugin_http_user = abc #plugin_http_passwd = abc |
六、注意事项
注意:客户端的时钟不能和服务器端时钟相差太大!否则会出现认证超时的错误:authorization timeout
这个超时时间可以在配置文件中通过 authentication_timeout 这个参数来修改,单位为秒,默认值为 900,即 15 分钟。如果修改为 0,则 frps 将不对身份验证报文的时间戳进行超时校验。
使用命令:date 查看当前计算机时间
使用命令:date -s “YYYY-MM-DD HH-MM-SS” 设置时间