curl代理

命令行

http/https代理

  • 命令行 curl 可以直接使用代理:

    curl https://reqbin.com/echo -x myproxy.com:8080 -U login:password
    

举例,我在 Node.js开发环境 需要安装 nvm ,但是 raw.githubusercontent.com 被墙,所以需要通过以下命令通过代理访问:

curl -x 192.168.10.9:3128 -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

socks5代理

  • 命令行 curl 可以使用 socks5h 方式:

    curl -x socks5h://localhost:1080 -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
    

持久化配置

http/https代理

  • 环境设置,注意配置去除本地回环地址过滤,避免一些特殊的访问失败( Homebrew 安装openssl组件测试回环地址端口会失败)

配置curl的http/https代理环境变量
export http_proxy=http://127.0.0.1:3128
export https_proxy=$http_proxy
export no_proxy=localhost,127.0.0.0/8,10.0.0.0/8,172.16.0.0/12,192.168.0.0/16

比较通用的方法是设置操作系统全局环境变量,也就是设置 /etc/environment :

设置操作系统全局环境变量配置 /etc/environment
HTTP_PROXY="http://127.0.0.1:3128"
HTTPS_PROXY="http://127.0.0.1:3128"
NO_PROXY="*.baidu.com,192.168.0.0/16,10.0.0.0/8"
http_proxy="http://127.0.0.1:3128"
https_proxy="http://127.0.0.1:3128"
no_proxy="*.baidu.com,192.168.0.0/16,10.0.0.0/8"
  • 环境变量设置:

配置curl的http/https代理环境变量
export http_proxy=http://192.168.10.9:3128
export https_proxy=$http_proxy
  • 或者采用 ~/.curlrc :

配置 ~/.curlrc 设置http/https代理
proxy=127.0.0.1:3128

socks5代理

比较特别,对于socks代理,变量是使用全部大写字母:

配置curl的socks5代理环境变量
export ALL_PROXY=socks5h://localhost:1080

参考