热门!!Squid缓存加速——传统及透明模式服务搭建
- 一、緩存代理概述
- 1. Web代理的工作機制
- 2.代理的基本類型
- 3.使用代理的好處
- 二、squid傳統模式
- 【squid服務器】
- 1.設置主機名
- 2.編譯安裝squid
- 3.優化路徑
- 4.創建squid程序用戶,并改變目錄下文件屬性
- 5.修改squid配置
- 6.開啟服務
- 7.設置系統服務項
- 8.傳統代理服務器需要配置的選項
- 9.放通防火墻規則
- 【Web服務器】
- 1.安裝Apache服務
- 【客戶端瀏覽器訪問】訪問驗證
- 1.直接訪問Apache http://192.168.10.20
- 2.指定代理服務器后再次訪問Apache
- 3.查看web服務器的訪問日志
- 三、squid透明模式
- Squid透明模式掛載原理
- 1.實驗拓撲
- 2.配置雙網卡
- 3.設置路由轉發
- 4.修改squid.conf配置
- 5.配置iptables轉發規則
- 6.在Web服務器端配置靜態回程路由
- 7.客戶端(20.0.0.30)訪問web服務器(192.168.10.20)測試
- 8.查看Apache訪問日志
一、緩存代理概述
官方地址:http://www.squid-cache.org/
軟件下載地址:http://www.squid-cache.org/Versions/
1. Web代理的工作機制
- 緩存網頁對象,減少重復請求
2.代理的基本類型
● 傳統代理:適用于Internet,需明確指定服務端
● 透明代理:客戶機不需指定代理服務器的地址和端口,而是通過默認路由、防火墻策略將Web訪問重定向給代理服務器處理
3.使用代理的好處
- 提高Web訪問速度
- 隱藏客戶機的真實IP地址
二、squid傳統模式
實驗設計:一臺squid代理服務器,一臺Web服務器,一臺Client端
實驗拓撲:
【squid服務器】
1.設置主機名
[root@localhost~]# hostnamectl set-hostname squid [root@localhost~]# su2.編譯安裝squid
[root@squid~]# yum install gcc gcc-c++ -y [root@squid~]# tar zxvf squid-3.4.6.tar.gz -C /opt [root@squid~]# cd /opt/squid-3.4.6/ [root@squidsquid-3.4.6]# ./configure \ --prefix=/usr/local/squid \ ##安裝路徑 --sysconfdir=/etc \ ##配置文件目錄 --enable-arp-acl \ ##支持acl訪問控制列表 --enable-linux-netfilter \ ##支持網絡篩選 --enable-linux-tproxy \ ##支持透明 --enable-async-io=100 \ ## I/O優化 --enable-err-language="Simplify_Chinese" \ ##報錯顯示簡體中文 --enable-underscore \ ##支持下劃線 --enable-poll \ ##關閉默認使用poll模式,開啟epoll模式提提升性能 --enable-gnuregex ##支持正則表達 [root@squid squid-3.4.6]# make -j3 && make install3.優化路徑
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/ ##創建命令軟連接,方便系統識別'4.創建squid程序用戶,并改變目錄下文件屬性
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid ##創建系統用戶 [root@squid squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/ ##設置目錄的屬主和屬組5.修改squid配置
[root@squid squid-3.4.6]# vim /etc/squid.conf http_access allow all ##添加此行允許所有訪問(565行左右) #http_access deny all ##將拒絕所有注釋掉(不注釋也一樣,上面允許所有,這個就失去了意義) http_port 3128 ##默認是3128端口(可以不用改) cache_effective_user squid ##添加指定用戶squid (可以在60行下插入這兩行) cache_effective_group squid ##添加指定組 squid …… [root@squid squid-3.4.6]# squid -k parse ##檢查配置文件中的語法問題 [root@squid squid-3.4.6]# squid -z ##初始化緩存(需要等一會兒) ############################################################################################ squid -z 初始化錯誤,提示沒有緩存目錄 [root@squid squid-3.4.6]# squid -z [root@squid squid-3.4.6]# 2020/10/30 19:12:28 kid1| Set Current Directory to /usr/local/squid/var/cache/squid 2020/10/30 19:12:28 kid1| Creating missing swap directories 2020/10/30 19:12:28 kid1| No cache_dir stores are configured. 解決辦法 去掉”cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256 ”前面的注釋,大概62行左右,否則無法squid -z 初始化 [root@squid squid-3.4.6]# vim /etc/squid.conf cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256 ##去掉注釋 #############################################################################################6.開啟服務
[root@squid squid]# squid [root@squid squid]# netstat -anupt |grep 3128 ##查看監聽端口 tcp6 0 0 :::3128 :::* LISTEN 68246/(squid-1)7.設置系統服務項
[root@squid squid]# cd /etc/init.d/ [root@squid init.d]# vi squid #!/bin/bash #chkconfig: 2345 90 25 PID="/usr/local/squid/var/run/squid.pid" CONF="/etc/squid.conf" CMD="/usr/local/squid/sbin/squid"case "$1" in start)netstat -ntap | grep squid &> /dev/nullif [ $? -eq 0 ]thenecho "squid is running"elseecho "正在啟動 squid...."$CMDfi;; stop)$CMD -k kill &> /dev/nullrm -rf $PID &> /dev/null;; status)[ -f $PID ] &> /dev/nullif [ $? -eq 0 ]thennetstat -ntap | grep squidelseecho "squid is not running"fi;; restart)$0 stop &> /dev/nullecho "正在關閉 squid..."$0 start &> /dev/nullecho "正在啟動 squid...";; reload)$CMD -k reconfigure;; check)$CMD -k parse;; *)echo "用法:$0{start|stop|reload|status|check|restart}";; esac [root@squid init.d]# chmod +x squid [root@squid init.d]# chkconfig --add squid ##加入到service管理 [root@squid init.d]# chkconfig --list squid Note: This output shows SysV services only and does not include nativesystemd services. SysV configuration data might be overridden by native……省略部分 squid 0:off 1:off 2:on 3:on 4:on 5:on 6:off[root@squid init.d]# chkconfig --level 35 squid on ##35級別自啟 [root@squid init.d]# chkconfig --list……省略部分 netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off network 0:off 1:off 2:on 3:on 4:on 5:on 6:off squid 0:off 1:off 2:on 3:on 4:on 5:on 6:off ##加入到啟動項 [root@squid init.d]# service squid stop ##關閉squid [root@squid init.d]# netstat -anupt |grep 3128 ##監聽端口已關閉 [root@squid init.d]# service squid start ##啟動squid 正在啟動 squid.... [root@squid init.d]# netstat -anupt |grep 3128 tcp6 0 0 :::3128 :::* LISTEN 68446/(squid-1)8.傳統代理服務器需要配置的選項
[root@squid init.d]# vim /etc/squid.conf ## 插入以下幾行 cache_mem 64 MB ##指定緩存使用的空間大小,容量最好為4的倍數 reply_body_max_size 10 MB ##允許用戶下載的最大文件大小,以字節為單位,默認設置為0表示不限制 maximum_object_size 4096 KB ##允許保存到緩存空間的最大對象大小,以KB為單位,超過限制不會緩存,直接轉到web端 [root@squid init.d]# service squid reload ##重新加載服務9.放通防火墻規則
[root@squid init.d]# iptables -F [root@squid init.d]# setenforce 0 setenforce: SELinux is disabled [root@squid init.d]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT [root@squid init.d]# iptables -L --line-numbers Chain INPUT (policy ACCEPT) num target prot opt source destination 1 ACCEPT tcp -- anywhere anywhere tcp dpt:smartpacketsChain FORWARD (policy ACCEPT) num target prot opt source destination Chain OUTPUT (policy ACCEPT) num target prot opt source destination【Web服務器】
1.安裝Apache服務
[root@localhost ~]# yum -y install httpd
[root@localhost ~]# systemctl start httpd
【客戶端瀏覽器訪問】訪問驗證
1.直接訪問Apache http://192.168.10.20
[root@localhost ~]# cd /var/log/httpd/
[root@localhost httpd]# ls
access_log error_log
[root@localhost httpd]# cat access_log ##查看訪問日志,日志顯示來自192.168.10.1的請求
192.168.10.1 - - [30/Oct/2020:19:26:00 +0800] “GET / HTTP/1.1” 403 4897 “-” “Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko”
192.168.10.1 - - [30/Oct/2020:19:26:00 +0800] “GET /noindex/css/bootstrap.min.css HTTP/1.1” 200 19341 “http://192.168.10.20/” “Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko”
……省略部分
2.指定代理服務器后再次訪問Apache
3.查看web服務器的訪問日志
請求是來自squid的
[root@localhost ~]# cd /var/log/httpd/ [root@localhost httpd]# ls access_log error_log [root@localhost httpd]# cat access_log ##查看訪問日志,日志顯示來自代理服務器192.168.10.10的請求 ……省略部分 192.168.10.10 - - [30/Oct/2020:19:39:58 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36" 192.168.10.10 - - [30/Oct/2020:19:39:58 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36" 192.168.10.10 - - [30/Oct/2020:19:39:58 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://192.168.10.20/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.111 Safari/537.36"三、squid透明模式
Squid透明模式掛載原理
1.實驗拓撲
基于上面傳統模式上進行配置修改
2.配置雙網卡
[root@squid ~]# nmcli connection ##查詢新增網卡的UUID NAME UUID TYPE DEVICE ens33 decf0e5b-f858-4432-9518-eda561739439 802-3-ethernet ens33 virbr0 fcc9e96f-22f7-48a9-8e47-409a6961bef3 bridge virbr0 Wired connection 1 282144f0-c96e-309a-9557-b735e4f84518 802-3-ethernet -- [root@squid ~]# cd /etc/sysconfig/network-scripts/ [root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36 [root@squid network-scripts]# vi ifcfg-ens36 NAME=ens36 ##修改網卡名稱 UUID=282144f0-c96e-309a-9557-b735e4f84518 ##修改UUID DEVICE=ens36 ##修改設備名稱 IPADDR=20.0.0.10 ##修改ip GATEWAY=20.0.0.2 ##修改網關 [root@squid network-scripts]# ifup ens36 ##開啟網卡 [root@squid network-scripts]# ifconfig ens33: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 192.168.10.10 netmask 255.255.255.0 broadcast 192.168.10.255……省略部分 ens36: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500inet 20.0.0.2 netmask 255.255.255.0 broadcast 20.0.0.255 ……省略部分3.設置路由轉發
[root@squid ~]# vi /etc/sysctl.conf net.ipv4.ip_forward = 1 [root@squid ~]# sysctl -p ##生效內核參數修改 net.ipv4.ip_forward = 14.修改squid.conf配置
[root@squid ~]# vim /etc/squid.conf http_port 192.168.10.1:3128 transparent ##對http_port 3128字段進行修改,改成透明模式 [root@squid ~]# service squid stop [root@squid ~]# service squid start ##重啟squid5.配置iptables轉發規則
[root@squid ~]# iptables -t nat -F [root@squid ~]# iptables -F [root@squid ~]# iptables -t nat -I PREROUTING -i ens33 -s 20.0.0.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 ##原地址是20.0.0.0網段80端口轉換成3128端口發送出去 [root@squid ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 [root@squid network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT ##允許入口方向訪問3128端口6.在Web服務器端配置靜態回程路由
[root@squid ~]# route add -net 20.0.0.0/24 gw 192.168.10.10 ##添加路由,來自20.0.0.0網段的數據往192.168.10.10接口(Squid外網口)發送 [root@squid ~]# route -n ##路由條目添加成功 Destination Gateway Genmask Flags Metric Ref Use Iface 0.0.0.0 192.168.10.2 0.0.0.0 UG 100 0 0 ens33 20.0.0.0 192.168.10.10 255.255.255.0 UG 0 0 0 ens33 192.168.10.0 0.0.0.0 255.255.255.0 U 100 0 0 ens33 192.168.122.0 0.0.0.0 255.255.255.0 U 0 0 0 virbr07.客戶端(20.0.0.30)訪問web服務器(192.168.10.20)測試
直接訪問,不需要在瀏覽器上配置代理
8.查看Apache訪問日志
請求是來自squid代理服務器的
[root@localhost ~]# tail -f /var/log/httpd/access_log ##請求是來自squid服務器的地址 192.168.10.10 - - [30/Oct/2020:23:37:16 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.woff HTTP/1.1" 404 239 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" 192.168.10.10 - - [30/Oct/2020:23:37:16 +0800] "GET /noindex/css/fonts/Light/OpenSans-Light.ttf HTTP/1.1" 404 240 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" 192.168.10.10 - - [30/Oct/2020:23:37:16 +0800] "GET /noindex/css/fonts/Bold/OpenSans-Bold.ttf HTTP/1.1" 404 238 "http://192.168.10.20/noindex/css/open-sans.css" "Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0" 192.168.10.1 - - [30/Oct/2020:23:37:40 +0800] "-" 408 - "-" "-"總結
以上是生活随笔為你收集整理的热门!!Squid缓存加速——传统及透明模式服务搭建的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 在虚拟机里安装windows
- 下一篇: 用c语言计算高考成绩,C语言计算距离高考