使用frp内网穿透时如何获取真实用户IP?真实可用!

作者:Administrator 发布时间: 2025-07-15 阅读量:29

我的服务端直接安装了frp,监听的 80 和 443 端口,没有使用 nginx 进行代理,前置处理。

❓ 内网穿透发现的用户真实ip问题

部署在内网的应用,后台记录的IP都是内网IP,无法记录真实IP,通过IP获取天气等类型的服务无法正常使用!

✅ 为什么采用frp进行内网穿透?

高配置的云服务器比较贵,家里面自己组装的服务配置高,性能好,就是公网ip难获取,于是便采取了内网穿透这个方案,所有的服务存储在家里面的内网,公网只做流量转发,访问速度在于内网公网的上行带宽。

🌍 服务器选择

我买的是:

https://www.rainyun.com/gotab_

已经用了两年,自我感觉还不错,我选择的是宁波地区的按流量计费的服务器,每个月有 1 TB 的上下行合计流量,最低配置上下行带宽是 100Mbps ,使用起来速度还是可以的。而且也不用担心被恶意刷流量等问题,因为流量超出后,会限制带宽在 5mbps(此时是不限量)。完美解决了被盗刷的风险。

⚙️ 核心配置( frps.toml 服务端)

bindAddr = "0.0.0.0"
bindPort = 7000
vhostHTTPSPort = 443

auth.method = "token"
auth.token = "xxxxxxxxx"

webServer.addr = "0.0.0.0"
webServer.port = 7500
webServer.user = "xxxxx"
webServer.password = "xxxxxxxxx"

# tls
#transport.tls.force = true
#transport.tls.certFile = "/etc/frp/ssl/server.crt"
#transport.tls.keyFile = "/etc/frp/ssl/server.key"
#transport.tls.trustedCaFile = "/etc/frp/ssl/ca.crt"

设置 https 的代理端口:443,进行安全链接的代理访问

⚙️ 核心配置2( frpc.toml 客户端,内网机器)

# 服务端公网ip
serverAddr = "xxx.xxx.xxx.xxx"
serverPort = 7000

auth.method = "token"
# 服务端认证密码
auth.token = "xxxxxxxxx"

webServer.addr = "0.0.0.0"
webServer.port = 7400
# 管理面板用户名
webServer.user = "xxxxx"
# 管理面板密码
webServer.password = "xxxxxxxxx"
webServer.pprofEnable = false

# tls
#transport.tls.certFile = "/etc/frp/ssl/client.crt"
#transport.tls.keyFile = "/etc/frp/ssl/client.key"
#transport.tls.trustedCaFile = "/etc/frp/ssl/ca.crt"

# 直接穿透80端口,在内网的nginx或者OpenResty服务中进行https的重定向
[[proxies]]
name = "http"
type = "tcp"
localIP = "127.0.0.1"
localPort = 80
remotePort = 80

# https的核心配置
[[proxies]]
name = "https"
type = "https"
#内网IP,填127.0.0.1不行
localIP = "192.168.1.100"
localPort = 443
# 绑定域名,支持 *.example.com 泛域名
customDomains = ["example.com", "*.example.com"]
# 开启 proxyProtocol
transport.proxyProtocolVersion = "v2"

# 其他代理
[[proxies]]
name = "mysql"
type = "tcp"
localIP = "127.0.0.1"
localPort = 3306
remotePort = 3306

主要在于80代理到内网后,由内网重定向到443,然后再经过https的方式访问内网内容,开启 proxyProtocol 后,是可以获取到用户的真实IP的

❓ 为什么这么配置,感觉转发好几次

主要是所有内容存放在本地,不用对服务端进行多少配置的改动,只需要开启vhostHTTPSPort = 443 即可,这样在切换服务器时候会比较省心,比如你用的雨云服务器,后面想换成阿里云的或者腾讯云的服务器,只需客户端更改服务端的 IP 即可。


腾讯云服务器优惠:

https://cloud.tencent.com/act/cps/redirect?redirect=6425&cps_key=ed5d353e58c55002bf6045db776f7f7c&from=console

阿里云服务器优惠:

https://www.aliyun.com/minisite/goods?userCode=uk86fxdk