第一次听说内网穿透这个词时,我也是一头雾水。简单来说,内网穿透就是让外网能够访问你内网服务的技术。想象一下,你家里有个NAS,或者你在公司内网开发了一个网站,想让朋友或者客户访问,这时候就需要内网穿透了。
以前我经常遇到这样的场景:在家调试微信公众号开发,需要公网能访问本地服务;或者出差时需要连接公司内网的数据库。如果没有内网穿透,这些需求根本无法实现。市面上有很多内网穿透工具,比如ngrok,但后来发现frp更符合我的需求 - 开源、免费、配置灵活,而且性能相当不错。
frp特别适合以下人群:
选择服务器时,我踩过不少坑。根据经验,建议选择:
我常用的云服务商有阿里云、腾讯云等,个人项目用最基础的按量付费实例就行,一个月成本也就几十块钱。
域名是必须的,我推荐在阿里云或腾讯云购买,一个普通的.top域名一年才十几块钱。解析设置很简单:
安装frp其实特别简单,我习惯用最新稳定版。以下是具体步骤:
bash复制# 在服务器上执行
wget https://github.com/fatedier/frp/releases/download/v0.44.0/frp_0.44.0_linux_amd64.tar.gz
tar -zxvf frp_0.44.0_linux_amd64.tar.gz
mv frp_0.44.0_linux_amd64 /usr/local/frp
客户端安装也类似,只是下载对应平台的版本。Windows用户可以直接下载.zip文件解压即可。
服务端配置文件frps.ini是核心,这是我的生产环境配置:
ini复制[common]
bind_port = 7000
token = your_strong_password_here
vhost_http_port = 8080
vhost_https_port = 8443
# 仪表盘配置
dashboard_port = 7500
dashboard_user = admin
dashboard_pwd = another_strong_password
enable_prometheus = true
# 高级配置
max_pool_count = 50
tls_only = true
allow_ports = 40000-50000
几个关键点:
为了让frps稳定运行,我推荐用systemd管理:
bash复制# /etc/systemd/system/frps.service
[Unit]
Description=Frp Server Service
After=network.target
[Service]
Type=simple
User=nobody
Restart=on-failure
RestartSec=5s
ExecStart=/usr/local/frp/frps -c /usr/local/frp/frps.ini
[Install]
WantedBy=multi-user.target
然后执行:
bash复制systemctl daemon-reload
systemctl enable frps
systemctl start frps
安全方面我吃过亏,现在会做这些加固:
这是我常用的web服务穿透配置:
ini复制[common]
server_addr = frp.yourdomain.com
server_port = 7000
token = your_strong_password_here
tls_enable = true
[web]
type = http
local_ip = 127.0.0.1
local_port = 8080
custom_domains = test.yourdomain.com
# 如果需要HTTPS
[https-web]
type = https
local_ip = 127.0.0.1
local_port = 8443
custom_domains = secure.yourdomain.com
启动客户端:
bash复制./frpc -c ./frpc.ini
TCP穿透适合SSH、数据库等场景:
ini复制[ssh]
type = tcp
local_ip = 127.0.0.1
local_port = 22
remote_port = 6000
[mysql]
type = tcp
local_ip = 127.0.0.1
local_port = 3306
remote_port = 33060
使用时:
bash复制ssh -p 6000 user@frp.yourdomain.com
mysql -h frp.yourdomain.com -P 33060 -u root -p
frp还有一些很实用的高级功能:
比如这是带压缩和带宽限制的配置:
ini复制[web]
type = http
local_ip = 127.0.0.1
local_port = 8080
custom_domains = test.yourdomain.com
use_compression = true
bandwidth_limit = 1MB
我习惯用Nginx做前端代理,配置如下:
nginx复制server {
listen 80;
server_name *.frp.yourdomain.com;
return 301 https://$host$request_uri;
}
server {
listen 443 ssl;
server_name *.frp.yourdomain.com;
ssl_certificate /path/to/cert.pem;
ssl_certificate_key /path/to/key.pem;
location / {
proxy_pass http://127.0.0.1:8080;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
}
}
推荐使用Let's Encrypt免费证书:
bash复制certbot certonly --nginx -d *.frp.yourdomain.com
证书自动续期可以添加到crontab:
bash复制0 3 * * * certbot renew --quiet
经过多次调优,我发现这些参数很有效:
nginx复制proxy_buffering off;
proxy_http_version 1.1;
proxy_set_header Connection "";
keepalive_timeout 65;
遇到连接问题时,我通常这样排查:
如果发现速度慢,可以尝试:
每月我都会做这些安全检查:
我使用Prometheus+Grafana监控frp:
ini复制# frps.ini
enable_prometheus = true
然后配置Grafana展示关键指标:
对于重要业务,可以部署多实例:
配置文件备份很重要,我使用git管理:
bash复制git init
git add frps.ini frpc.ini
git commit -m "Initial config"
我用frp暴露本地开发环境:
ini复制[dev]
type = http
local_ip = 127.0.0.1
local_port = 3000
custom_domains = dev.yourdomain.com
这样就能随时随地访问本地开发中的项目了。
家庭网络配置:
ini复制[nas]
type = tcp
local_ip = 192.168.1.100
local_port = 5000
remote_port = 5000
在企业中,我们这样使用:
frp的灵活性让它能适应各种复杂场景,从我个人的使用经验来看,它完全能够满足从个人开发到中小企业级的各种内网穿透需求。配置过程中最重要的是安全意识的培养,每次修改配置前,我都会问自己:这样设置安全吗?会不会暴露不必要的服务?多问几个这样的问题,能避免很多潜在风险。