实现与CentOS7,其他版本请自行变动。

环境搭建

具体搭建参考 使用Squid搭建CentOS7代理服务器上网 部分内容参考此文章

安装软件

终端执行命令

yum install squid -y
yum install httpd-tools -y

生成密码文件

创建文件夹

mkdir /etc/squid3/
#ikaros 是用户名
htpasswd -cd /etc/squid3/passwords ikaros
#提示输入密码,比如输入123456

CentOS7搭建Squid代理服务器,代理QQ_SSL

测试密码文件

/usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords
#输入用户名 密码
ikaros  123456
#提示ok说明成功
ok
#ctrl+c退出

CentOS7搭建Squid代理服务器,代理QQ_squid_02

配置squid.conf文件

vi /etc/squid/squid.conf

#在最后添加
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated

修改下面这两处

# And finally deny all other access to this proxy
http_access allow all

#这里是端口号,可以按需修改
#http_port 3128 这样写会同时监听ipv6和ipv4的端口,推荐适应下面的配置方法。
http_port 0.0.0.0:3128

http_access deny CONNECT !SSL_ports改为http_access allow CONNECT !SSL_ports 完整配置文件参考

#
# Recommended minimum configuration:
#

# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
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 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 1025-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

#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
http_access deny !Safe_ports

# Deny CONNECT to other than secure SSL ports
http_access allow CONNECT !SSL_ports

# Only allow cachemgr access from localhost
http_access allow localhost manager
http_access deny manager

# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost

#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#

# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
http_access allow localnet
http_access allow localhost

# And finally deny all other access to this proxy
http_access allow all

# Squid normally listens to port 3128
http_port 3128

# Uncomment and adjust the following to add a disk cache directory.
#cache_dir ufs /var/spool/squid 100 16 256

# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid

#
# Add any of your own refresh_pattern entries above these.
#
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

auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords
auth_param basic realm proxy
acl authenticated proxy_auth REQUIRED
http_access allow authenticated

日志

squid的日志位于/var/log/squid/目录下。

启动,停止,重启等

#启动start
systemctl start squid.service
#停止stop
systemctl stop squid.service
#重启stop
systemctl restart squid.service
#配置开机自启动
systemctl enable squid.service
#关闭开机自启动
systemctl disable squid.service
#查看运行状态
systemctl status squid.service

CentOS7搭建Squid代理服务器,代理QQ_squid_03

Windows下客户端测试

根据以上步骤安装完后,Windows下安装Proxifier

CentOS7搭建Squid代理服务器,代理QQ_d3_04


点击文件下面的按钮,新建代理服务器

CentOS7搭建Squid代理服务器,代理QQ_SSL_05

CentOS7搭建Squid代理服务器,代理QQ_代理服务器_06


填完信息后我们检查一下

CentOS7搭建Squid代理服务器,代理QQ_squid_07


正常情况下是这样的

CentOS7搭建Squid代理服务器,代理QQ_重启_08

如果报错

可以采取解决方案:

1、是否开放了服务器端口,例如云服务器安全组开放出入站规则,加入3128端口

2、

CentOS7搭建Squid代理服务器,代理QQ_d3_09


这种情况需要修改配置文件 /etc/squid/squid.conf。

http_access deny CONNECT !SSL_ports改为http_access allow CONNECT !SSL_ports 3、开头文章教程里的这一块部分是否做了配置

CentOS7搭建Squid代理服务器,代理QQ_SSL_10


4、这一部分是否也 allow

CentOS7搭建Squid代理服务器,代理QQ_重启_11


5、以上步骤结束后,重启squid

systemctl restart squid

配置规则

CentOS7搭建Squid代理服务器,代理QQ_squid_12


关闭默认的 localhost 和 default

CentOS7搭建Squid代理服务器,代理QQ_重启_13


针对对应程序进行代理设置

CentOS7搭建Squid代理服务器,代理QQ_squid_14


我这里以QQ为例

CentOS7搭建Squid代理服务器,代理QQ_重启_15


配置好后,打开QQ,点击设置

CentOS7搭建Squid代理服务器,代理QQ_squid_16


填入信息,默认的3128端口

CentOS7搭建Squid代理服务器,代理QQ_SSL_17


测试一下

CentOS7搭建Squid代理服务器,代理QQ_重启_18


就可以登录QQ了

CentOS7搭建Squid代理服务器,代理QQ_d3_19


回到 proxifier

CentOS7搭建Squid代理服务器,代理QQ_代理服务器_20


可以看到QQ已经接入代理了。