frp内网穿透服务搭建

类似于花生壳,ngrok内网穿透,可以本地调试微信开发,搭建步骤十分简单,只需要一台公网服务器即可

FRP服务端

下载解压

1
2
3
4
# 下载
wget https://github.com/fatedier/frp/releases/download/v0.33.0/frp_0.33.0_linux_amd64.tar.gz
# 解压
tar -zxvf frp_0.33.0_linux_amd64.tar.gz

移动位置

1
mv frp_0.33.0_linux_amd64 /usr/local

配置文件

打开配置文件

1
vi /usr/local/frp_0.33.0_linux_amd64/frps.ini

参考配置如下

1
2
3
4
5
6
7
8
9
10
11
[common]
# frp监听的端口,用作服务端和客户端通信
bind_port = 7000

# 服务端通过此端口接监听和接收公网用户的http请求
vhost_http_port = 7071

# 在多人同时使用一个 frps 时,通过自定义二级域名的方式来使用会更加方便。
# 通过在 frps 的配置文件中配置 subdomain_host,就可以启用该特性。之后在 frpc 的 http、https 类型的代理中可以不配置 custom_domains,而是配置一个 subdomain 参数。
# 只需要将 *.{subdomain_host} 解析到 frps 所在服务器。之后用户可以通过 subdomain 自行指定自己的 web 服务所需要使用的二级域名,通过 {subdomain}.{subdomain_host} 来访问自己的 web 服务。
subdomain_host = frp.itfuyun.com

启动

1
2
3
4
5
6
# 进入目录
cd /usr/local/frp_0.33.0_linux_amd64
# 启动
nohup ./frps -c frps.ini &
# 查看日志
tail -f nohup.out

停止

1
2
3
4
5
# 查看进程
ps -ef |grep frp

# 使用kill命令杀掉
kill -9 [pid]

FRP客户端

下载解压

地址:https://github.com/fatedier/frp/releases

我是在windows下使用,选择frp_0.33.0_windows_amd64.zip下载解压

配置文件

进入解压目录,打开frpc.ini

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
[common]
# frp服务端的公网服务器ip
server_addr = frp服务器IP
# 和服务端的bind_port保持一致
server_port = 7000


# 代理服务一 ,[]内的代理服务名称在全局范围内确保唯一,每个人的每个代理服务不能重名,
# 否则会影响正常使用。
[web01]
type = http
# local_port代表你想要暴露给外网的本地web服务端口
local_port = 8080
# subdomain 在全局范围内要确保唯一,每个代理服务的subdomain不能重名,否则会影响正常使用。
# 客户端的subdomain需和服务端的subdomain_host配合使用
# test1.frp.itfuyun.com相当于访问本地8080端口
subdomain = test1

启动

在解压目录打开cmd或者其他命令工具,执行如下命令即可

1
.\frpc.exe -c .\frpc.ini

Nginx反向代理

将泛域名 *.frp.itfuyun.com 解析到 frp 部署的服务器的 IP 地址,nginx安装和配置可以参考另一篇文章

nginx配置

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
server {
listen 80;
server_name *.frp.itfuyun.com;

#charset koi8-r;
#access_log /var/log/nginx/host.access.log main;

location / {
proxy_pass http://127.0.0.1:7071;
proxy_set_header Host $host:80;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;

proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";

proxy_connect_timeout 7d;
proxy_send_timeout 7d;
proxy_read_timeout 7d;

}

#error_page 404 /404.html;

# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}

# proxy the PHP scripts to Apache listening on 127.0.0.1:80
#
#location ~ \.php$ {
# proxy_pass http://127.0.0.1;
#}

# pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000
#
#location ~ \.php$ {
# root html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$fastcgi_script_name;
# include fastcgi_params;
#}

# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
#location ~ /\.ht {
# deny all;
#}
}

防止白嫖

按以上配置,如果有人直接配置就可以免费使用你的内网穿透了,为了防止这样,我们需要加个验证,在服务端配置文件[common]中增加token选项,这样客户端也需要有token才能使用

服务端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
[common]
# frp监听的端口,用作服务端和客户端通信
bind_port = 7000

# 服务端通过此端口接监听和接收公网用户的http请求
vhost_http_port = 7071
# token值
token = 123456

# 在多人同时使用一个 frps 时,通过自定义二级域名的方式来使用会更加方便。
# 通过在 frps 的配置文件中配置 subdomain_host,就可以启用该特性。之后在 frpc 的 http、https 类型的代理中可以不配置 custom_domains,而是配置一个 subdomain 参数。
# 只需要将 *.{subdomain_host} 解析到 frps 所在服务器。之后用户可以通过 subdomain 自行指定自己的 web 服务所需要使用的二级域名,通过 {subdomain}.{subdomain_host} 来访问自己的 web 服务。
subdomain_host = frp.itfuyun.com

客户端配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[common]
# frp服务端的公网服务器ip
server_addr = frp服务器IP
# 和服务端的bind_port保持一致
server_port = 7000
# token值,与服务端保持一致
token = 123456

# 代理服务一 ,[]内的代理服务名称在全局范围内确保唯一,每个人的每个代理服务不能重名,
# 否则会影响正常使用。
[web01]
type = http
# local_port代表你想要暴露给外网的本地web服务端口
local_port = 8080
# subdomain 在全局范围内要确保唯一,每个代理服务的subdomain不能重名,否则会影响正常使用。
# 客户端的subdomain需和服务端的subdomain_host配合使用
# test1.frp.itfuyun.com相当于访问本地8080端口
subdomain = test1

参考

掘金https://juejin.im/post/6844903766701899784#heading-1

官方文档https://github.com/fatedier/frp/blob/master/README_zh.md

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×