使用Squid部署代理緩存服務(CentOS 7.4)

使用Squid部署代理緩存服務

正向代理不只可讓用戶使用squid代理服務器上網,還能夠基於IP地址、網站關鍵字、下載文件後綴等實現相似行爲管控的功能。linux

反向代理能夠大幅提高網站的訪問速度,減輕網站服務器的負載壓力。vim

使用Squid部署代理緩存服務(CentOS 7.4)

  • 虛擬機配置

使用Squid部署代理緩存服務(CentOS 7.4)

  • 服務端IP
[root@mail ~]# ifconfig
ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.16.10  netmask 255.255.255.0  broadcast 192.168.16.255
        inet6 fe80::6bea:7e4d:fd17:8225  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:08:3e:47  txqueuelen 1000  (Ethernet)
        RX packets 6  bytes 726 (726.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 266  bytes 25392 (24.7 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens37: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.10.157  netmask 255.255.255.0  broadcast 192.168.10.255
        inet6 fe80::119b:77df:e22:7194  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:08:3e:51  txqueuelen 1000  (Ethernet)
        RX packets 38296  bytes 2498587 (2.3 MiB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 1261  bytes 125135 (122.2 KiB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • 服務端安裝squid
[root@mail ~]# yum install -y squid
[root@mail ~]# systemctl restart squid
[root@mail ~]# systemctl enable squid
Created symlink from /etc/systemd/system/multi-user.target.wants/squid.service to /usr/lib/systemd/system/squid.service.
  • 標準正向代理——客戶端設置,IE選項設置
C:\Users\Administrator>ping www.baidu.com
Ping 請求找不到主機 www.baidu.com。請檢查該名稱,而後重試。  #外網不通

使用Squid部署代理緩存服務(CentOS 7.4)
使用Squid部署代理緩存服務(CentOS 7.4)

3128端口號是squid標準正向解析的默認監聽端口,設置完成以後依然是沒法ping通外網的,可是能夠打開網頁了緩存

使用Squid部署代理緩存服務(CentOS 7.4)

  • 服務端修改squid配置文件
[root@mail ~]# vim /etc/squid/squid.conf
http_port 10000  #大約第59行,將默認的3128端口修改成10000
[root@mail ~]# systemctl restart squid
  • 配置selinux
[root@mail ~]# semanage port -l | grep squid
squid_port_t                   tcp      3128, 3401, 4827
squid_port_t                   udp      3401, 4827
[root@mail ~]# semanage port -a -t squid_port_t -p tcp 10000
[root@mail ~]# systemctl restart squid
  • 客戶端修改默認端口號

使用Squid部署代理緩存服務(CentOS 7.4)

修改IE選項中的端口號爲10000 服務器

使用Squid部署代理緩存服務(CentOS 7.4)

從新打開網頁,成功網絡

使用Squid部署代理緩存服務(CentOS 7.4)

  • 透明正向代理——客戶端還原IE選項
[root@mail ~]# iptables -t nat -A POSTROUTING -p udp --dport 53 -o ens37 -j MASQUERADE  #DNS流量轉發,端口默認53
[root@mail ~]# vim /etc/sysctl.conf
net.ipv4.ip_forward=1  #新增此行,容許Squid服務器轉發IPv4數據包
[root@mail ~]# sysctl -p  #讓轉發參數當即生效
net.ipv4.ip_forward = 1

客戶端網關和DNS設置tcp

網關爲192.168.16.10
DNS位114.114.114.114
此時ping www.baidu.com進行測試,仍沒法ping通,可是能夠返回其公網IP了ide

C:\Users\Administrator>ping www.baidu.com

正在 Ping www.a.shifen.com [111.13.100.92] 具備 32 字節的數據:
請求超時。
請求超時。
請求超時。
請求超時。

111.13.100.92 的 Ping 統計信息:
    數據包: 已發送 = 4,已接收 = 0,丟失 = 4 (100% 丟失),
  • 服務端進行修改squid配置文件
[root@mail ~]# vim /etc/squid/squid.conf
http_port 3128 transparent  #大約第59行,設置爲透明轉發
cache_dir ufs /var/spool/squid 100 16 256  #大約第62行,去掉前面的#設置緩存的保存路徑
[root@mail ~]# systemctl stop squid  #先中止squid服務
[root@mail ~]# squid -k parse  #檢查主配置文件是否有錯誤
2018/08/03 21:49:23| Startup: Initializing Authentication Schemes ...
2018/08/03 21:49:23| Startup: Initialized Authentication Scheme 'basic'
2018/08/03 21:49:23| Startup: Initialized Authentication Scheme 'digest'
2018/08/03 21:49:23| Startup: Initialized Authentication Scheme 'negotiate'
2018/08/03 21:49:23| Startup: Initialized Authentication Scheme 'ntlm'
2018/08/03 21:49:23| Startup: Initialized Authentication.
2018/08/03 21:49:23| Processing Configuration File: /etc/squid/squid.conf (depth 0)
2018/08/03 21:49:23| Processing: acl localnet src 10.0.0.0/8    # RFC1918 possible internal network
2018/08/03 21:49:23| Processing: acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
2018/08/03 21:49:23| Processing: acl localnet src 192.168.0.0/16        # RFC1918 possible internal network
2018/08/03 21:49:23| Processing: acl localnet src fc00::/7       # RFC 4193 local private network range
2018/08/03 21:49:23| Processing: acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machines
2018/08/03 21:49:23| Processing: acl SSL_ports port 443
2018/08/03 21:49:23| Processing: acl Safe_ports port 80         # http
2018/08/03 21:49:23| Processing: acl Safe_ports port 21         # ftp
2018/08/03 21:49:23| Processing: acl Safe_ports port 443                # https
2018/08/03 21:49:23| Processing: acl Safe_ports port 70         # gopher
2018/08/03 21:49:23| Processing: acl Safe_ports port 210                # wais
2018/08/03 21:49:23| Processing: acl Safe_ports port 1025-65535 # unregistered ports
2018/08/03 21:49:23| Processing: acl Safe_ports port 280                # http-mgmt
2018/08/03 21:49:23| Processing: acl Safe_ports port 488                # gss-http
2018/08/03 21:49:23| Processing: acl Safe_ports port 591                # filemaker
2018/08/03 21:49:23| Processing: acl Safe_ports port 777                # multiling http
2018/08/03 21:49:23| Processing: acl CONNECT method CONNECT
2018/08/03 21:49:23| Processing: http_access deny !Safe_ports
2018/08/03 21:49:23| Processing: http_access deny CONNECT !SSL_ports
2018/08/03 21:49:23| Processing: http_access allow localhost manager
2018/08/03 21:49:23| Processing: http_access deny manager
2018/08/03 21:49:23| Processing: http_access allow localnet
2018/08/03 21:49:23| Processing: http_access allow localhost
2018/08/03 21:49:23| Processing: http_access deny all
2018/08/03 21:49:23| Processing: http_port 3128 transparent
2018/08/03 21:49:23| Starting Authentication on port [::]:3128
2018/08/03 21:49:23| Disabling Authentication on port [::]:3128 (interception enabled)
2018/08/03 21:49:23| Processing: cache_dir ufs /var/spool/squid 100 16 256
2018/08/03 21:49:23| Processing: coredump_dir /var/spool/squid
2018/08/03 21:49:23| Processing: refresh_pattern ^ftp:          1440    20%     10080
2018/08/03 21:49:23| Processing: refresh_pattern ^gopher:       1440    0%      1440
2018/08/03 21:49:23| Processing: refresh_pattern -i (/cgi-bin/|\?) 0    0%      0
2018/08/03 21:49:23| Processing: refresh_pattern .              0       20%     4320
2018/08/03 21:49:23| Initializing https proxy context
[root@mail ~]# squid -z  #對Squid服務程序的透明代理技術進行初始化,生成緩存目錄
[root@mail ~]# 2018/08/03 21:50:01 kid1| Set Current Directory to /var/spool/squid
2018/08/03 21:50:01 kid1| Creating missing swap directories
2018/08/03 21:50:01 kid1| /var/spool/squid exists
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/00
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/01
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/02
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/03
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/04
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/05
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/06
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/07
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/08
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/09
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/0A
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/0B
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/0C
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/0D
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/0E
2018/08/03 21:50:01 kid1| Making directories in /var/spool/squid/0F
[root@mail ~]# iptables -t nat -A PREROUTING  -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 3128  #把全部客戶端主機對網站80端口的請求轉發至Squid服務器本地的3128端口上
[root@mail ~]# iptables -t nat -A POSTROUTING -s 192.168.16.0/24 -o ens33 -j SNAT --to 192.168.10.157  #SNAT數據轉發。16.0網段爲僅主機模式網段,ens33位僅主機網絡端口,10.157爲橋接網絡端口
[root@mail ~]# systemctl restart squid

此時客戶端能夠直接ping通了工具

C:\Users\Administrator>ping www.wuxier.cn

正在 Ping www.wuxier.cn [182.61.34.191] 具備 32 字節的數據:
來自 182.61.34.191 的回覆: 字節=32 時間=34ms TTL=50
來自 182.61.34.191 的回覆: 字節=32 時間=34ms TTL=50
來自 182.61.34.191 的回覆: 字節=32 時間=35ms TTL=50
來自 182.61.34.191 的回覆: 字節=32 時間=35ms TTL=50

182.61.34.191 的 Ping 統計信息:
    數據包: 已發送 = 4,已接收 = 4,丟失 = 0 (0% 丟失),
往返行程的估計時間(以毫秒爲單位):
    最短 = 34ms,最長 = 35ms,平均 = 34ms
  • 驗證確實是經過代理轉發,而不是經過服務器來直接上網

①基於客戶端的IP地址
服務端配置squid配置文件測試

[root@mail ~]# vim /etc/squid/squid.conf
acl client src 192.168.16.21  #新增此行,容許來源是192.168.16.21的客戶端上網
http_access allow client  #大約第31行,新增此行,容許剛纔設置的客戶端192.168.16.21上網
http_access deny all  #大約第32行,新增此行,拒絕其餘客戶端上網
[root@mail ~]# systemctl restart squid  #配置完成後重啓服務

從客戶端(IP爲192.168.16.20)從新打開剛纔的網頁,提示以下圖網站

使用Squid部署代理緩存服務(CentOS 7.4)

將IP修改成192.168.16.21後,網頁恢復正常訪問,以下圖

使用Squid部署代理緩存服務(CentOS 7.4)

說明咱們的網絡服務是受到了代理服務的限制

②基於網址關鍵詞
修改squid配置文件

[root@mail ~]# vim /etc/squid/squid.conf
acl deny_keyword url_regex -i wuxi  #新增此行,acl後的至關因而別名,能夠自定義。url_regex表明匹配網址,接參數-i表明拒絕網址中包含此關鍵詞的。或者不用參數-i,直接接特定的網址全名,如http://www.wuxier.cn
 http_access deny deny_keyword  #新增此行
[root@mail ~]# systemctl restart squid  #配置完成後重啓服務

當打開www.wuxier.cn的時候沒法訪問,由於包含了wuxi;而打開www.51cto.com等不包含wuxi的網址時均可以正常訪問。

注意:若是網址時https的是通過加密的則是不受限制的

使用Squid部署代理緩存服務(CentOS 7.4)

③禁止下載帶有某些後綴的文件
修改squid配置文件

[root@mail ~]# vim /etc/squid/squid.conf
acl badfile url_regex -i \.mp3$ \.rar$  #新增此行,\表明轉義,$表明以某後綴結尾
http_access deny badfile  #新增此行
[root@mail ~]# systemctl restart squid  #配置完成後重啓服務

當下載.rar壓縮包的時候沒法下載,以下圖(若是是使用迅雷等工具會沒法進行限制)

使用Squid部署代理緩存服務(CentOS 7.4)

  • 反向代理

首先找到一個網站源服務器的IP地址,經過ping網站域名獲得其公網IP,若是經過公網IP能打開其網站,則符合咱們的條件。而後編輯Squid服務程序的主配置文件,把端口號3128修改成網站源服務器的地址和端口號,此時正向解析服務會被暫停(它不能與反向代理服務同時使用)。

修改squid配置文件

[root@mail ~]# vim /etc/squid/squid.conf
http_port 192.168.10.157:80 vhost  #大約第58行,將原來的http_port 3128修改成此配置。192.168.10.157爲本機IP。
cache_peer 58.250.26.38 parent 80 0 originserver  #新增此行。緩存節點的IP地址,58.250.26.38即獲取的公網IP,定義爲咱們的一個父節點,端口號爲80,它是一臺原始的服務器
[root@mail ~]# systemctl restart squid  #配置完成後重啓服務

當咱們訪問192.168.10.157的時候就能直接訪問剛纔公網能夠訪問的網站了,以下圖

使用Squid部署代理緩存服務(CentOS 7.4)

相關文章
相關標籤/搜索