为了账号安全,请及时绑定邮箱和手机立即绑定

一文搞懂frp内网穿透并搭建配置使用

标签:
前端工具

实现内网穿需要有一台公网服务器、域名。(本文将公网服务器称为服务端,内网服务器称为客户端)

frp 是什么

frp 是一个专注于内网穿透的高性能的反向代理应用,支持 TCP、UDP、HTTP、HTTPS 等多种协议。可以将内网服务以安全、便捷的方式通过具有公网 IP 节点的中转暴露到公网。

有了内网穿透你能干什么?

  • 远程访问内网的 http/https 服务
  • 远程桌面(Windows/Mac)
  • 远程文件、 SSH
  • 小程序开发

有了内网穿透在外访问家里的电脑、NAS、树莓派、摄像头等网络设备或远程控制,那都不是事儿~

安装

官方项目地址:[https://github.com/fatedier/frp]

以下命令请在服务器中执行

下载

wget https://github.com/fatedier/frp/releases/download/v0.38.0/frp_0.38.0_linux_amd64.tar.gz

解压

tar -xvf frp_0.38.0_linux_amd64.tar.gz 

移动至/usr/local

mkdir /usr/local/frp
mv frp_0.38.0_linux_amd64/* /usr/local/frp/

文件说明

frps.ini: 服务端配置文件
frps: 服务端软件
frpc.ini: 客户端配置文件
frpc: 客户端软件

配置systemctl来控制,服务端运行

vim新建文件并写入配置内容

vim /usr/lib/systemd/system/frp.service

写入以下内容,注意上文移动放置的路径和此处有关。这里是启动的服务端

[Unit]
Description=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target

[Service]
Type=simple
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.ini
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
StandardOutput=syslog
StandardError=inherit

[Install]
WantedBy=multi-user.target

重新加载服务的配置文件

systemctl daemon-reload

现在就可以用 systemctl 套装来控制 frp 了。

启动/停止/重启,查看状态,设置开机自启/关闭开机自启

systemctl start frp
systemctl stop frp
systemctl restart frp
systemctl status frp
systemctl enable frp
systemctl disable frp

配置和使用

服务端

frps.ini

[common] #必须设置
bind_port = 7000 #是自己设定的frp服务端端口
vhost_http_port = 80 #是自己设定的http访问端口
token = 123  #核实身份用,加了更安全

[ssh] #ssh反向代理(不是必须设置)
listen_port = 6000 是自己设定的ssh访问端口

[web] #http反向代理[]里的内容可以自己设定,但是客户端和服务端必须要对应(如[aaa],[bbb]);
type = http #为服务类型,可以设为http,https
custom_domains = test1.a.com #为要映射的域名,记得域名的A记录要解析到外网主机的IP。

[web2] #同上(可设置多个)

示例

[common]
bind_port = 7000
vhost_http_port = 80

[ssh]
listen_port = 6000

[web]
type = http
custom_domains = test1.a.com

[web2]
type = http
custom_domains = test2.a.com

启动

./frps -c ./frps.ini

# 后台启动
nohup ./frps -c ./frps.ini &

客户端

frpc.ini

[common]
server_addr = 远程frp服务器ip
server_port = 远程frp服务器端口
token = 远程frp服务器token

[http]
type = http
local_ip = 127.0.0.1
local_port = 本地端口号
remote_port = 远程frp服务器的http服务端口号
custom_domains = 自定义配置的域名
subdomain = 匹配服务端配置的subdomain_host

示例

[common]
server_addr = xx.xx.xx.xx
server_port = 7000
token = 123

[web] 
type = http
custom_domains = xx.xx.xx.xx
local_ip = 127.0.0.1
local_port = 8080
custom_domains = xxx.xxx.xxx
subdomain = k2p

启动

./frpc -c ./frpc.ini

# 后台启动
nohup ./frpc -c ./frpc.ini &

访问内网http/https服务

客户端配置

[common]
server_addr = FRP服务端IP
server_port = 7000
vhost_http_port = 80 #可自定义
vhost_https_port = 443 #可自定义
token= 123

[web] 
type = http
local_ip = 127.0.0.1
local_port = 8080
custom_domains = 自定义域名

运行服务后访问 **custom_domains:vhost_http_port **

示例:http://123.123.123.123 80可以省略

连接内网SSH

客户端配置

[common]
server_addr = FRP服务端IP
server_port = 7000
token= 123

[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000

运行服务后使用 ssh 工具直接连接即可

ssh 用户名@服务端ip  -p 端口号
# ssh youhun@x.x.x.x -p 6000 (将 x.x.x.x 替换成公网服务器 IP 地址)

远程连接Windows电脑(RDP)

准备工作

1.服务器放开端口 3389也可以自定义,我这里用 7001

2.开启windows电脑的允许远程连接

桌面 -> 此电脑 -> 图标右键 -> 属性 -> 远程设置 -> 修改为允许远程连接到此计算机

Win10 以上系统直接设置中打开远程桌面即可

客户端配置

[common]
server_addr = FRP服务端IP
server_port = 7000
token= 123

[RDP]
type = tcp
local_ip = 127.0.0.1
local_port = 3389
remote_port = 7001

新建 RDP 连接

现在我们在外网任意一台电脑,搜索并打开windows远程桌面程序

输入 服务器IP:remote_port 例如:1.2.3.4:7001,就可以连接到内网的windows了。

在此过程中需要输入内网电脑的用户名和开机密码后才能进行远程操控。

设置开机自启

windows 设置开机自启的方法有很多,这里推荐使用winsw将frp注册为系统服务。

这个方法最稳定,这是我最终用的方法。

远程连接Mac电脑(VNC)

准备工作

1.服务器放开端口 5900

2.进入 Mac 的【系统偏好设置】->【共享】,勾选“屏幕共享”和“远程登录”,用户为“所有用户”

客户端配置

[common]
server_addr = FRP服务端IP
server_port = 7000
token = 123

[vnc]
type = tcp
local_ip = 127.0.0.1
local_port = 5900
remote_port = 5900
use_encryption = true
use_compression = true

新建 VNC 连接

连接成功后,输入 Mac 的用户名和密码就行

设置开机自启

1.进入内网 Mac 系统,执行下列操作

# 编辑自启动文件
touch ~/Library/LaunchAgents/frpc.plist
vim ~/Library/LaunchAgents/frpc.plist

2.frpc.plist 文件内容如下,注意文件中的 frpcfrpc.ini 路径,可以将这两个文件移到下方配置文件的路径下或者修改为你的路径

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC -//Apple Computer//DTD PLIST 1.0//EN
http://www.apple.com/DTDs/PropertyList-1.0.dtd >
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>frpc</string>
    <key>ProgramArguments</key>
    <array>
     <string>/usr/local/bin/frpc/frpc</string>
         <string>-c</string>
     <string>/usr/local/bin/frpc/frpc.ini</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

3.加载并生效

sudo chown root ~/Library/LaunchAgents/frpc.plist
sudo launchctl load -w ~/Library/LaunchAgents/frpc.plist

绑定域名

取消端口访问http服务

每次访问域名都需要加上端口很麻烦,可以设置 Nginx 转发

在客户端配置文件中设置域名,记得将域名解析到公网 IP

[web] 
type = http
local_ip = 127.0.0.1
local_port = 8080
custom_domains = test.iyouhun.com;

然后设置 Nginx 转发

...
server {
          listen 80; #监听http的80端口
          server_name test.iyouhun.com;  # 这是我们设置的域名
          access_log  logs/test.iyouhun.com.log  main; # 记录日志
          location / {
                  proxy_pass http://127.0.0.1:7001; # 转发到我们在frps.ini中设置的vhost_http_port端口
                  proxy_set_header    Host            $host:80;  #不添加这行时访问需要 域名:7001才能访问到内网的机器,如果加了就不再需要后面的端口,直接使用域名访问
                  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;
          }
  
}
...

配置泛域名(frp默认域名)

域名解析

解析两条A记录:frp.xxx.com*.frp.xxx.com,都指向你的云服务器ip,稍等一会,ping frp.xxx.com,如果能ping通说明解析成功

配置服务端

# frps.ini
[common]
bind_port = 7000
...
subdomain_host = frp.xxx.com

客户端使用

# frpc.ini
[web] 
type = http
local_ip = 127.0.0.1
local_port = 8080
# 与frps中的subdomain_host连接起来就是 test.frp.xxx.com
subdomain = test #自定义域名前缀

访问域名:test.frp.xxx.com:vhost_http_port

注:如果 frps 配置了 subdomain_host,则 custom_domains 中不能是属于 subdomain_host 的子域名或者泛域名。

配置Nginx

还是老样子,每次都加端口访问太麻烦了,因此使用 Nginx 做下转发

server {
	# 监听的80端口
	listen 80;
	# 域名配置 记得一定要加上*.frp.xxx.com这个,只加frp.xxx.com是不行的
	server_name * .frp.xxx.com frp.xxx.com;
	location / {
		proxy_pass http: //127.0.0.1:8080;
		# 这个Host的header一定要加,不然转发后frp拿不到通过哪个域名访问的,导致转发失败
		proxy_set_header Host $host;
	}
}

相关问题

启动不了

服务端和客户端启动不了的情况下,并且没有报错误,有概率是因为文件没有执行权限

# 赋值权限命令
chomd 777 文件名

服务端启动报错 Exec format error

/frpc: cannot execute binary file: Exec format error

这是下载错文件了,下载的 FRP 与操作系统处理器架构不匹配,需要重新下载正确的文件。

可以使用 archuname -a 等命令查看系统架构。

arch
#x86_64

vue 项目出现Invalid Host header解决办法

// vue.config.js文件中增加disableHostCheck属性

module.exports = {
  devServer: {
    disableHostCheck: true
  }
}
点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
Web前端工程师
手记
粉丝
27
获赞与收藏
179

关注作者,订阅最新文章

阅读免费教程

  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消