Linux-Squid代理服务器

一.Squid的简介
Squid是一个支持HTTP HTTPS FTP等服务的Web缓存代理服务,可以通过缓存页面实现降低带宽占用并优化页面响应时间。此外,Squid还具有强大的访问功能。
Squid代码请求流程为:客户端访问Squid代理服务器,由代理服务器代表客户访问后端真实服务器,真实服务器将响应数据返回给代理服务器,代理服务器将数据返回给客户,同时将页面缓存到本地内存及硬盘中,当下次客户端再有响应要求时,代理服务器将直接从本地缓存中提取数据返回给客户端。
代理服务器有多种类型,一般意义上常分为标准正向代理服务器 透明代理服务器 反向代理服务器
二.Squid的正向代理
正向代理:代理服务器位于客户端和真实服务器之间的服务器,为了从真实服务器取得内容,客户端向代理发送一个请求并指定目标(真实服务器),然后代理向真实服务器转交请求并将获得的内容返回给客户端。客户端才能使用正向代理。
客户端主动寻找代理服务器,为了通过代理服务器访问自己本身无法直接访问的主机,客户端借由正向代理可以间接访问很多不同互联网服务器的资源
正向代理作为客户端的代理,将从互联网上获取的资源返回给一个或多个的客户端,服务端(如Web服务器)只知道代理的IP地址而不知道客户端的IP地址。
实验前提:两台虚拟机,一台有网作为真实服务器,一台没网作为客户端
1.服务端虚拟主机的设定
(1)设置网络保证有网,火墙处于开启状态

[root@localhost ~]# cat /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=none
DEVICE=eth0
ONBOOT=yes
DNS1=114.114.114.114
IPADDR=172.25.4.105  ##ip
PREFIX=24  ##子网掩码
GATEWAY=172.25.4.250  ##网关
[root@localhost ~]# systemctl status firewalld.service 
● firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; disabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-05-04 22:12:28 CST; 38min ago
     Docs: man:firewalld(1)  ##火墙开启状态
 Main PID: 4337 (firewalld)
   CGroup: /system.slice/firewalld.service
           └─4337 /usr/bin/python -Es /usr/sbin/firewalld --nofork --nopid

May 04 22:12:28 localhost.localdomain systemd[1]: Starting firewalld - dynami...
May 04 22:12:28 localhost.localdomain systemd[1]: Started firewalld - dynamic...
May 04 22:23:47 localhost.localdomain firewalld[4337]: ERROR: UNKNOWN_INTERFA...
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# ping www.baidu.com  ##验证网络正常
PING www.a.shifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=1 ttl=53 time=59.7 ms
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=2 ttl=53 time=67.5 ms
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=3 ttl=53 time=76.4 ms
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=4 ttl=53 time=83.9 ms

(2)Squid服务器的安装

[root@localhost ~]# yum install squid -y
Loaded plugins: langpacks, product-id, search-disabled-repos, subscription-
              : manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
westos                                                   | 4.1 kB     00:00     
Resolving Dependencies
--> Running transaction check
---> Package squid.x86_64 7:3.5.20-2.el7 will be installed
--> Processing Dependency: perl(Digest::MD5) for package: 7:squid-3.5.20-2.el7.x86_64
--> Processing Dependency: squid-migration-script for package: 7:squid-3.5.20-2.el7.x86_64
--> Processing Dependency: libecap.so.3()(64bit) for package: 7:squid-3.5.20-2.el7.x86_64
--> Running transaction check
---> Package libecap.x86_64 0:1.0.0-1.el7 will be installed
---> Package perl-Digest-MD5.x86_64 0:2.52-3.el7 will be installed
--> Processing Dependency: perl(Digest::base) >= 1.00 for package: perl-Digest-MD5-2.52-3.el7.x86_64
---> Package squid-migration-script.x86_64 7:3.5.20-2.el7 will be installed
--> Running transaction check
---> Package perl-Digest.noarch 0:1.17-245.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package                     Arch        Version              Repository   Size
================================================================================
Installing:
 squid                       x86_64      7:3.5.20-2.el7       westos      3.0 M
Installing for dependencies:
 libecap                     x86_64      1.0.0-1.el7          westos       21 k
 perl-Digest                 noarch      1.17-245.el7         westos       23 k
 perl-Digest-MD5             x86_64      2.52-3.el7           westos       30 k
 squid-migration-script      x86_64      7:3.5.20-2.el7       westos       47 k

Transaction Summary
================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 3.1 M
Installed size: 10 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                               17 MB/s | 3.1 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : 7:squid-migration-script-3.5.20-2.el7.x86_64                 1/5 
  Installing : libecap-1.0.0-1.el7.x86_64                                   2/5 
  Installing : perl-Digest-1.17-245.el7.noarch                              3/5 
  Installing : perl-Digest-MD5-2.52-3.el7.x86_64                            4/5 
  Installing : 7:squid-3.5.20-2.el7.x86_64                                  5/5 
  Verifying  : 7:squid-3.5.20-2.el7.x86_64                                  1/5 
  Verifying  : perl-Digest-1.17-245.el7.noarch                              2/5 
  Verifying  : perl-Digest-MD5-2.52-3.el7.x86_64                            3/5 
  Verifying  : libecap-1.0.0-1.el7.x86_64                                   4/5 
  Verifying  : 7:squid-migration-script-3.5.20-2.el7.x86_64                 5/5 

Installed:
  squid.x86_64 7:3.5.20-2.el7                                                   

Dependency Installed:
  libecap.x86_64 0:1.0.0-1.el7                                                  
  perl-Digest.noarch 0:1.17-245.el7                                             
  perl-Digest-MD5.x86_64 0:2.52-3.el7                                           
  squid-migration-script.x86_64 7:3.5.20-2.el7                                  

Complete!
[root@localhost ~]# systemctl start squid

(3)Squid服务器的设置

[root@localhost ~]# netstat -antlupe | grep squid  ##查看squid服务端口为3128
tcp6       0      0 :::3128                 :::*                    LISTEN      0          80808      7627/(squid-1)      
udp        0      0 0.0.0.0:37751           0.0.0.0:*                           23         80805      7627/(squid-1)      
udp6       0      0 :::56003                :::*                                23         80804      7627/(squid-1)      
[root@localhost ~]# firewall-cmd --add-port=3128/tcp  ##火墙添加3128端口
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ftp http ssh
  ports: 443/tcp 3128/tcp  ##添加成功
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 
	
[root@localhost ~]# vim /etc/squid/squid.conf  ##编辑squid主配置文件

Linux-Squid代理服务器_第1张图片

[root@localhost ~]# ll /var/spool/squid/  ##此目录用于存放缓存文件,此时无缓存文件
total 0
[root@localhost ~]# systemctl restart squid.service  ##重启服务 
[root@localhost ~]# ll /var/spool/squid/  ##缓存文件出现
total 196
drwxr-x---. 258 squid squid 8192 May  4 23:02 00
drwxr-x---. 258 squid squid 8192 May  4 23:02 01
drwxr-x---. 258 squid squid 8192 May  4 23:02 02
drwxr-x---. 258 squid squid 8192 May  4 23:02 03
drwxr-x---. 258 squid squid 8192 May  4 23:02 04
drwxr-x---. 258 squid squid 8192 May  4 23:02 05
drwxr-x---. 258 squid squid 8192 May  4 23:02 06
drwxr-x---. 258 squid squid 8192 May  4 23:02 07
drwxr-x---. 258 squid squid 8192 May  4 23:02 08
drwxr-x---. 258 squid squid 8192 May  4 23:02 09
drwxr-x---. 258 squid squid 8192 May  4 23:02 0A
drwxr-x---. 258 squid squid 8192 May  4 23:02 0B
drwxr-x---. 258 squid squid 8192 May  4 23:02 0C
drwxr-x---. 258 squid squid 8192 May  4 23:02 0D
drwxr-x---. 258 squid squid 8192 May  4 23:02 0E
drwxr-x---. 258 squid squid 8192 May  4 23:02 0F
-rw-r-----.   1 squid squid   72 May  4 23:02 swap.state

2.客户端虚拟主机的设定

[root@localhost ~]# ping www.baidu.com
ping: unknown host www.baidu.com
[root@localhost ~]# ping 114.114.114
connect: Network is unreachable

浏览器端测试及设定
Linux-Squid代理服务器_第2张图片
Linux-Squid代理服务器_第3张图片
Linux-Squid代理服务器_第4张图片
Linux-Squid代理服务器_第5张图片
三.Squid的反向代理
反向代理是服务器根据客户端的请求,从其关系的一组或多组后端服务器上获取资源,然后再将这些资源返回给客户端,客户端只会得知反向代理的IP地址,而不知道在代理服务器后面的服务器簇的存在
反向代理是作为服务器端(如Web服务器)的代理使用,反向代理是供很多客户端都通过它间接访问不同后端服务器上的资源,而不需要知道这些后端服务器的存在,而以为所有资源都来自于这个反向代理服务器。
企业自己进行设置 让客户进行访问时候访问代理服务器看到资源服务器的东西,代理服务器中并没有资源
实验前提:三台主机
1.服务器端:有http 无squid并且关闭火墙
2.代理端 无http 有squid
3.客户端进行测试
服务端的设置:172.25.4.104

[root@localhost ~]# systemctl stop firewalld.service   ##关闭火墙
[root@localhost ~]# systemctl status firewalld.service  ##查看火墙状态 
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: inactive (dead) since Sat 2019-05-04 12:25:38 EDT; 10s ago
 Main PID: 498 (code=exited, status=0/SUCCESS)

May 04 12:15:35 localhost systemd[1]: Started firewalld - dynamic firewall ...n.
May 04 12:25:38 localhost systemd[1]: Stopping firewalld - dynamic firewall.....
May 04 12:25:38 localhost systemd[1]: Stopped firewalld - dynamic firewall ...n.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# yum install httpd -y
Loaded plugins: langpacks
Resolving Dependencies
--> Running transaction check
---> Package httpd.x86_64 0:2.4.6-45.el7 will be installed
--> Processing Dependency: httpd-tools = 2.4.6-45.el7 for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: /etc/mime.types for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libapr-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Processing Dependency: libaprutil-1.so.0()(64bit) for package: httpd-2.4.6-45.el7.x86_64
--> Running transaction check
---> Package apr.x86_64 0:1.4.8-3.el7 will be installed
---> Package apr-util.x86_64 0:1.5.2-6.el7 will be installed
---> Package httpd-tools.x86_64 0:2.4.6-45.el7 will be installed
---> Package mailcap.noarch 0:2.1.41-2.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package             Arch           Version                Repository      Size
================================================================================
Installing:
 httpd               x86_64         2.4.6-45.el7           westos         1.2 M
Installing for dependencies:
 apr                 x86_64         1.4.8-3.el7            westos         103 k
 apr-util            x86_64         1.5.2-6.el7            westos          92 k
 httpd-tools         x86_64         2.4.6-45.el7           westos          84 k
 mailcap             noarch         2.1.41-2.el7           westos          31 k

Transaction Summary
================================================================================
Install  1 Package (+4 Dependent packages)

Total download size: 1.5 M
Installed size: 4.3 M
Downloading packages:
--------------------------------------------------------------------------------
Total                                              9.0 MB/s | 1.5 MB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : apr-1.4.8-3.el7.x86_64                                       1/5 
  Installing : apr-util-1.5.2-6.el7.x86_64                                  2/5 
  Installing : httpd-tools-2.4.6-45.el7.x86_64                              3/5 
  Installing : mailcap-2.1.41-2.el7.noarch                                  4/5 
  Installing : httpd-2.4.6-45.el7.x86_64                                    5/5 
  Verifying  : httpd-tools-2.4.6-45.el7.x86_64                              1/5 
  Verifying  : apr-util-1.5.2-6.el7.x86_64                                  2/5 
  Verifying  : mailcap-2.1.41-2.el7.noarch                                  3/5 
  Verifying  : httpd-2.4.6-45.el7.x86_64                                    4/5 
  Verifying  : apr-1.4.8-3.el7.x86_64                                       5/5 

Installed:
  httpd.x86_64 0:2.4.6-45.el7                                                   

Dependency Installed:
  apr.x86_64 0:1.4.8-3.el7                 apr-util.x86_64 0:1.5.2-6.el7       
  httpd-tools.x86_64 0:2.4.6-45.el7        mailcap.noarch 0:2.1.41-2.el7       

Complete!
[root@localhost ~]# systemctl stop firewalld.service 
[root@localhost ~]# systemctl status firewalld.service 
firewalld.service - firewalld - dynamic firewall daemon
   Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled)
   Active: inactive (dead) since Sat 2019-05-04 12:25:38 EDT; 10s ago
 Main PID: 498 (code=exited, status=0/SUCCESS)

May 04 12:15:35 localhost systemd[1]: Started firewalld - dynamic firewall ...n.
May 04 12:25:38 localhost systemd[1]: Stopping firewalld - dynamic firewall.....
May 04 12:25:38 localhost systemd[1]: Stopped firewalld - dynamic firewall ...n.
Hint: Some lines were ellipsized, use -l to show in full.
[root@localhost ~]# cd /var/www/html/
[root@localhost html]# ls
[root@localhost html]# vim index.html
[root@localhost html]# cat index.html

www.shenzhen.com

[root@localhost html]# systemctl restart httpd [root@localhost html]# systemctl enable httpd ln -s '/usr/lib/systemd/system/httpd.service' '/etc/systemd/system/multi-user.target.wants/httpd.service'

代理端设置:172.25.4.105

[root@localhost ~]# firewall-cmd --permanent --add-port=80/tcp  ##添加80端口
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: eth0
  sources: 
  services: dhcpv6-client ftp http ssh
  ports: 443/tcp 3128/tcp 80/tcp
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 
[root@ftp_server ~]# vim /etc/squid/squid.conf  ##编辑主配置文件
[root@ftp_server ~]# systemctl restart squid

在这里插入图片描述
59 http_port 80 vhost vport ##允许通过的端口为80,80为虚拟化端口
60 cache_peer 172.25.254.205 parent 80 0 proxy-only ##80端口仅作代理

客户端的测试:输入代理端ip看是否能得到服务端内容

Linux-Squid代理服务器_第6张图片

你可能感兴趣的:(Linux-Squid代理服务器)