squid代理及常见的代理上网
本文中涉及两台服务器,这两台服务器均有内网
ip
地址,分别为
123 A: 192.168.0.200B: 192.168.0.100C: 192.168.0.101
其中A
具有公网访问能力,B
和C
不具备公网访问能力。
1、安装squid服务
1 |
sudo yum install -y squid |
1、默认安装的squid
服务只需要配置一点,默认是拒绝所有服务器连接,只需要修改成允许所有服务器连接即可
1 2 |
# sudo vim /etc/squid/squid.conf http_access allow all |
2、启动服务并设置成开机启动
1 2 |
# sudo systemctl start squid.service # sudo systemctl enable squid.service |
3、配置受信
这样配置之后,squid
代理服务器就可以使用了,默认的端口是3128
,但是为了安全,只让受信的服务器连接,通常还需要对squid
配置账号验证授权使用,通过httpd-tools
生成密码文件,安装
1 |
# yum install httpd-tools -y |
生成密码文件
1 2 3 4 |
# sudo mkdir /etc/squid3/ # 生成密码文件,指定文件路径,其中squid是用户名 # sudo htpasswd -cd /etc/squid3/passwords squid #提示输入密码,不能超过8个字符,输入密码123456 |
测试密码文件
1 2 3 4 |
# /usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords squid 123456 OK # 测试完成,crtl + c 打断 |
配置squid
使用验证,修改配置文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
# sudo vim /etc/squid/squid.conf # And finally deny all other access to this proxy auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords #账户密码文件 auth_param basic realm proxy auth_param basic children 50 #最多 50 个账户同时运行 auth_param basic realm CoolTube Proxy Server #密码框描述 auth_param basic credentialsttl 2 hours #认证持续时间 acl authenticated proxy_auth REQUIRED #对 authenticated 进行外部认证 http_access allow authenticated #允许 authenticated 中的成员访问 http_access deny all #拒绝所有其他访问 visible_hostname squid.CoolTube #代理机名字 # 这里是端口号,可以按需修改 # http_port 3128 这样写会同时监听 ipv6 和 ipv4 的端口,推荐适应下面的配置方法。 http_port 0.0.0.0:3128 |
初始化服务并重新启动
1 2 |
sudo squid -z sudo systemctl restart squid.service |
如果开启了防火墙,还要在防火墙中允许端口3128
的策略,以CentOS7
为例
1 2 3 4 |
# 把 3128 端口加入防火墙过滤掉 sudo firewall-cmd --permanent --zone=public --add-port=3128/tcp # 重启防火墙 sudo firewall-cmd --reload |
2、配置代理
2.1、配置全局代理
修改环境变量文件并使其生效
1 2 3 4 5 6 7 8 |
sudo vim /etc/profile #在最后加入(有认证的情况) export http_proxy="http://squid:123456@192.168.0.200:3128" export https_proxy="http://squid:123456@192.168.0.200:3128" 如果没有开启认证 http_proxy=http://192.168.0.200:3128 https_proxy=http://192.168.0.200:3128 # source /etc/profile |
2.2、配置wget代理
新增wget
的环境变量文件
1 2 3 4 5 6 7 8 |
# sudo vim ~/.wgetrc http_proxy=http://192.168.0.200:3128 https_proxy=http://192.168.0.200:3128 use_proxy = on wait = 30 # 验证,如果返回值为0表示wget可用 # sudo wget --spider -T 5 -q -t 2 www.baidu.com | echo $? 0 |
2.3、配置yum代理
编辑yum
的配置文件,新增下面的内容
1 2 3 4 5 6 7 |
# vim /etc/yum.conf # 有认证的情况 # Proxy proxy=http://squid:123456@192.168.0.200:3128 # 没有认证 # Proxy proxy=http://192.168.0.200:3128 |
2.4、配置docker代理
docker
配置代理是为了保证docker
能从外网pull
镜像,在安装了docker
之后,在docker
配置文件下新增配置并重启docker
1 2 3 4 5 6 |
# mkdir -p /etc/systemd/system/docker.service.d # vim /etc/systemd/system/docker.service.d/http-proxy.conf [Service] Environment="HTTP_PROXY=squid:123456@192.168.0.200:3128" # systemctl daemon-reload # systemctl restart docker |
验证,通过docker pull
命令pull
一个公网的镜像
1 |
docker pull busybox:latest |
0 Comments