使用场景:

github国内访问不稳定,我们可以在日本或者香港的ECS上部署个squid,给github的访问提提速。docker pul拉镜像慢的问题,也可以通过这种方式来解决。



使用squid实现正向代理_正向代理


在192.168.0.121执行:

yum install squid -y


vim/etc/squid/squid.conf  修改下配置文件

acl local src 192.168.0.0/24        //允许192.168.0.0/24网段内所有客户机访问代理服务器

http_access allow localnet         //该记录一定要添在deny all之前

http_port 3128


设置开机自启动

systemctl start squid

systemctl enable squid



在192.168.0.125执行:

exporthttp_proxy=http://192.168.0.121:3128

exporthttps_proxy=http://192.168.0.121:3128


echo "exporthttp_proxy=http://192.168.0.121:3128" >>/etc/profile

echo "exporthttps_proxy=http://192.168.0.121:3128" >>/etc/profile


然后执行访问公网的测试请求:

1、wget ​http://www.cmake.org/files/v3.3/cmake-3.3.1.tar.gz​​​​

2、curlwww.baidu.com


到192.168.0.121的squid上查看日志:

$ cat /var/log/squid/access.log

1633701689.382   6107 192.168.0.125 TCP_MISS/301 596 GEThttp://www.cmake.org/files/v3.3/cmake-3.3.1.tar.gz - HIER_DIRECT/66.194.253.25 text/html

1633701698.955   9573 192.168.0.125 TCP_MISS/301 598 GEThttp://cmake.org/files/v3.3/cmake-3.3.1.tar.gz - HIER_DIRECT/66.194.253.25 text/html

1633701732.869  33889 192.168.0.125 TCP_TUNNEL/200 291833CONNECT cmake.org:443 - HIER_DIRECT/66.194.253.25 -


1633701741.520   5036 192.168.0.125 TCP_MISS/200 2888 GEThttp://www.baidu.com/ - HIER_DIRECT/180.101.49.12 text/html






上面只是一个最简单的squid的配置,生产上可能比较复杂,可以参考这篇:​


附带一个我生产上用的配置(没有开密码验证):

cat/etc/squid/squid.conf | egrep -v '^#|^$'

acl localnet src 10.0.0.0/8  # RFC1918 possible internal network

acl localnet src 172.16.0.0/12   # RFC1918 possible internal network

acl localnet src 192.168.0.0/16  # RFC1918 possible internal network

acl localnet src fc00::/7       # RFC 4193 local private network range

acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged)machines

acl net src all

acl SSL_ports port 443

acl Safe_ports port 80       # http

acl Safe_ports port 21       # ftp

acl Safe_ports port 443      # https

acl Safe_ports port 70       # gopher

acl Safe_ports port 210      # wais

acl Safe_ports port 1-65535  # unregistered ports

acl Safe_ports port 280      # http-mgmt

acl Safe_ports port 488      # gss-http

acl Safe_ports port 591      # filemaker

acl Safe_ports port 777      # multiling http

acl CONNECT method CONNECT

http_access deny !Safe_ports

http_access allow net

http_access allow localhost manager

http_access deny manager

http_access allow localnet

http_access allow localhost

http_access deny all

http_port 3128

coredump_dir /var/spool/squid

refresh_pattern ^ftp:    1440   20% 10080

refresh_pattern ^gopher:  1440   0%  1440

refresh_pattern -i (/cgi-bin/|\?) 0  0%  0

refresh_pattern .     0   20% 4320



上面我们没开启密码验证,因此最好在SLB上做下限制下,只允许国内的某几个IP去访问squid服务。



代理dockersystemd

参考:​​https://cloud.tencent.com/developer/article/1806455​


mkdir -pv /etc/systemd/system/docker.service.d/


vim/etc/systemd/system/docker.service.d/proxy.conf

[Service]

Environment="HTTP_PROXY=http://1.2.3.4:3128/"

Environment="HTTPS_PROXY=http://1.2.3.4:3128/"

Environment="NO_PROXY=localhost,127.0.0.1,.example.com"


dockerd 代理的修改比较特殊,它实际上是改 systemd 的配置,因此需要重载 systemd 并重启 dockerd 才能生效。

systemctl daemon-reload

systemctl restart docker


代理dockerbuild

虽然 docker build 的本质,也是启动一个容器,但是环境会略有不同,用户级配置无效。在构建时,需要注入 http_proxy 等参数。


docker build . \

--build-arg"HTTP_PROXY=http://proxy.example.com:8080/" \

--build-arg"HTTPS_PROXY=http://proxy.example.com:8080/" \

--build-arg"NO_PROXY=localhost,127.0.0.1,.example.com" \

-t your/image:tag


注意:无论是 dockerrun 还是 docker build,默认是网络隔绝的。如果代理使用的是 localhost:3128 这类,则会无效。这类仅限本地的代理,必须加上 --network host 才能正常使用。而一般则需要配置代理的外部IP,而且代理本身要开启 Gateway 模式。


dockerbuild 代理是在执行前设置的,所以修改后,下次执行立即生效。


Linux上代理http和https

export http_proxy=http://1.2.3.4:3128

export https_proxy=http://1.2.3.4:3128

上面地址替换成你实际squid地址就行


不太建议写到/etc/profile这类的配置文件里面,这类访问慢的情况毕竟是少数,用到时候临时开启下就行。



Linux上代理git http/https方式


# cat /root/.gitconfig

[user]

    name= xxxxx

[http]

    proxy= http://1.2.3.4:3128

[https]

    proxy= http://1.2.3.4:3128


或者使用git命令方式添加


git config --global http.proxy http://proxy.yourcompany.com:3128

git config --global https.proxy http://proxy.yourcompany.com:3128


Linux上代理git socket方式

可以参考这篇,具体本人没有实操过,不确定。


​https://digglife.net/articles/squid-for-git-proxy.html​