前言
内网穿透的原理我就不多说了哈,既然会看到我这篇文章,想必都知道内网穿透是做什么的吧
frp分为服务端和客户端,服务端一般是搭在公网服务器中,客户端一般搭在本地或者局域网,需要提前在服务端搭好ftp server,然后客户端搭建frp client,与服务端保持连接
远程用户一般是访问不到本地内网的,所以需要访问服务端,服务端通过frp转发到内网中来
本教程是用docker搭建的frp,阅读本文需要有一定的docker基础,当然windows也可以操作,原理是一样的
初次玩frp建议先把防火墙和安全组关了,以免遇到端口不能访问的问题
搭建frp服务端
在公网服务器中新建配置文件
mkdir -p /data/frps
vim /data/frps/frps.toml
配置文件内容
[common]
# frp客户端连接端口
bind_port = 7000
# 管理页面的端口
dashboard_port = 7500
# 管理页面的用户名
dashboard_user = admin
# 管理页面的密码
dashboard_pwd = 123456
# 客户端连接的令牌
token = 123456
拉取frp server镜像
docker pull snowdreamtech/frps:0.62
运行容器
docker run -d \--name=frps \--network=host \--restart=always \-v /data/frps:/etc/frp \snowdreamtech/frps:0.62
访问http://你的公网ip:7500即可进入管理端页面,账号密码就是配置文件中的账号密码
搭建frp客户端,代理tcp
我们先启动两个内网服务,让它可以正常访问
在内网服务器中新建配置文件
mkdir -p /data/frpc
vim /data/frpc/frpc.toml
配置文件内容
[common]
server_addr = 你的公网ip
# 公网服务端的端口
server_port = 7000
# 公网服务器配置的token
token = 123456# 代理一个端口,通过公网服务器的8080端口访问到内网的80端口
[web80]
# 代理类型
type = tcp
# 公网服务器的端口
remote_port = 8080
# 内网ip
local_ip = 192.168.200.100
# 内网端口
local_port = 80# 通过公网的8081端口访问到内网的81端口
[web81]
type = tcp
remote_port = 8081
local_ip = 192.168.200.100
local_port = 81
拉取frp client镜像
docker pull snowdreamtech/frpc:0.62
运行容器
docker run -d \--name=frpc \--network=host \--restart=always \-v /data/frpc:/etc/frp \snowdreamtech/frpc:0.62
如果本地是windows系统,可以下载windows客户端
下载地址:https://github.com/fatedier/frp/releases
启动方式也很简单,直接frpc.exe -c frpc.toml即可
我们到管理页面就能看到客户端代理信息了
这时候就可以通过你的公网ip去访问你的内网了
代理http
使用前必读,非常重要,可以让你少走很多弯路:
所有http代理,在服务器中都是共用一个端口,通过域名来区分不同的http服务,所以代理http一定要有域名,没有域名的话只能使用tcp的方式代理,tcp方式也能代理http
我们提前准备好域名,然后解析到你的公网服务器中
服务端frps.toml增加一句配置,定义http端口
vhost_http_port = 8088
注意:多个不同的http代理将会共用这个端口
完整配置:
[common]
bind_port = 7000
dashboard_port = 7500
dashboard_user = admin
dashboard_pwd = 123456
token = 123456
vhost_http_port = 8088
客户端frpc.toml配置:
[common]
server_addr = 118.31.42.134
# 公网服务端的端口
server_port = 7000
# 公网服务器配置的token
token = 123456[web80]
# 代理类型
type = http
# 代理类型为http时,remote_port字段无效,远程端口默认是服务端配的vhost_http_port
# remote_port = 8080
# 内网ip
local_ip = 192.168.200.100
# 内网端口
local_port = 80
# 域名
custom_domains = test.linzhehao.cn
就可以通过域名访问你的内网了
代理https
申请SSL证书并审核,阿里云、腾讯云等厂家都可以申请免费证书
选择nginx证书进行下载,下载后解压,会有一个.key和.pem文件
将证书文件上传到内网服务器的/data/frpc/cert
服务端frps.toml增加一句配置,定义https端口,所有的https请求都会走这个端口
vhost_https_port = 443
完整配置:
[common]
bind_port = 7000
dashboard_port = 7500
dashboard_user = admin
dashboard_pwd = 123456
token = 123456
vhost_http_port = 8088
vhost_https_port = 443
客户端frpc.toml配置:
[common]
server_addr = 你的公网ip
# 公网服务端的端口
server_port = 7000
# 公网服务器配置的token
token = 123456[web1]
type = https
# https中,local_ip和local_port字段无效
# local_ip = 192.168.200.100
# local_port = 80
custom_domains = test.linzhehao.cn
plugin = https2http
# 配置证书的路径
plugin_crt_path = /etc/frp/cert/test.linzhehao.cn.pem
plugin_key_path = /etc/frp/cert/test.linzhehao.cn.key
plugin_host_header_rewrite = test.linzhehao.cn
# 代理的地址
plugin_local_addr = 192.168.200.100:80
重启一下frpc
docker restart frpc
就可以用https访问了
子域名
简介:
如果有多个子域名,比如test1.linzhehao.cn、test2.linzhehao.cn,如果不使用子域名的话,那么custom_domains每次都需要写上完整的域名,使用子域名的话只需要在服务端配上linzhehao.cn,在客户端配置test1、test2即可
frps.toml中添加:
subdomain_host = linzhehao.cn
完整配置:
[common]
bind_port = 7000
dashboard_port = 7500
dashboard_user = admin
dashboard_pwd = 123456
token = 123456
vhost_http_port = 8088
vhost_https_port = 443
subdomain_host = linzhehao.cn
客户端frpc.toml配置:
[common]
server_addr = 你的公网ip
# 公网服务端的端口
server_port = 7000
# 公网服务器配置的token
token = 123456[web80]
# 代理类型
type = http
# 内网ip
local_ip = 192.168.200.100
# 内网端口
local_port = 80# 如果想使用子域名,需要去掉custom_domains
# custom_domains = test.linzhehao.cn# subdomain会自动拼上frps.toml中的subdomain_host
subdomain = test
成功访问