nginx + ssh 端口转发实现内网穿透

原理

  • 客户端 ssh 端口转发 web 网站端口
  • 服务端 nginx 反向代理端口
  • 服务端开启 ssh

步骤

  1. 客户端执行

    ssh -o ServerAliveInterval=60 -fNR 8080:127.0.0.1:80 root@server_ip -p 22

    将本地的 80 端口转发到服务器 8080 端口。

    注: 服务端需开放 8080 端口访问

  2. 在服务端查看转发端口

nginx + ssh 端口转发实现内网穿透

  1. 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;
    }
}
  1. 域名解析或修改 /etc/hosts 文件, 1.2.3.4 sample.com
  2. 验证: curl sample.com

nginx + ssh 端口转发实现内网穿透

nginx + ssh 端口转发实现内网穿透

至此就完成内网穿透了。

昨天看到一篇博文 无客户端内网穿透工具, 使用命令就可以做到内网穿透,然后就有了这个想法,后出了这篇博文。

PS:

  1. 稳定性未测试,但是这 2 个软件已经很稳定了,出问题概率应该很低。
  2. windows 平台可用git bash 或用自带 ssh 实现。
本作品采用《CC 协议》,转载必须注明作者和本文链接
世界上最遥远的距离不是生与死,而是你亲手制造的BUG就在你眼前,你却怎么都找不到ta。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!