frp 的内网穿透 使用
下载与文档地址:
https://github.com/fatedier/frp
SSH 的穿透:
Access your computer in a LAN network via SSH Modify frps.toml on server A by setting the bindPort for frp clients to connect to: # frps.toml bindPort = 7000 Start frps on server A: ./frps -c ./frps.toml Modify frpc.toml on server B and set the serverAddr field to the public IP address of your frps server: # frpc.toml serverAddr = "x.x.x.x" serverPort = 7000 [[proxies]] name = "ssh" type = "tcp" localIP = "127.0.0.1" localPort = 22 remotePort = 6000
./frps -c ./frps.toml
Web 的穿透: ./frps.toml 文件: bindPort = 8900 # 服务器端口 vhostHTTPPort = 8901 # 需要映射的端口 , 本机localPort对应的服务器端口
2025-04-16 21:08:33.589 [I] [frps/root.go:105] frps uses config file: ./frps.toml 2025-04-16 21:08:33.698 [I] [server/service.go:237] frps tcp listen on 0.0.0.0:8900 2025-04-16 21:08:33.698 [I] [server/service.go:305] http service listen on 0.0.0.0:8901 2025-04-16 21:08:33.698 [I] [frps/root.go:114] frps started successfully
添加服务: /lib/systemd/system/frps.service
systemctl enable frps
systemctl start frps
systemctl status frps
[Unit] Description=frps daemon [Service] Type=simple #此处把/root/frp_linux_arm64替换成 frps的实际安装目录 #ExecStart=/srv/frp/frps -c /srv/frp/frps.toml >> /srv/frp/log.log ExecStart=/srv/frp/frps -c /srv/frp/frps.toml [Install] WantedBy=multi-user.target
以下是实例:
用户访问 http://xx.116.209.xx:8901 即访问的是服务器的 8900端口
而8900是客户端连接的端口
相当于用户访问 http://xx.116.209.xx:8901 -> 转到 -> 本地机器的 8787 端口 , 此时服务器上只有端口转发功能, 服务器也无需太高配置, 只要有公网地址 能转发即可, 所有的服务在本地机器运行, 如果有域名也可以配置对应的域名, 按这个理解服务器都不需要太好的配置
./frps -c ./frps.toml
Web 的穿透: ./frps.toml 文件: bindPort = 8900 # 服务器端口 vhostHTTPPort = 8901 # 需要映射的端口 , 本机localPort对应的服务器端口
2025-04-16 21:08:33.589 [I] [frps/root.go:105] frps uses config file: ./frps.toml 2025-04-16 21:08:33.698 [I] [server/service.go:237] frps tcp listen on 0.0.0.0:8900 2025-04-16 21:08:33.698 [I] [server/service.go:305] http service listen on 0.0.0.0:8901 2025-04-16 21:08:33.698 [I] [frps/root.go:114] frps started successfully
./frpc -c ./frpc.toml
serverAddr = "xx.116.209.xx" #serverAddr = "127.0.0.1" serverPort = 8900 # 服务器端口 #serverPort = 7000 [[proxies]] name = "web" type = "http" #localIP = "127.0.0.1" localPort = 8787 # 将本机的8787端口映射到公网 customDomains = ["xx.116.209.xx"] #remotePort = 6000 transport.useCompression = true locations = ["/"] #[[proxies]] #name = "web2" #type = "http" #localIP = "127.0.0.1" #localPort = 2888 #customDomains = ["xx.116.209.xx"] #locations = ["/pc", "/dev"]
================以下是文档说明============================
https://gofrp.org/zh-cn/docs/examples/stcp/
通过自定义域名访问内网的 Web 服务
通过简单配置 HTTP 类型的代理,您可以让用户通过自定义域名访问内网的 Web 服务。
HTTP 类型的代理非常适合将内网的 Web 服务通过自定义域名提供给外部用户。相比于 TCP 类型代理,HTTP 代理不仅可以复用端口,还提供了基于 HTTP 协议的许多功能。
HTTPS 与此类似,但是需要注意,frp 的 https 代理需要本地服务是 HTTPS 服务,frps 端不会做 TLS 终止。也可以结合 https2http 插件来实现将本地的 HTTP 服务以 HTTPS 协议暴露出去。
步骤
配置 frps.toml
在 frps.toml 文件中添加以下内容,以指定 HTTP 请求的监听端口为 8080:
bindPort = 7000 vhostHTTPPort = 8080
如果需要配置 HTTPS 代理,还需要设置 vhostHTTPSPort。
配置 frpc.toml
serverAddr = "x.x.x.x" serverPort = 7000 [[proxies]] name = "web" type = "http" localPort = 80 customDomains = ["www.yourdomain.com"] [[proxies]] name = "web2" type = "http" localPort = 8080 customDomains = ["www.yourdomain2.com"]
启动 frps 和 frpc
域名解析
将 www.yourdomain.com 和 www.yourdomain2.com 的域名 A 记录解析到服务器的 IP 地址 x.x.x.x。如果服务器已经有对应的域名,您还可以将 CNAME 记录解析到原始域名。另外,通过修改 HTTP 请求的 Host 字段也可以实现相同的效果。
通过浏览器访问
使用浏览器访问 http://www.yourdomain.com:8080 即可访问内网机器上的 80 端口服务,访问 http://www.yourdomain2.com:8080 可以访问内网机器上的 8080 端口服务。