内网穿透工具是nps,因为nps不支持Proxy Protocol协议,所以还需要haproxy。如果使用frp的话则不需要haproxy,frp原生支持Proxy Protocol协议,理论上不支持Proxy Protocol协议的穿透工具都可以加一级HA来获取真实IP。
我的安排是这样子的,大家可以参考一下
VPS服务器上的443端口,haproxy监听443端口,加上用户真实ip后转发给nps监听的3443端口
内网穿透本地nginx 443端口到VPS上的3443端口,借一张图,大概就这样子,随便看看吧。
安装haproxy
主流的linux发行版的软件仓库都收录了haproxy,只需要一条命令就可以安装haproxy了。
对于ubuntu/debian用下面的命令安装
apt update && apt upgrade && apt install haproxy
对于centos用下面的命令安装
yum install epel-release -ysudo yum update -y && yum install haproxy -y
用下面的命令编辑haproyx的配置文件
vim /etc/haproxy/haproxy.cfg
按“i”键进入编辑模式,在文件的最后面插入下面的内容,要根据的自己的实际情况更改。
listen webhttp
# haproxy监听的端口80、443,根据我的端口规划确定的
bind 0.0.0.0:80
mode tcp
option forwardfor
#haproxy转发给nps监听的443端口
server webhttp1 127.0.0.1:8080 send-proxy check inter 3000 fall 3 rise 5
listen webhttps
bind 0.0.0.0:443
mode tcp
option forwardfor
server webhttps1 127.0.0.1:3443 send-proxy check inter 3000 fall 3 rise 5
#转发我的世界到2556端口
listen minecraft
bind *:25565
mode tcp
balance leastconn
option tcp-check
server minecraft1 127.0.0.1:2556 send-proxy
保存配置文件后,运行下面命令启动和重启haproxy
systemctl start haproxy
systemctl restart haproxy
然后运行下面命令检查haproxy是否运行,如果输出下图内容则配置被应用。
systemctl status haproxy
配置nginx,编辑conf文件加入以下内容
server
{
listen 80 proxy_protocol; #proxy_protocol
listen 443 ssl proxy_protocol; #proxy_protocol
......其他代码......
# 开启排除IP功能
real_ip_recursive on;
# 排除本地ip
set_real_ip_from 192.168.0.0/24;
set_real_ip_from 127.0.0.1;
#CF 地址https://www.cloudflare-cn.com/ips/
set_real_ip_from 173.245.48.0/20;
set_real_ip_from 103.21.244.0/22;
set_real_ip_from 103.22.200.0/22;
set_real_ip_from 103.31.4.0/22;
set_real_ip_from 141.101.64.0/18;
set_real_ip_from 108.162.192.0/18;
set_real_ip_from 190.93.240.0/20;
set_real_ip_from 188.114.96.0/20;
set_real_ip_from 197.234.240.0/22;
set_real_ip_from 198.41.128.0/17;
set_real_ip_from 162.158.0.0/15;
set_real_ip_from 104.16.0.0/13;
set_real_ip_from 104.24.0.0/14;
set_real_ip_from 172.64.0.0/13;
set_real_ip_from 131.0.72.0/22;
# 真实IP使用proxy_protocol协议
real_ip_header proxy_protocol;
#real_ip_header X-Real-IP;
......其他代码......
}
参考:
https://post.smzdm.com/p/a30vrqlk/
https://myth.cx/p/minecraft-haproxy/
