nginx + ssh 端口转发实现内网穿透
原理
- 客户端 ssh 端口转发 web 网站端口
- 服务端 nginx 反向代理端口
- 服务端开启 ssh
步骤
客户端执行
ssh -o ServerAliveInterval=60 -fNR 8080:127.0.0.1:80 root@server_ip -p 22
将本地的
80
端口转发到服务器8080
端口。注: 服务端需开放
8080
端口访问在服务端查看转发端口
- nginx 配置代理
server {
listen 80;
server_name sample.com;
index index.html index.htm;
location ~ / {
proxy_redirect off;
proxy_pass http://127.0.0.1:8080; # 代理到 8080 端口
client_max_body_size 1024M; # 允许上传大文件
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
location ~ /\.ht {
deny all;
}
}
- 域名解析或修改
/etc/hosts
文件,1.2.3.4 sample.com
- 验证:
curl sample.com
至此就完成内网穿透了。
昨天看到一篇博文 无客户端内网穿透工具, 使用命令就可以做到内网穿透,然后就有了这个想法,后出了这篇博文。
PS:
- 稳定性未测试,但是这 2 个软件已经很稳定了,出问题概率应该很低。
- windows 平台可用
git bash
或用自带ssh
实现。
本作品采用《CC 协议》,转载必须注明作者和本文链接