iTerm2 配置 http 代理

终端下的很多应用会通过 http_proxyhttps_proxy 环境变量的影响,比如 curl/brew/wget。如果设置了这些变量,应用会直接通过该变量设置的代理访问目的地址。 因此我们可以使用如下的命令配置代理。

1
2
3
export https_proxy=http://127.0.0.1:7890 
export http_proxy=http://127.0.0.1:7890 
export all_proxy=socks5://127.0.0.1:7890

为了方便开启和关闭代理,可以再终端的配置文件(比如 zsh,那可以在 .zshrc)中加上响应的 alias。

1
2
3
# 终端开启 proxy
alias pon="export no_proxy=localhost,127.0.0.1,localaddress,.netease.com;export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890"
alias poff="unset https_proxy http_proxy all_proxy"
1
2
3
4
5
6
7
8
➜  guixian-blog git:(master) ✗ curl -I https://github.com -vvv
* Uses proxy env variable no_proxy == 'localhost,127.0.0.1,localaddress,.netease.com'
* Uses proxy env variable https_proxy == 'http://127.0.0.1:7890'
*   Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to 127.0.0.1 (127.0.0.1) port 7890 (#0)
* allocate connect buffer!
* Establish HTTP proxy tunnel to github.com:443