frp 与 caddy 实现内网穿透自动小绿锁 ¶
准备环境 ¶
服务器环境 | 公网IP | 内网IP | 所需软件 | 系统 |
---|---|---|---|---|
公网 | linuxnbg.com | docker、frps、caddy | Ubuntu20.04 | |
内网 | 192.168.1.99 | docker、frpc、caddy | Ubuntu20.04 |
公网服务器 与 内网 安装 软件 ¶
1. 安装 caddy ¶
apt install -y debian-keyring debian-archive-keyring apt-transport-https \
&& curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/gpg.key' | sudo gpg --dearmor -o /usr/share/keyrings/caddy-stable-archive-keyring.gpg \
&& curl -1sLf 'https://dl.cloudsmith.io/public/caddy/stable/debian.deb.txt' | sudo tee /etc/apt/sources.list.d/caddy-stable.list \
&& sudo apt update \
&& sudo apt install caddy \
&& caddy list-modules | grep dnspod \
&& [ $? -eq 0 ] || (echo "不存在, 正在安装" ; caddy add-package github.com/caddy-dns/dnspod ; systemctl restart caddy)
2. docker-compose 启动 frp ¶
1. 编写服务端 compose.yaml 文件 ¶
services:
frps:
container_name: frps
image: swr.cn-east-3.myhuaweicloud.com/china/fxdl/frps:0.52.2
restart: always
network_mode: "host"
volumes:
- ./frps.toml:/etc/frp/frps.toml
2. 编写服务端 frps 配置文件 frps.toml ¶
bindPort=40000
auth.token="11111111111111111111"
webServer.addr = "0.0.0.0"
webServer.port = 10000
# dashboard 用户名密码,可选,默认为空
webServer.user = "datarc"
webServer.password = "datarc"
3. 启动 frps 服务 ¶
docker compose up -d
1. 编写客户端 compose.yaml 文件 ¶
services:
frpc:
container_name: frpc
image: swr.cn-east-3.myhuaweicloud.com/china/fxdl/frpc:0.52.2
restart: always
network_mode: "host"
volumes:
- ./frpc.toml:/etc/frp/frpc.toml
2. 编写服务端 frpc 配置文件 frpc.toml ¶
serverAddr = "linuxnbg.com"
serverPort = 40000
auth.method = "token"
auth.token = "11111111111111111111"
transport.useEncryption = true #加密,还有更多参数,可以参考官网
transport.useCompression = true #压缩
[[proxies]]
name = "ubuntu.linuxnbg.com"
type = "tcp"
localIP = "192.168.1.99"
localPort = 22
remotePort = 11111
[[proxies]]
name = "wordpress.linuxnbg.com"
type = "tcp"
localIP = "192.168.1.99"
localPort = 8080
remotePort = 18080
3. 启动 frpc 服务 ¶
docker compose up -d
目前实现 ¶
Wordpress 访问链路
这时用户通过公网访问内网服务 linuxnbg.com:18080
→ 内网机器 192.168.1.99:8080
- 这时用户可以通过 公网域名
http://linuxnbg.com:18080
来问访问线下内网服务192.168.1.99:8080
,但是http://域名:端口
,不符合网友的访问习惯。 - 好的域名是便于记忆输入方便安全的短网址,类似于
https://wordpress.linuxnbg.com
,https
最常见的架构是Nginx+证书
。但是申请证书繁琐。用caddy
服务代替,可以实现自动https
或泛域名小绿锁。
服务端编写 Caddyfile ¶
1. 编写 Caddyfile ¶
cat > /etc/caddy/Caddyfile << 'EOF'
wordpress.linuxnbg.com {
tls linuxnbg@163.com
reverse_proxy 127.0.0.1:7777
}
EOF
2. 启动服务 ¶
systemctl enable caddy && systemctl restart caddy
此时实现的效果 ¶
访问链路
这时用户访问 wordpress.linuxnbg.com
→ 跳到https://wordpress.linuxnbg.com
→ 公网机器127.0.0.1:18080
→ 内网机器 192.168.1.2:8080