配置 Windows 终端代理

auther: abinng date: 2026-01-31 17:52 createDate:2026-01-31 15:05

前言

我在WSL2 安装与配置中配置wsl代理时,提前试了一下windows的终端代理,发现不行,本文诞生

正文

使用curl尝试连接谷歌

1
curl.exe -I -v https://www.google.com

发现显示的是超时、连接失败等关键词

那么接下来来配置一下代理(PowerShell)

提一嘴,我的是Clash Verge,其他代理软件的端口号可能不同

1
2
$Env:http_proxy="http://127.0.0.1:7897"
$Env:https_proxy="http://127.0.0.1:7897"

再次尝试:

1
curl.exe -I -v https://www.google.com

会返回一长串,看第一句就好:

file-20260131151532412

成功了

快捷命令

不能每次需要代理的时候都输入一长串环境变量吧,太麻烦了

PowerShell有一个类似于Linux的.bashrc的配置文件,$PROFILE

接着我们来配置快捷命令

首先输入

1
if (!(Test-Path -Path $PROFILE)) { New-Item -ItemType File -Path $PROFILE -Force }

意思是:如果 $PROFILE 这个文件不存在,就强制创建它(连同缺少的文件夹一起创建)。

接着用记事本打开

1
notepad $PROFILE

将以下代码粘贴进去:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
function proxy {
$env:http_proxy = "http://127.0.0.1:7897"
$env:https_proxy = "http://127.0.0.1:7897"
[System.Net.WebRequest]::DefaultWebProxy = New-Object System.Net.WebProxy("http://127.0.0.1:7897")
Write-Host "Proxy enabled: http://127.0.0.1:7897" -ForegroundColor Green
}

function unproxy {
$env:http_proxy = $null
$env:https_proxy = $null
[System.Net.WebRequest]::DefaultWebProxy = $null
Write-Host "Proxy disabled" -ForegroundColor Yellow
}

function check-proxy {
if ($env:http_proxy -or $env:https_proxy) {
Write-Host "Current proxy settings:" -ForegroundColor Cyan
Write-Host "HTTP Proxy: $env:http_proxy"
Write-Host "HTTPS Proxy: $env:https_proxy"
} else {
Write-Host "No proxy is currently set." -ForegroundColor Cyan
}
}

重启终端,输入proxy

file-20260131153442253

输入unproxy即可关闭,输入check-proxy即可检查是否正在开着代理

一些注意事项

代理软件开着,但是没开系统代理,是可以配置终端代理的,因为代理软件随时监听那个端口