下面是整理后的完整配置和步骤说明,用于在办公室通过 SSH 隧道访问家里 VMware Workstation 上的服务器(内网 IP:192.168.209.128)。
前提条件
假设:
- 家里服务器(内网 IP: 192.168.209.128)上的 SSH 密码为
123456
。 - 使用的公共跳板机为
server.example.com
(假定其上有sshproxy
用户可以 SSH 登录)。 - 办公室的电脑需要通过
server.example.com
来访问家里的服务器。
步骤 1:家里电脑配置 Nginx 四层转发
编辑 Nginx 配置文件,添加如下内容:
stream {
server {
listen 7777;
proxy_pass 192.168.209.128:22; # 转发到家里服务器的 SSH 服务端口
}
}
重启 Nginx 以应用新配置:
sudo systemctl restart nginx
步骤 2:家里电脑建立 SSH 隧道
在家里的电脑上执行以下命令,将远程主机的 8888 端口流量转发到家里的 Nginx 监听的 7777 端口:
ssh -fN -p 443 -R 0.0.0.0:8888:127.0.0.1:7777 sshproxy@server.example.com
步骤 3:办公室电脑连接家里服务器
在办公室的电脑上,通过 SSH 隧道连接家里的服务器:
ssh -p 8888 root@server.example.com
输入家里服务器的密码 123456
即可登录成功。
步骤 4:配置公共跳板机 Nginx
在 server.example.com
上配置 Nginx,使其监听 7443 端口并启用 SSL:
server {
listen 7443 ssl proxy_protocol;
server_name server.example.com;
# SSL配置 (根据具体情况设置)
ssl_certificate /path/to/your/server.crt;
ssl_certificate_key /path/to/your/server.key;
location / {
proxy_pass http://127.0.0.1:80; # 修改为你的实际代理地址
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_set_header X-NginX-Proxy true;
proxy_redirect off;
}
}
重启 Nginx 以应用新配置:
sudo systemctl restart nginx
步骤 5:配置 HAProxy
编辑 HAProxy 配置文件 /etc/haproxy/haproxy.cfg
,添加如下内容:
frontend https-in
bind *:443 ssl crt /path/to/ssl_cert.pem
mode tcp
option tcplog
acl is_local_pp_domain req.ssl_sni -i -f /etc/haproxy/local_pp_domain.txt
use_backend nginx_pp_https if is_local_pp_domain
backend nginx_pp_https
mode tcp
server nginx 127.0.0.1:7443 send-proxy
创建或编辑 /etc/haproxy/local_pp_domain.txt
文件,添加以下内容:
server.example.com
重启 HAProxy 以应用新配置:
sudo systemctl restart haproxy
总结
通过以上配置,可以在办公室通过 server.example.com
的 8888 端口访问家里服务器的 SSH 服务。主要步骤包括家里电脑配置 Nginx 四层转发、建立 SSH 隧道,以及在公共跳板机上配置 Nginx 和 HAProxy。这样就能实现安全的远程访问。