跳转至

在服务器上通过本地代理访问外网的配置方法(SSH & HTTP 两种方式)

在某些场景下,我们希望远程服务器访问外网时,能够走本地的代理(如 V2Ray 或其他 SOCKS5 代理),以突破防火墙或加速访问。本文总结了两种常用方式:SSH 端口转发HTTP/HTTPS 代理

前置条件

本地需要运行一个代理服务(V2Ray/Shadowsocks 等),假设: - SOCKS5 代理地址:127.0.0.1:10808 - HTTP 代理地址:127.0.0.1:10800

⚠️ 注意:V2RayN 默认 SOCKS5 端口是 10808,很多教程写的 10810 是错的。


一、SSH 方式(远程端口转发)

原理

远程服务器                    本地机器                     V2Ray 代理
+------------+              +------------+              +------------+
|  请求外网   |  --(SSH隧道)--> |  127.0.0.1 |  --(本地)--> |  127.0.0.1 |
| :10810     |              | :10810     |              | :10808     |
+------------+              +------------+              +------------+

通过 SSH 建立的远程端口转发,在远程服务器上监听一个端口,所有流量通过 SSH 隧道转发到本地,再由本地代理访问外网。

配置步骤

方法一:命令行临时转发

ssh -f -N -R 10810:127.0.0.1:10808 user@remote-server
  • -R 10810:127.0.0.1:10808:远程服务器监听 10810 端口,流量转发到本地 127.0.0.1:10808
  • -f:后台运行
  • -N:不执行远程命令
  • 关闭终端或杀掉进程后,隧道断开
  • 适合临时使用

方法二:配置文件自动转发

在本地 ~/.ssh/config 添加:

Host remote-server
    HostName <server-ip>
    User <username>
    RemoteForward 10810 127.0.0.1:10808

每次 ssh remote-server 时自动创建转发隧道。

方法三:自动重连(推荐)

使用 autossh 保证隧道断开后自动重连:

# 安装 autossh
# Ubuntu/Debian: sudo apt install autossh
# macOS: brew install autossh

autossh -M 10984 -f -N -R 10810:127.0.0.1:10808 user@remote-server
  • -M 10984:监控端口,用于检测连接状态

远程服务器使用

SSH 隧道建立后,远程服务器上会监听 127.0.0.1:10810,需要把代理环境变量指向这个端口:

# 远程服务器执行:指向 SSH 隧道端口
export all_proxy=socks5://127.0.0.1:10810
export http_proxy=socks5://127.0.0.1:10810
export https_proxy=socks5://127.0.0.1:10810

# 写入 ~/.bashrc 持久化
echo 'export all_proxy=socks5://127.0.0.1:10810' >> ~/.bashrc
source ~/.bashrc

⚠️ 注意:这里指向的是远程服务器上 127.0.0.1:10810(SSH 隧道端口),不是本地的 10808

验证
# 远程服务器执行
curl -I https://www.google.com
curl -I https://api.github.com
Git SSH 推送(需要配合 ProxyCommand)

如果程序不支持 all_proxy(如 Git SSH),需要配置 ProxyCommand:

# 远程服务器执行
# 先安装 nc: apt install netcat-openbsd
git config --global core.ssh_command "ssh -o 'ProxyCommand=nc -X 5 -x 127.0.0.1:10810 %h %p'"

# 然后正常推送
git push

二、HTTP/HTTPS 方式(环境变量代理)

原理

通过设置环境变量,让 HTTP/HTTPS 请求走本地代理。不需要 SSH 隧道,但需要本地有 HTTP 代理端口。

配置步骤

1. 确保本地有 HTTP 代理端口

V2RayN 可以开启 HTTP 代理(或使用 socks2http 转换):

本地代理:
- SOCKS5: 127.0.0.1:10808
- HTTP: 127.0.0.1:10800(如果开启了 HTTP 出站)

2. 服务器设置环境变量

# 方式一:HTTP 代理
export http_proxy=http://127.0.0.1:10800
export https_proxy=http://127.0.0.1:10800

# 方式二:SOCKS5 代理(部分程序支持)
export all_proxy=socks5://127.0.0.1:10808
export http_proxy=socks5://127.0.0.1:10808
export https_proxy=socks5://127.0.0.1:10808

写入 ~/.bashrc 持久化:

echo 'export all_proxy=socks5://127.0.0.1:10808' >> ~/.bashrc
source ~/.bashrc

3. Git HTTPS 推送

# 确保远程地址是 HTTPS
git remote -v
# 如果是 git@github.com:xxx,改为 https://github.com/xxx
git remote set-url origin https://github.com/USERNAME/REPO.git

# 然后正常推送
git push

⚠️ 注意:Git 默认走 http_proxy/https_proxy,不支持 SOCKS5。需要用 all_proxygit config --global http.proxy socks5://127.0.0.1:10808

4. 验证

curl -I https://www.google.com
curl -I https://api.github.com

三、两种方式对比

特性 SSH 远程端口转发 HTTP/HTTPS 代理
原理 隧道转发所有 TCP 流量 环境变量控制 HTTP 请求
适用范围 任意 TCP(Git SSH、curl、wget、pip 等) 仅 HTTP/HTTPS 协议
配置复杂度 较高(需要 SSH 隧道) 简单(设置环境变量)
本地要求 运行 SSH + 代理 运行代理
稳定性 依赖 SSH 连接,断开需要重连 依赖代理,代理挂了就不通
延迟 稍高(多一层 SSH 转发) 低(直接代理)

四、常见问题

2. pip/conda 走代理

pip install some-package --proxy socks5://127.0.0.1:10808
# 或
pip install some-package --proxy http://127.0.0.1:10800

3. Docker 拉取镜像

# 方式一:配置 Docker 代理
sudo mkdir -p /etc/systemd/system/docker.service.d
sudo tee /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
[Service]
Environment="HTTP_PROXY=socks5://127.0.0.1:10810"
Environment="HTTPS_PROXY=socks5://127.0.0.1:10810"
EOF
sudo systemctl daemon-reload
sudo systemctl restart docker

# 方式二:手动指定代理
docker pull library/ubuntu --proxy socks5://127.0.0.1:10810

五、自动化脚本

本地:启动 SSH 隧道

#!/bin/bash
# 本地执行,启动 SSH 隧道
SERVER="user@remote-server"
PROXY_PORT="10808"
LISTEN_PORT="10810"

echo "正在启动 SSH 隧道..."
ssh -f -N -R ${LISTEN_PORT}:127.0.0.1:${PROXY_PORT} ${SERVER}
echo "SSH 隧道已启动,远程服务器可通过 127.0.0.1:${LISTEN_PORT} 访问代理"

远程:快捷代理别名

# 写入 ~/.bashrc
echo 'alias proxy-on="export all_proxy=socks5://127.0.0.1:10810"' >> ~/.bashrc
echo 'alias proxy-off="unset all_proxy http_proxy https_proxy"' >> ~/.bashrc
source ~/.bashrc

# 使用
proxy-on    # 开启代理
proxy-off   # 关闭代理

💡 总结

  • 临时使用:HTTP 代理 + 环境变量,最简单
  • 长期使用:SSH 隧道 + autossh,更稳定
  • Git SSH:必须用 SSH 隧道 + ProxyCommand
  • pip/docker:HTTP 代理或 SSH 隧道都可以

评论