- 发布时间
内网穿透
| 工具 | 核心特点 | 适用场景 | 技术门槛 |
|---|---|---|---|
| frp | 轻量、高性能,支持 TCP/UDP/HTTP/HTTPS,支持自定义域名、加密传输 | 开发测试、内网服务暴露 | 中等 |
| nps | 功能丰富,支持集群、用户管理、流量统计,适合企业 / 多设备场景 | 多设备穿透、团队共享 | 中等 |
| ngrok2(衍生版) | 基于 ngrok 改进,支持自定义服务器,适合需要 HTTPS 的场景 | 本地 HTTPS 服务测试、小程序开发 | 中等 |
FRP
不建议使用容器,使用本机端口号映射容器端口,就不会有端口问题
安装
// 获取资源
wget https://github.com/fatedier/frp/releases/download/v0.58.1/frp_0.58.1_linux_amd64.tar.gz
# 资源解压
tar -zxvf frp_0.58.1_linux_amd64.tar.gz
cd frp_0.58.1_linux_amd64
确保服务端和客户端的版本一致
配置
配置服务端frps:/root/frp_0.58.1_linux_amd64/frps.toml
# 创建文件
nano frps.toml
# 编辑配置
bindPort = 7000
auth.token = "you_stronge_token"
# 设置二级域名的主域
subdomainHost = "xxxxxxx.com"
vhostHTTPPort = 8080 # 用于HTTP协议的端口(可选,但建议保留)
vhostHTTPSPort = 8443 # 用于HTTPS协议的端口
# 可选:Web仪表板配置
webServer.addr = "0.0.0.0"
webServer.port = 7500 # 用来在启动服务后登录IP查看 "xxx:xxx:xxx:xxx:7500"
webServer.user = "admin"
webServer.password = "admin"
客户端frpc:/home/Frp/frp_0.58.1_linux_amd64/frpc.toml
# frpc.toml
serverAddr = "xxx.xxx.xxx.xxx" # 公网ip
serverPort = 7000
auth.token = "you_stronge_token" # 必须与服务端一致
# 配置 next.huixiangwuyou.com 映射到本地的 3000 端口
[[proxies]]
name = "next-app"
type = "https"
localPort = 5678 # 代理容器3000
subdomain = "next" # 访问 next.huixiangwuyou.com
# 配置 fast.huixiangwuyou.com 映射到本地的 8000 端口
[[proxies]]
name = "fast-api"
type = "https"
localPort = 8123
subdomain = "fast" # 访问 fast.huixiangwuyou.com
# 你可以继续按照此格式添加其他子域名服务...
# 例如,为 dev.huixiangwuyou.com 添加配置
[[proxies]]
name = "market"
type = "https"
localPort = 443
# 若是服务端开启了二级域名功能customDomains 不能使用,只能用subdomain
customDomains = ["market.huixiangwuyou.com"]
建议直接使用 http 模式,若是使用https模式还要考虑容器和配置,nginx配置直接使用http的80端口
使用https方式
[[proxies]]
name = "nginx_https"
type = "https" # 代理类型是 https
customDomains = ["your-domain.com"]
[proxies.plugin]
type = "https2https" # 插件类型是 https2https
localAddr = "127.0.0.1:443"
# 设置证书位置
crtPath = "/etc/nginx/ssl_cert/huixiangwuyou.com.pem"
keyPath = "/etc/nginx/ssl_cert/huixiangwuyou.com.key"
hostHeaderRewrite = "127.0.0.1"
创建service服务
apt install systemd
sudo nano /etc/systemd/system/frps.service
[Unit]
# 服务名称,可自定义
Description = frp server
After = network.target syslog.target
Wants = network.target
[Service]
Type = simple
# 启动frps的命令,需修改为您的frps的安装路径
ExecStart = /path/to/frps -c /path/to/frps.toml
[Install]
WantedBy = multi-user.target
## 使用 systemd 命令管理 frps 服务
# 启动frp
sudo systemctl start frps
# 停止frp
sudo systemctl stop frps
# 重启frp
sudo systemctl restart frps
# 查看frp状态
sudo systemctl status frps
# 设置 frps 开机自启动
sudo systemctl enable frps
连接原理
- 核心组件与准备工作
- 建立控制连接
- 注册与服务暴露
- 建立数据通道与传输
使用
- 公网ip+8080访问:此访问方式是用于没有域名的情况,只能通过http访问
- 域名+8443访问:xxx.com:8443,可以通过http和https访问,但是多了个端口号不美观
- 直接域名访问:使用nginx反向代理服务器的8443端口号转向内网服务器
server {
listen [::]:80;
listen 443 ssl;
listen [::]:443 ssl;
server_name www.next.huixiangwuyou.com next.huixiangwuyou.com;
ssl_certificate /etc/nginx/ssl_cert/huixiangwuyou.com.pem;
ssl_certificate_key /etc/nginx/ssl_cert/huixiangwuyou.com.key;
......
location / {
# 代理到本机的frp服务上
proxy_pass https://127.0.0.1:8443;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header X-Forwarded-Proto $scheme;
proxy_ssl_verify off;
# 以下为常用代理参数
proxy_connect_timeout 60s;
proxy_read_timeout 600s;
proxy_send_timeout 600s;
proxy_buffering off;
}
}
连接数据库
frp支持内网穿透的外网访问端口,建议加密,不要直接暴露在公网上
frps.toml 基本不需要额外修改
# frpc.toml - 客户端配置
serverAddr = "your-frps-server.com"
serverPort = 7000
auth.token = "your_auth_token"
[[proxies]]
name = "mysql"
type = "tcp" # 使用 TCP 协议
localIP = "192.168.3.3" # 内网 MySQL 服务器 IP
localPort = 3306 # 内网 MySQL 端口
remotePort = 13306 # 公网服务器映射端口
# 启用传输加密(可选)
transport.useEncryption = true
transport.useCompression = true
# 带宽限制(可选)
bandwidthLimit = "1MB"
bandwidthLimitMode = "client"
# 限定ip白名单
allowedIps = ["your-client-ip/32", "another-ip/32"]
STCP(安全 TCP)模式
# 内网 MySQL 服务器 frpc.toml
[[proxies]]
name = "mysql-stcp"
type = "stcp"
localIP = "192.168.3.3"
localPort = 3306
sk = "your-secret-key" # 密钥,双方保持一致
# 访问端 frpc.toml(需要在另一台机器上运行)
[[visitors]]
name = "mysql-visitor"
type = "stcp"
serverName = "mysql-stcp"
sk = "your-secret-key"
bindAddr = "127.0.0.1"
bindPort = 3307
如果两端都能建立直接连接,可以使用 P2P 模式减少延迟:
# 内网 Redis 服务器
[[proxies]]
name = "redis-xtcp"
type = "xtcp"
localIP = "192.168.3.4"
localPort = 6379
sk = "redis-secret"
# 访问端(需要安装 frp)
[[visitors]]
name = "redis-xtcp-visitor"
type = "xtcp"
serverName = "redis-xtcp"
sk = "redis-secret"
bindAddr = "127.0.0.1"
bindPort = 6379
常见问题排除方式
常遇到的就是逻辑没问题,就是没通内网,就要弄清楚排查过程
# 首先排查的就是frp的两个配置上是否有问题,然后排查下一步
确定域名访问的地址是外网IP,然后通过nginx经过指定的8080、8443端口映射了内网的80、443端口,然后由内网的nginx映射到指定端口和文件
# 排查访问是否到外网服务器
# eg: 例如排查域名是否通过了本地的 8080端口
curl -v -H "Host: word.huixiangwuyou.com" http://127.0.0.1:8080
# 同样的方式排查下内网,是否访问了80端口
curl -v -H "Host: word.huixiangwuyou.com" http://127.0.0.1:80
常见问题
- 页面重定向:检查nginx配置
- 404页面:重启nginx配置
- 无服务:检查服务是否开启|端口是否正常sudo netstat -tlnp | grep :8001
- 网站使用https访问服务报502:将nginx上的[::] 443去除掉就可以访问了
- journalctl -u frps -f 查看服务端日志
- tail -n 50 /var/log/frps/frps.log 查看最新50条记录(路径主要看你的frps.toml设置的路径)
- journalctl -u frpc -f 查看客户端服务日志
- tail -n 50 /var/log/frpc/frpc.log 同理