使用 frp 实现内网穿透:让本地服务器安全暴露到公网

使用 frp 实现内网穿透:让本地服务器安全暴露到公网

frp(frp 是 Fast Reverse Proxy 的缩写)是一个轻量、高效的内网穿透工具,可以将内网的 SSH、Web、MySQL 等服务安全地暴露到公网。本文基于一个实际生产环境,介绍 frp 的安装、配置和使用方法,实现:

  • 通过域名访问本地服务器的 Web 服务

  • 通过公网 SSH 端口访问本地服务器的 SSH

  • (可选)通过公网访问 MySQL

整个方案紧凑、安全,只需一台有公网 IP 的云服务器作为 frps 服务端。

网络拓扑图

  • 云服务器 B 拥有公网 IP,运行 frps 和 Nginx

  • 本地服务器 AA 通过 VPN 能访问云服务器 B 的内网 IP,运行 frpc

  • VPN 内用户也可通过 云服务器B内网IP:2201 直接 SSH 到本地 AA(无需知道 AA 内网 IP)

1. 安装 frp

前往 frp GitHub Releases 下载最新版本(推荐 amd64 或 arm64)。

服务端(云服务器 B)

bash 复制代码
# 创建目录
mkdir -p /opt/frp && cd /opt/frp

# 下载(按你的 CPU 架构选择)
wget https://github.com/fatedier/frp/releases/download/v0.66.0/frp_0.66.0_linux_amd64.tar.gz

tar zxvf frp_0.66.0_linux_amd64.tar.gz
cd frp_0.66.0_linux_amd64

# 拷贝二进制
cp frps /usr/local/bin/
chmod +x /usr/local/bin/frps

客户端(本地服务器 AA)

bash 复制代码
mkdir -p /opt/frp && cd /opt/frp

wget https://github.com/fatedier/frp/releases/download/v0.66.0/frp_0.66.0_linux_amd64.tar.gz
tar zxvf frp_0.66.0_linux_amd64.tar.gz

cp frp_0.66.0_linux_amd64/frpc /usr/local/bin/
chmod +x /usr/local/bin/frpc

建议使用 systemd 服务开机自启(略,可自行搜索 "frp systemd")。

2. 服务端配置(云服务器 B)

/etc/frp/frps.toml

toml 复制代码
bindPort = 37001          # frpc 连接 frps 的端口(内网安全)
bindAddr = "0.0.0.0"

vhostHTTPPort = 31080     # HTTP 穿透虚拟主机端口

# Dashboard(可选,监控用)
webServer.addr = "0.0.0.0"
webServer.port = 37501
webServer.user = "frp_topka"
webServer.password = "xxxx"

# 必须开启鉴权
auth.method = "token"
auth.token = "xxxx"       # 自定义强令牌

log.to = "/var/log/frps.log"
log.level = "info"
log.maxDays = 7

systemd 服务(服务端)

bash 复制代码
vim /etc/systemd/system/frps.service
复制代码
[Unit]
Description=FRP Server
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/frps -c /etc/frp/frps.toml
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

启动

bash 复制代码
systemctl daemon-reload
systemctl enable frps
systemctl start frps
systemctl status frps

3. Nginx 配置(云服务器 B,反向代理 frp HTTP 端口)

nginx 复制代码
server {

    listen 80;
    server_name *.kong.xxx.cn kong.xxx.cn;
    
    # IP 白名单
    #allow 1.2.3.4;
    #allow 5.6.7.0/24;
    #deny all;

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

重启 Nginx 即可。域名已解析到云服务器 B 的公网 IP。

4. 客户端配置(本地服务器 AA)

/etc/frp/frpc.toml

toml 复制代码
serverAddr = "192.168.1.13"   # 云服务器 B 的内网 IP
serverPort = 37001

auth.method = "token"
auth.token = "xxxx"           # 与服务端一致

# SSH 穿透
[[proxies]]
name = "ubuntu"
type = "tcp"
localIP = "127.0.0.1"
localPort = 22
remotePort = 2201             # 公网访问:公网IP:2201 → 本地SSH

# Web 穿透
[[proxies]]
name = "admin_workstation"
type = "http"
localIP = "127.0.0.1"
localPort = 80
customDomains = ["kong.xxx.cn"]

# MySQL 穿透(可选)
[[proxies]]
name = "mysql"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3306
remotePort = 13306

systemd 服务(客户端)

bash 复制代码
vim /etc/systemd/system/frpc.service
复制代码
[Unit]
Description=FRP Client
After=network.target

[Service]
Type=simple
ExecStart=/usr/local/bin/frpc -c /etc/frp/frpc.toml
Restart=always
RestartSec=5

[Install]
WantedBy=multi-user.target

启动

bash 复制代码
systemctl daemon-reload
systemctl enable frpc
systemctl start frpc
systemctl status frpc

5. 使用方式

  • Web 服务 :直接访问 http://kong.xxx.cn(或子域名)即可访问本地 AA 的 Web 服务

  • SSH 访问本地 AA

    • 公网:ssh user@云服务器B公网IP -p 2201

    • VPN 内:ssh user@192.168.1.13 -p 2201(无需知道 AA 内网 IP)

  • MySQL :连接 云服务器B公网IP:13306

  • 管理面板 :访问 云服务器B公网IP:37501,用户名 frp_topka,密码自行设置

安全建议

  • 令牌(token)设置复杂

  • 防火墙只开放必要端口(80、2201、13306、37501 等)

  • 可配合 Nginx 添加 IP 白名单或基本认证进一步加强安全

  • 定期查看 Dashboard 监控连接状态

这样就完成了安全、稳定的内网穿透方案,整个配置简单高效,适合个人和小型团队使用。祝使用愉快!

相关推荐
小小福仔2 小时前
Linux运维基础篇(二)之用户管理
linux·运维·服务器·增删改查
企业对冲系统官2 小时前
大宗商品风险对冲系统统计分析功能的技术实现
运维·python·算法·区块链·github·pygame
下海fallsea2 小时前
德邦跟了京东,极兔搂住顺丰
网络·人工智能·安全
Web极客码2 小时前
如何在WordPress登录页面添加隐藏或显示密码按钮
运维·服务器
QQ12154614682 小时前
使用远程桌面连接Windows 2012 R2 Standard服务器报错:出现身份验证错误。要求的函数不受支持。这可能是由于CredSSP加密数据库修正。
服务器·windows·windows server
曹天骄2 小时前
Cloudflare Worker vs 阿里云 DCND:回源次数、链路结构与真实性能对比
运维·阿里云·云计算
haluhalu.2 小时前
[特殊字符] 深入理解Linux信号机制:信号的产生,保存和捕捉
linux·运维·服务器
JY.yuyu2 小时前
Linux磁盘管理 / 硬盘分区、创建逻辑卷
linux·运维·服务器
iSee8573 小时前
struts2 XML外部实体注入漏洞复现(CVE-2025-68493)
xml·安全·struts2