CentOS 7.8上部署 Squid代理服务器

  • A+

一.准备工作

1.基础环境规划
基础项目基础信息
Suid服务器IP192.168.250.231
测试客户机WIN10家庭版,10.129.1.200,浏览器:火狐
2.安全问题预处理

关闭selinux

  1. [root@mysquid ~]# setenforce 0  
  2. [root@mysquid ~]# vim /etc/selinux/config  
  3. [root@mysquid ~]# cat /etc/selinux/config  
  4.   
  5. # This file controls the state of SELinux on the system.  
  6. # SELINUX= can take one of these three values:  
  7. #     enforcing - SELinux security policy is enforced.  
  8. #     permissive - SELinux prints warnings instead of enforcing.  
  9. #     disabled - No SELinux policy is loaded.  
  10. SELINUX=disabled  
  11. # SELINUXTYPE= can take one of three values:  
  12. #     targeted - Targeted processes are protected,  
  13. #     minimum - Modification of targeted policy. Only selected processes are protected.   
  14. #     mls - Multi Level Security protection.  
  15. SELINUXTYPE=targeted   
  16.   
  17.   
  18. [root@mysquid ~]  

防火墙稍后处理。

二.安装Squid服务

Squid是一个功能全面的缓存代理服务器,它支持著名的网络协议像HTTPHTTPSFTP 等等。将 Squid 放在网页服务器的前端,通过缓存重复请求,过滤网络流量等,可以极大地提高服务器的性能。

1.yum方式安装squid
  1. [root@mysquid ~]# yum -y install squid  
  2. 已加载插件:fastestmirror, langpacks  
  3. Determining fastest mirrors  
  4.  * base: mirrors.aliyun.com  
  5.  * extras: mirrors.aliyun.com  
  6.  * updates: mirrors.aliyun.com  
  7. base                                                                                                                                                                                      | 3.6 kB  00:00:00       
  8. extras                                                                                                                                                                                    | 2.9 kB  00:00:00       
  9. updates                                                                                                                                                                                   | 2.9 kB  00:00:00       
  10. (1/2): extras/7/x86_64/primary_db                                                                                                                                                         | 246 kB  00:00:05       
  11. (2/2): updates/7/x86_64/primary_db                                                                                                                                                        |  15 MB  00:00:44       
  12. 正在解决依赖关系  
  13. --> 正在检查事务  
  14. ---> 软件包 squid.x86_64.7.3.5.20-17.el7_9.6 将被 安装  
  15. --> 正在处理依赖关系 squid-migration-script,它被软件包 7:squid-3.5.20-17.el7_9.6.x86_64 需要  
  16. --> 正在处理依赖关系 perl(Digest::MD5),它被软件包 7:squid-3.5.20-17.el7_9.6.x86_64 需要  
2.启动squid服务
  1. [root@mysquid ~]# systemctl start squid  
  2. [root@mysquid ~]# systemctl enable squid  
  3. Created symlink from /etc/systemd/system/multi-user.target.wants/squid.service to /usr/lib/systemd/system/squid.service.  
  4. [root@mysquid ~]  
3.查看服务启动状态
  1. [root@mysquid ~]# systemctl status squid  
  2. ● squid.service - Squid caching proxy  
  3.    Loaded: loaded (/usr/lib/systemd/system/squid.service; enabled; vendor preset: disabled)  
  4.    Active: active (running) since 一 2022-04-18 11:25:22 CST; 4min 1s ago  
  5.  Main PID: 3019 (squid)  
  6.    CGroup: /system.slice/squid.service  
  7.            ├─3019 /usr/sbin/squid -f /etc/squid/squid.conf  
  8.            ├─3021 (squid-1) -f /etc/squid/squid.conf  
  9.            └─3024 (logfile-daemon) /var/log/squid/access.log  
  10.   
  11. 4月 18 11:25:22 mysquid systemd[1]: Starting Squid caching proxy...  
  12. 4月 18 11:25:22 mysquid squid[3019]: Squid Parent: will start 1 kids  
  13. 4月 18 11:25:22 mysquid squid[3019]: Squid Parent: (squid-1) process 3021 started  
  14. 4月 18 11:25:22 mysquid systemd[1]: Started Squid caching proxy.  
  15. [root@mysquid ~]  

active (running)表示启动成功。

三.配置 Squid

squid默认配置文件在/etc/squid/squid.conf下,默认内容如下:

  1. [root@mysquid ~]# cat /etc/squid/squid.conf  
  2. #  
  3. # Recommended minimum configuration:  
  4. #  
  5.   
  6. # Example rule allowing access from your local networks.  
  7. # Adapt to list your (internal) IP networks from where browsing  
  8. # should be allowed  
  9. acl localnet src 10.0.0.0/8 # RFC1918 possible internal network  
  10. acl localnet src 172.16.0.0/12  # RFC1918 possible internal network  
  11. acl localnet src 192.168.0.0/16 # RFC1918 possible internal network  
  12. acl localnet src fc00::/7       # RFC 4193 local private network range  
  13. acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines  
  14.   
  15. acl SSL_ports port 443  
  16. acl Safe_ports port 80      # http  
  17. acl Safe_ports port 21      # ftp  
  18. acl Safe_ports port 443     # https  
  19. acl Safe_ports port 70      # gopher  
  20. acl Safe_ports port 210     # wais  
  21. acl Safe_ports port 1025-65535  # unregistered ports  
  22. acl Safe_ports port 280     # http-mgmt  
  23. acl Safe_ports port 488     # gss-http  
  24. acl Safe_ports port 591     # filemaker  
  25. acl Safe_ports port 777     # multiling http  
  26. acl CONNECT method CONNECT  
  27.   
  28. #  
  29. # Recommended minimum Access Permission configuration:  
  30. #  
  31. # Deny requests to certain unsafe ports  
  32. http_access deny !Safe_ports  
  33.   
  34. # Deny CONNECT to other than secure SSL ports  
  35. http_access deny CONNECT !SSL_ports  
  36.   
  37. # Only allow cachemgr access from localhost  
  38. http_access allow localhost manager  
  39. http_access deny manager  
  40.   
  41. # We strongly recommend the following be uncommented to protect innocent  
  42. # web applications running on the proxy server who think the only  
  43. # one who can access services on "localhost" is a local user  
  44. #http_access deny to_localhost  
  45.   
  46. #  
  47. # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS  
  48. #  
  49.   
  50. # Example rule allowing access from your local networks.  
  51. # Adapt localnet in the ACL section to list your (internal) IP networks  
  52. # from where browsing should be allowed  
  53. http_access allow localnet  
  54. http_access allow localhost  
  55.   
  56. # And finally deny all other access to this proxy  
  57. http_access deny all  
  58.   
  59. # Squid normally listens to port 3128  
  60. http_port 3128  
  61.   
  62. # Uncomment and adjust the following to add a disk cache directory.  
  63. #cache_dir ufs /var/spool/squid 100 16 256  
  64.   
  65. # Leave coredumps in the first cache dir  
  66. coredump_dir /var/spool/squid  
  67.   
  68. #  
  69. # Add any of your own refresh_pattern entries above these.  
  70. #  
  71. refresh_pattern ^ftp:       1440    20% 10080  
  72. refresh_pattern ^gopher:    1440    0%  1440  
  73. refresh_pattern -i (/cgi-bin/|\?) 0 0%  0  
  74. refresh_pattern .       0   20% 4320  
  75. [root@mysquid ~]  

先进行备份:

  1. [root@mysquid ~]# cp /etc/squid/squid.conf /etc/squid/squid.conf.ori.20220418  
  2. [root@mysquid ~]  

默认情况下,Squid 被配置成在服务器所有网络接口上监听端口3128。 如果想修改端口号,并且设置监听接口,定位到http_port处,并且指定接口 IP 地址和新端口。如果没有接口指定,Squid 将监听所有网络接口。

  1. /etc/squid/squid.conf  
  2. # Squid normally listens to port 3128  
  3. http_port IP_ADDR:PORT  

在所有接口上和默认端口上运行 Squid 对大部分用户都很适用。 可以使用 Access Control Lists (ACLs)来控制 Squid 服务器的访问。 默认情况下,Squid 仅仅允许从本地主机和本地网络来的访问。 如果所有将要使用代理服务器的客户端都有一个固定 IP 地址,你可以创建一个 包含允许 IP 地址的 ACL。 不用在主要配置中添加 IP地址,我们可以创建一个新配置文件,用来配置地址:/etc/squid/myallowed_ips.txt

  1. [root@mysquid squid]# cat squid.conf  
  2.   
  3. acl allowed_ips src "/etc/squid/myallowed_ips.txt"  增加该项  
  4. ……  
  5.   
  6. # Only allow cachemgr access from localhost  
  7. http_access allow localhost manager  
  8. http_access allow allowed_ips   #增加该项  
  9. http_access deny manager  

这里需要注意的是,确认在http_access deny all之前添加了这一行。

http_access指令类似于防火墙规则。

Squid从上到下读取规则,并且后面匹配的规则不会被处理。

接着重启一下squid服务并查看状态:

  1. [root@mysquid squid]# systemctl status squid.service  
  2. ● squid.service - Squid caching proxy  
  3.    Loaded: loaded (/usr/lib/systemd/system/squid.service; enabled; vendor preset: disabled)  
  4.    Active: active (running) since 一 2022-04-18 11:54:04 CST; 4s ago  
  5.   Process: 3353 ExecStop=/usr/sbin/squid -k shutdown -f $SQUID_CONF (code=exited, status=1/FAILURE)  
  6.   Process: 3401 ExecStart=/usr/sbin/squid $SQUID_OPTS -f $SQUID_CONF (code=exited, status=0/SUCCESS)  
  7.   Process: 3393 ExecStartPre=/usr/libexec/squid/cache_swap.sh (code=exited, status=0/SUCCESS)  
  8.  Main PID: 3405 (squid)  
  9.     Tasks: 3  
  10.    CGroup: /system.slice/squid.service  
  11.            ├─3405 /usr/sbin/squid -f /etc/squid/squid.conf  
  12.            ├─3407 (squid-1) -f /etc/squid/squid.conf  
  13.            └─3410 (logfile-daemon) /var/log/squid/access.log  
  14.   
  15. 4月 18 11:53:59 mysquid systemd[1]: Starting Squid caching proxy...  
  16. 4月 18 11:54:04 mysquid squid[3405]: Squid Parent: will start 1 kids  
  17. 4月 18 11:54:04 mysquid squid[3405]: Squid Parent: (squid-1) process 3407 started  
  18. 4月 18 11:54:04 mysquid systemd[1]: Started Squid caching proxy.  
  19. [root@mysquid squid]  

四.Squid 身份验证

Squid 可以使用不同的后端,包括 Samba,LDAP 和 HTTP 基本验证来验证用户。

在本例中,我们配置 Squid 使用基本验证。它是一个简单的验证方法,内置在 HTTP 协议中。

我们将使用openssl来生成密码,并且附加username:password对到文件/etc/squid/htpasswd文件中,并且显示出来:

格式如下:

  1. printf "USERNAME:$(openssl passwd -crypt PASSWORD)\n" | sudo tee -a /etc/squid/htpasswd  

比如,这里创建一个用户名为moonrong,密码为SUse#87ss,运行如下:

  1. [root@mysquid squid]# printf "moonrong:$(openssl passwd -crypt SUse#87ss)\n" | sudo tee -a /etc/squid/htpasswd  
  2. Warning: truncating password to 8 characters  
  3. moonrong:eM1d7DCkceo6g  
  4. [root@mysquid squid]  

下一步是配置 Squid 去启用 HTTP 基本验证,并且使用这个文件。

打开主要配置文件,添加下面的内容: /etc/squid/squid.conf

  1. [root@mysquid squid]# vim squid.conf  
  2. [root@mysquid squid]# cat squid.conf  
  3.   
  4. auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid/htpasswd  
  5. auth_param basic realm proxy  
  6. acl authenticated proxy_auth REQUIRED  
  7. # ...  
  8. http_access allow localnet  
  9. http_access allow localhost  
  10. http_access allow authenticated  
  11. # And finally deny all other access to this proxy  
  12. http_access deny all  

前面三行我们创建了一个 ACL 名称为authenticated,最后一行允许通过验证用户访问。

接着重启squid服务:

  1. [root@mysquid squid]# systemctl restart squid  
  2. [root@mysquid squid]# systemctl status squid  
  3. ● squid.service - Squid caching proxy  
  4.    Loaded: loaded (/usr/lib/systemd/system/squid.service; enabled; vendor preset: disabled)  
  5.    Active: active (running) since 一 2022-04-18 12:04:40 CST; 8s ago  
  6.   Process: 3558 ExecStop=/usr/sbin/squid -k shutdown -f $SQUID_CONF (code=exited, status=0/SUCCESS)  
  7.   Process: 3569 ExecStart=/usr/sbin/squid $SQUID_OPTS -f $SQUID_CONF (code=exited, status=0/SUCCESS)  
  8.   Process: 3563 ExecStartPre=/usr/libexec/squid/cache_swap.sh (code=exited, status=0/SUCCESS)  
  9.  Main PID: 3572 (squid)  
  10.     Tasks: 3  
  11.    CGroup: /system.slice/squid.service  
  12.            ├─3572 /usr/sbin/squid -f /etc/squid/squid.conf  
  13.            ├─3574 (squid-1) -f /etc/squid/squid.conf  
  14.            └─3577 (logfile-daemon) /var/log/squid/access.log  
  15.   
  16. 4月 18 12:04:35 mysquid systemd[1]: Starting Squid caching proxy...  
  17. 4月 18 12:04:40 mysquid squid[3572]: Squid Parent: will start 1 kids  
  18. 4月 18 12:04:40 mysquid squid[3572]: Squid Parent: (squid-1) process 3574 started  
  19. 4月 18 12:04:40 mysquid systemd[1]: Started Squid caching proxy.  
  20. [root@mysquid squid]  

五.防火墙端口放行配置

squid服务需要打开3128端口:

  1. [root@mysquid /]# firewall-cmd --permanent --add-port=3128/tcp  
  2. success  
  3. [root@mysquid /]# firewall-cmd --reload  
  4. success  
  5. [root@mysquid /]  

六.客户端配置并测试

以火狐浏览器为例,设置代理服务器,进行测试。

CentOS 7.8上部署 Squid代理服务器
moonrong
  • 版权声明:本站原创文章,于2022年11月15日08:19:02,由 发表,共 10401 字。
  • 版权声明: 本文由于2022年11月15日08:19:02 发表在 好派笔记,共 10401 字。

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: