Git/NPM/Docker 魔法代理设置

Git 设置代理

分两种情况:第一种是 http/https协议,第二种是 ssh协议;

http/https 协议

 

# HTTP/HTTPS 协议,port 需与代理软件设置的一致
# 格式:git config –-global http.proxy http://127.0.0.1:port
git config –-global http.proxy http://127.0.0.1:7890

# SOCKS5 协议
# 格式:git config --global http.proxy socks5://127.0.0.1:port
git config --global http.proxy socks5://127.0.0.1:7890

# 注意:http 和 socks5 两者只能选一个

注意:

  • --glboal 指 修改 Git 的全局配置文件 ~/.gitconfig,而非各个 Git 仓库里的配置文件.git/config
  • port 代理端口
  • Git 不认 https.proxy,设置http.proxy就可以支持 https 了。

ssh 协议

通过使用 ssh 处理连接时,为了通过代理进行连接,必须配置 ssh 本身,在 ~/.ssh/config 文件中设置 ProxyCommand 选项。Linux 和 macOS 是通过 nc 来执行 ProxyCommand 的,Windows 下则是通过 connect。

Linux/Mac

编辑 ~/.ssh/config 文件,给文件加上如下对应内容

# HTTP 代理
Host github.com
    User git
    ProxyCommand nc -X connect -x 127.0.0.1:7890 %h %p
  • Host 后面 接的 github.com 是指定要走代理的仓库域名。
  • 在 ProxyCommand 中,Linux 和 macOS 用户用的是 nc。
  • -X 选项后面接的是 connect 的意思是 HTTPS 代理。
  • -x 选项后面加上代理地址和端口号。
  • 在调用 ProxyCommand 时,%h 和 %p 将会被自动替换为目标主机名和 SSH 命令指定的端口(%h 和 %p 不要修改,保留原样即可)。
# SOCKS5 协议
# 两种方式任选一个

# 第一种
Host github.com
    User git
    ProxyCommand nc -X 5 -x 127.0.0.1:7891 %h %p
    
# 第二种
Host github.com
    User git
    ProxyCommand nc -x 127.0.0.1:7891 %h %p
  • Host 后面 接的 github.com 是指定要走代理的仓库域名。
  • 在 ProxyCommand 中,Linux 和 macOS 用户用的是 nc 。
  • 在调用 ProxyCommand 时,%h 和 %p 将会被自动替换为目标主机名和 SSH 命令指定的端口( %h 和 %p 不要修改,保留原样即可)。
  • 如果 -X 选项后面接的是数字 5,那么指的就是 socks5 代理。
  • 当然不写上 -X 选项也是可以的,因为在没有指定协议的情况下,默认是使用socks5代理的。所以2 种的写法效果一样 ,都指的是走 socks5 代理

Window

编辑 ~/.ssh/config 文件,给文件加上如下对应内容.windows 的 ~ 路径一般是C:\Users\用户名,可在 git bash 中 输入 cd ~进入 ~目录,再用pwd命令显示当前路径.

# HTTP代理
Host github.com
User git
ProxyCommand connect -H 127.0.0.1:7890 %h %p

# SOCKS5代理
Host github.com
User git
ProxyCommand connect -S 127.0.0.1:7891 %h %p
  • Host 后面 接的 github.com 是指定要走代理的仓库域名。
  • 在 ProxyCommand 中,Windows 用户用的是 connect。
  • -H 选项的意思是 HTTP 代理
  • -S 选项指的就是 socks5 代理
  • 在调用 ProxyCommand 时,%h 和 %p 将会被自动替换为目标主机名和 SSH 命令指定的端口( %h 和 %p 不要修改,保留原样即可)。

 

Nodejs/NPM

安装好node后,输入 npm config list 查看npmrc文件所在目录,打开此文件

输入下面内容:

proxy=http://127.0.0.1:7890
redistry=http://registry.npm.taobao.org/
https_proxy=http://127.0.0.1:7890

如果找不到路径,请输入 npm config list -l 来查看 globalconfig 和 userconfig 的信息,在对应目录中创建npmrc文件并将上面内容输入进去即可;

 

Docker

 配置docker通过代理服务器拉取镜像

问题现象
如果不配置代理服务器就直接拉镜像,docker 会直接尝试连接镜像仓库,并且连接超时报错。如下所示:

$ docker pull busybox
Using default tag: latest
Error response from daemon: Get https://registry-1.docker.io/v2/: net/http: request canceled 
while waiting for connection (Client.Timeout exceeded while awaiting headers)

 具体方法:https://www.cnblogs.com/abc1069/p/17496240.html

posted @ 2022-04-12 16:50  醉马踏千秋  阅读(962)  评论(0编辑  收藏  举报