使用 frp 进行内网穿透

全 docker 部署使用 frp 进行内网穿透,尝试了下速度还不错,延迟可以接受

配置 DNS

需要自己有一个公网服务器,这里我的 SSL 证书是通过 cloudflare 进行配置的,所以我的全部连接是通过 80 端口

配置几个域名,分别是连接 frp 的域名 (比如 frp.example.com ),然后是自己需要代理的服务 (比如 server1.example.com, server2.example.com) 指向服务器,注意连接 frp 的域名 (frp.example.com) 不能启用 cloudflare 代理,也就是使用仅 DNS 模式

服务端

在服务器上安装 frp 较为简单,准备配置文件 frps.toml

1
2
3
4
5
bindPort = 7000
vhostHTTPPort = 8000

auth.method = "token"
auth.token = "set_your_token"

设置一个复杂的 token 然后是 docker-compose.yml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
services:
  frps:
    image: snowdreamtech/frps:latest
    container_name: frps
    restart: unless-stopped
    ports:
      - "7000:7000"
      - "8000:8000"
    volumes:
      - ./frps.toml:/etc/frp/frps.toml

直接启动即可

1
docker compose up -d

配置 Nginx

将连接 frp 的域名 (frp.example.com) 反向代理至 bindPort = 7000 也就是 172.17.0.1:7000

然后将所有的服务域名都反向代理至 vhostHTTPPort = 8000 也就是 172.17.0.1:8000

因为使用 cloudflare 代理,所以不能只使用一个 nginx 配置 server_name 为这些域名,不然只有第一个域名会生效,所以每个域名需要单独配置,但每个都反向代理至 172.17.0.1:8000

客户端

客户端的配置较为复杂,配置文件 frpc.toml

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
serverAddr = "frp.example.com"
serverPort = 80
transport.protocol = "websocket"

auth.method = "token"
auth.token = "set_your_token"

[[proxies]]
name = "nas-server1"
type = "http"
localIP = "192.168.6.x"
localPort = 8001
customDomains = ["server1.example.com"]

[[proxies]]
name = "nas-server2"
type = "http"
localIP = "192.168.6.x"
localPort = 8002
customDomains = ["server2.example.com"]

然后是 docker-compose.yml

1
2
3
4
5
6
7
8
services:
  frpc:
    image: snowdreamtech/frpc:latest
    container_name: frpc
    restart: unless-stopped
    network_mode: "host"
    volumes:
      - "./frpc.toml:/etc/frp/frpc.toml"

直接启动即可

1
docker compose up -d
This post is licensed under CC BY-NC-SA 4.0 by the author.

访问统计

自 2025-02-08 起的访问统计

使用 Hugo 构建 | 主题 StackJimmy 设计 | 修改自 yexca