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

作者:doxwant 发布时间: 2025-07-15 阅读量:310 评论数:0

我的服务端直接安装了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

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 = "tcp"
localIP = "127.0.0.1"
localPort = 443
remotePort = 443
# 开启 proxyProtocol
transport.proxyProtocolVersion = "v2"

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

开启 proxyProtocol 后,获取到用户真实IP;

⚙️ 核心配置3( nginx配置)

server {
    listen 443 ssl proxy_protocol; 
    set_real_ip_from 127.0.0.1;
    real_ip_header proxy_protocol;
}

⚙️ 1panel的openResty便捷配置(仅一处修改)

user  root;
worker_processes  auto;
error_log  /var/log/nginx/error.log notice;
error_log  /dev/stdout notice;
pid        /var/run/nginx.pid;
worker_rlimit_nofile 51200;

events {
      use epoll;
      worker_connections 5120;
      multi_accept on;
}

http {
    server {
        listen 443 ssl proxy_protocol;
    }
    include       mime.types;
    default_type  application/octet-stream;
    log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
                      '$status $body_bytes_sent "$http_referer" '
                      '"$http_user_agent" "$http_x_forwarded_for"';
    access_log  /var/log/nginx/access.log  main;
    access_log /dev/stdout main;
    
    server_tokens off;
    sendfile   on;
    tcp_nopush on;       
    tcp_nodelay on;  

    server_names_hash_bucket_size 512;
    client_header_buffer_size 32k;
    client_max_body_size 50m;
    keepalive_timeout 60;
    keepalive_requests 5000;

    gzip on;
    gzip_min_length  1k;
    gzip_buffers     4 16k;
    gzip_http_version 1.1;
    gzip_comp_level 2;
    gzip_types     text/plain application/javascript application/x-javascript text/javascript text/css application/xml;
    gzip_vary on;
    gzip_proxied   expired no-cache no-store private auth;
    gzip_disable   "MSIE [1-6]\.";

    limit_conn_zone $binary_remote_addr zone=perip:10m;
    limit_conn_zone $server_name zone=perserver:10m;

    include /usr/local/openresty/nginx/conf/conf.d/*.conf;
    include /usr/local/openresty/nginx/conf/default/*.conf;
    include /usr/local/openresty/1pwaf/data/conf/waf.conf;
    set_real_ip_from 127.0.0.1;
    real_ip_header proxy_protocol;
}

❓ 为什么这么配置?

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


腾讯云服务器优惠:

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

阿里云服务器优惠:

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

评论