今天用frp工具搭建了内网穿透服务,可以在外网访问内网网站了。
需求:
1.我想在任何地方访问家里的服务器的文件,
2.我想在博客中调用内网的大文件,这样的话,对于这些大文件,就可以存放在内网,而不需要上传到网站服务器,节省服务器空间。
3.我想在任何地方都可以维护家里的网络环境
做法:
使用frp工具,服务端放在阿里云服务器里,客户端运行在exsi虚拟机下的centos里或者windows里,这里只需要有一处运行即可。
下载地址:Releases · fatedier/frp · GitHub
解压后,将文件夹里面的文件放到 /root/frp 目录下
服务端配置:
将 frps.ini 文件修改为如下内容
1
2
3
4
5
6
7
|
[common]
bind_port = 5443
vhost_http_port = 808
vhost_https_port = 909
dashboard_user = admin
dashboard_pwd = admin
token=yourpassword
|
这里的5443是frp服务端口,808是http服务端口,909是https服务端口。之所以这样做,是因为我的服务器本身用了宝塔面板搭建了网站,因此为了错开80端口和443端口进行了这样的设置。
以上端口需要在宝塔面板中打开防火墙放行,也需要在阿里云管理面板中放行。
然后做域名解析,将需要的域名A记录解析到阿里云服务器上。比如把nas.abc.com解析到了服务器上。
在宝塔面板中新建网站,输入刚才解析的域名nas.abc.com,建立的时候只需要修改一个地方,即PHP版本处,修改为“静态”。然后在“反向代理”处进行修改,对于访问http的话,修改为 “http://127.0.0.1:808”,访问https的话修改为“https://127.0.0.1:909”。
服务端配置完毕。
SSH进入服务器,输入
1
|
./frps -c 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
|
[common]
server_addr = 替换为你的服务器IP地址
server_port = 5443
tls_enable = true
token=yourpassword
[EXSI]
type = https
local_ip = 192.168.5.200
local_port = 80
remote_port = 909
custom_domains = exsi.abc.com
[NAS]
type = http
local_ip = 192.168.5.189
local_port = 5000
remote_port = 808
custom_domains = nas.abc.com
[localblog]
type = http
local_ip = 192.168.5.188
local_port = 8888
remote_port = 808
custom_domains = localblog.taholab.com
[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
|
以上的内网IP地址、端口和域名相应的替换为你的即可。
[common] 部分为客户端的初始化板块,包括了服务端的ip地址,服务端口,tls是否启动,token密码等内容,一次设置,全局有效。
除了[common]部分外,以上配置文件中,每一个 [ ] 板块下的一段,对应着一个你需要内网穿透的应用。如果你只有一个网站需要内网穿透,则只保留一个板块就可以。里面的 [ ] 是服务标识符,其下对应的代码含义如下:
- type 表示服务类型,包括了http、https、tcp、udp等服务,比如搭建网站的话就用http或https服务。
- local_ip 表示你的内网网站的ip地址,比如我的测试博客 localblog.taholab.com 在内网访问就可以输入 http://192.168.5.188:端口号 来访问。端口号就是你的内网网站设置的端口号,默认是80。
- local_port 表示你的内网网站的网站端口,默认是80,你也可以改成你要的端口号,比如上述的 localblog.taholab.com 如果把网站端口设置为了8888,那么内网网站访问地址就是:http://192.168.5.189:8888
- remote_port 表示frp服务中http网站所用的端口,本配置文件用的808,服务端也是808,如果是https网站的话,那么就得用909(因为服务端配置的是909)。
- custom_domains 是访问该服务的域名,只对网站有用,tcp服务不需要这一项。以 localblog.taholab.com 为例,这里就输入 localblog.taholab.com 。
配置完毕后保存。然后运行
1
|
./frpc -c frpc.ini
|
无误的话就会启动客户端,于服务端建立连接。举例如下:
如果在浏览器访问 http://nas.abc.com:808,则会打开内网的http://192.168.5.189:5000。
所有数据基本上都是从服务器转发的,因此会受限于服务器的带宽以及流量上限。我的服务器一个月2000Gb浏览,30Mbps带宽,勉强够用。
已经可以访问NAS了
下面就是设置开机自启教程
虽然可以通过frp自带的服务文件来做,但事实发现其实不如手动配置来的靠谱,因为还需要做一些修改,所以不如直接手动添加服务。
首先做好frp服务配置,服务端和客户端案例分别如下:
服务端配置和自动设置
创建服务脚本并编辑 vi /etc/systemd/system/frps.service
内容如下:(注意,代码中的frps.ini是服务端配置文件,请依据自己的实际情况做相应修改)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[Unit]
Dcription=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
User=root
Restart=on-failure
ExecStart=/root/frp/frps -c /root/frp/frps.ini
ExecReload=/root/frp/frps -c /root/frp/frps.ini
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
StandardOutput=syslog
StandardError=inherit
[Install]
WantedBy=multi-user.target
|
加入服务列表并设置自启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#重新加载服务列表
systemctl daemon-reload
#将frps加入开机自启动
systemctl enable frps.service
#启动服务
systemctl start frps.service
#以下是其他常用命令
#关闭服务
systemctl stop frps.service
#重启服务
systemctl restart frps.service
#显示服务的状态
systemctl status frps.service
#禁用服务开机启动
systemctl disable frps.service
#查看服务是否开机启动
systemctl is-enabled frps.service
#查看已经启动的服务列表
systemctl list-unit-files|grep enable
#查看启动失败的服务列表
systemctl --failed配置和自启动设置
|
客户端配置和自动设置
客户端与服务端配置方法完全一致,代码如下:
创建服务脚本并编辑 vi /etc/systemd/system/frpc.service
内容如下:(注意,代码中的frpc.ini是服务端配置文件,请依据自己的实际情况做相应修改)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
[Unit]
Dcription=The nginx HTTP and reverse proxy server
After=network.target remote-fs.target nss-lookup.target
[Service]
Type=simple
User=root
Restart=on-failure
ExecStart=/root/frp/frpc -c /root/frp/frpc.ini
ExecReload=/root/frp/frpc -c /root/frp/frpc.ini
KillSignal=SIGQUIT
TimeoutStopSec=5
KillMode=process
PrivateTmp=true
StandardOutput=syslog
StandardError=inherit
[Install]
WantedBy=multi-user.target
|
加入服务列表并设置自启动
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
#重新加载服务列表
systemctl daemon-reload
#将frpc加入开机自启动
systemctl enable frpc.service
#启动服务
systemctl start frpc.service
#以下是其他常用命令
#关闭服务
systemctl stop frpc.service
#重启服务
systemctl restart frpc.service
#显示服务的状态
systemctl status frpc.service
#禁用服务开机启动
systemctl disable frpc.service
#查看服务是否开机启动
systemctl is-enabled frpc.service
#查看已经启动的服务列表
systemctl list-unit-files|grep enable
#查看启动失败的服务列表
systemctl --failed
|