nps实现代理,实现http请求代理到内网http端口
nps是一款轻量级、高性能、功能强大的内网代理服务器。目前支持tcp、udp流量转发,可支持任何tcp、udp上层协议(访问内网网站、本地支付接口调试、ssh访问、远程桌面,内网dns解析等等……),此外还支持内网http代理、内网socks5代理、p2p等,并带有功能强大的web管理端。
背景:
- 做微信公众号开发、小程序开发等----> 域名代理模式
- 想在外网通过ssh连接内网的机器,做云服务器到内网服务器端口的映射,----> tcp代理模式
- 在非内网环境下使用内网dns,或者需要通过udp访问内网机器等----> udp代理模式
- 在外网使用HTTP代理访问内网站点----> http代理模式
- 搭建一个内网穿透ss,在外网如同使用内网vpn一样访问内网资源或者设备----> socks5代理模式
相关地址:
nps github地址:
https://github.com/cnlh/nps
CentOS服务器端安装包:https://github.com/cnlh/nps/releases/download/v0.23.2/linux_arm64_server.tar.gz
Windows客户端安装包:
https://github.com/cnlh/nps/releases/download/v0.23.2/win_amd64_client.tar.gz
在CentOS服务器上安装nps server
直接解压linux_arm64_server.tar.gz
tar -zxvf linux_arm64_server.tar.gz
修改nps.conf
appname = nps
#Boot mode(dev|pro)
runmode = pro
#HTTP(S) proxy port, no startup if empty
http_proxy_ip=0.0.0.0
http_proxy_port=8088
#https_proxy_port=443
#https_just_proxy=true
#default https certificate setting
#https_default_cert_file=conf/server.pem
#https_default_key_file=conf/server.key
##bridge
bridge_type=tcp
bridge_port=8024
bridge_ip=0.0.0.0
启动nps server
./nps start
访问nps server管理界面
http://localhost:8080
现在有个需求访问http://api.example.com代理到内网http://127.0.0.1:8080端口
主要流程如下:
http://api.example.com -> nps server -> http://127.0.0.1:8080
http://api.example 通过nginx代理到nps server的8088端口
配置如下:
default.conf
server {
listen 80;
listen 443 ssl;
server_name api.example.com;
include /etc/nginx/common/example.com.ssl.conf;
location / {
proxy_pass http://localhost:8088;
}
if ($scheme != 'https') {
rewrite ^(.*)$ https://$server_name$1 permanent;
}
}
example.com.ssl.conf
ssl_certificate cert/example.com/fullchain.cer;
ssl_certificate_key cert/example.com/example.com.key;
ssl_session_timeout 5m;
ssl_ciphers ECDHE-RSA-AES128-GCM-SHA256:ECDHE:ECDH:AES:HIGH:!NULL:!aNULL:!MD5:!ADH:!RC4;
ssl_protocols TLSv1 TLSv1.1 TLSv1.2;
ssl_prefer_server_ciphers on;
在nps server web管理后台创建客户端
在Windows上安装nps client
直接解压win_amd64_client.tar.gz
配置npc.conf
[common]
server_addr=[nps server服务器IP]:8024
conn_type=tcp
vkey=[客户端vkey]
auto_reconnection=true
max_conn=1000
flow_limit=1000
rate_limit=1000
basic_username=11
basic_password=3
web_username=user
web_password=1234
crypt=true
compress=true
启动客户端
nps -config=conf/npc.conf
启动成功后可以看到client list id为2的client状态变为online的状态
配置域名解析
配置成功后,访问http://api.example.com 会被代理到内网的http://localhost:8080端口