溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

squid代理服務(wù)器介紹與配置(理論+實(shí)踐)一

發(fā)布時(shí)間:2020-07-19 20:31:21 來源:網(wǎng)絡(luò) 閱讀:316 作者:SiceLc 欄目:系統(tǒng)運(yùn)維

緩存的概述

Web代理的工作機(jī)制

  • 緩存網(wǎng)頁對象,減少重復(fù)請求
    squid代理服務(wù)器介紹與配置(理論+實(shí)踐)一

    代理的基本類型

  • 傳統(tǒng)代理:適用于Internet,需明確指定服務(wù)端
  • 透明代理:客戶機(jī)不需要指定代理服務(wù) 器的地址和端口,而是通過默認(rèn)路由、防火墻策略將Web訪問重定向給代理服務(wù)器處理

使用代理的好處

  • 提高Web訪問速度
  • 隱藏客戶機(jī)的真實(shí)IP地址

傳統(tǒng)代理實(shí)驗(yàn)搭建

實(shí)驗(yàn)環(huán)境

squid服務(wù)器IP地址:192.168.80.179
web服務(wù)器IP地址:192.168.80.151
client測試機(jī)IP地址:192.168.80.135

在squid服務(wù)器上安裝squid服務(wù)

[root@squid ~]# mkdir /abc
[root@squid ~]# mount.cifs //192.168.80.2/LNMP-C7 /abc/      //掛載
[root@squid ~]# cd /abc/
[root@squid abc]# tar zxvf squid-3.4.6.tar.gz -C /opt         //解壓
[root@squid abc]# yum install gcc gcc-c++ make -y          //安裝環(huán)境組件
[root@squid abc]# cd /opt/squid-3.4.6
[root@squid squid-3.4.6]# ./configure \
--prefix=/usr/local/squid \                       //指定安裝路徑
--sysconfdir=/etc \                               //配置文件目錄
--enable-arp-acl \                                //支持acl訪問控制列表
--enable-linux-netfilter \                         //支持網(wǎng)絡(luò)篩選
--enable-linux-tproxy \                           //支持透明
--enable-async-io=100 \                          //IO優(yōu)化
--enable-err-language="Simplify_Chinese" \         //報(bào)錯(cuò)顯示簡體中文
--enable-underscore \
--enable-poll \
--enable-gnuregex                                 //支持正則表達(dá)
[root@squid squid-3.4.6]# make && make install    //編譯安裝
[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/     //復(fù)制命令便于系統(tǒng)識別
[root@squid squid-3.4.6]# useradd -M -s /sbin/nologin squid                 //創(chuàng)建系統(tǒng)用戶
[root@squid squid-3.4.6]# chown -R squid.squid /usr/local/squid/var/        //給目錄所有文件屬主屬組權(quán)限

修改squid服務(wù)配置文件,配置優(yōu)化啟動(dòng)方式

[root@squid squid-3.4.6]# vim /etc/squid.conf         //進(jìn)入修改squid配置文件
# And finally deny all other access to this proxy
http_access allow all              //添加此項(xiàng)
#http_access deny all              //注釋,允許終端訪問

# Squid normally listens to port 3128
http_port 3128
cache_effective_user squid              //指定用戶squid
cache_effective_group squid             //指定組
[root@squid squid-3.4.6]# squid -k parse           //檢查配置文件語法
[root@squid squid-3.4.6]# squid -z               //初始化緩存目錄
[root@squid squid-3.4.6]# squid                  //開啟服務(wù)
[root@squid squid-3.4.6]# netstat -ntap | grep 3128        //查看squid端口
[root@squid squid-3.4.6]# cd /etc/init.d/
[root@squid init.d]# vim squid                    //編輯service啟動(dòng)squid服務(wù)的腳本文件
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"              //PID文件進(jìn)程號文件路徑
CONF="/etc/squid.conf"                                  //主配置文件路徑
CMD="/usr/local/squid/sbin/squid"                       //服務(wù)啟動(dòng)命令路徑

case "$1" in
start)
                netstat -ntap | grep squid &> /dev/null
                if [ $? -eq 0 ]
                then 
                 echo "squid is running"
                 else
                 echo "正在啟動(dòng) squid...." 
                 $CMD
                fi
                ;;
stop)
                $CMD -k kill &> /dev/null               //關(guān)閉squid服務(wù)
                rm -rf $PID &> /dev/null                //刪除PID文件
                ;;
status)
                [ -f $PID ] &> /dev/null
                 if [ $? -eq 0 ]
                                then
                                 netstat -ntap | grep squid
                                else
                                 echo "squid is not running"
                fi
                ;;
restart)
                $0 stop &> /dev/null
                echo "正在關(guān)閉 squid..."
                $0 start &> /dev/null
                echo "正在啟動(dòng) 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                    //給執(zhí)行權(quán)限
[root@squid init.d]# chkconfig --add squid              //添加到service管理中
[root@squid init.d]# chkconfig --level 35 squid on        //設(shè)置開機(jī)自啟

設(shè)置傳統(tǒng)代理配置

[root@squid init.d]# vim /etc/squid.conf         //修改主配置文件
# Squid normally listens to port 3128
http_port 3128
cache_mem 64 MB                                //內(nèi)存空間大小
reply_body_max_size 10 MB                      //允許下載最大文件大小
maximum_object_size 4096 KB                    //允許保存緩存空間最大對象大小
[root@squid init.d]# service squid restart
[root@squid init.d]# iptables -L                //查看表內(nèi)容
[root@squid init.d]# iptables -F                //清空表緩存
[root@squid init.d]# setenforce 0
[root@squid init.d]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT        //允許3128端口
[root@squid init.d]# service squid reload             //重載配置文件

在web服務(wù)器上安裝apache服務(wù)

[root@web ~]# systemctl stop firewalld.service                //關(guān)閉防火墻
[root@web ~]# setenforce 0
[root@web ~]# yum install httpd -y                        //安裝apache服務(wù)
[root@web ~]# systemctl start httpd.service               //啟動(dòng)服務(wù)

使用client訪問web網(wǎng)頁

squid代理服務(wù)器介紹與配置(理論+實(shí)踐)一

在web服務(wù)器日志文件中查看訪問日志文件

[root@web ~]# cd /etc/httpd/logs/       //查看日志文件
[root@web logs]# vim access_log         //此時(shí)是135地址訪問的

修改客戶機(jī)瀏覽器代理設(shè)置

squid代理服務(wù)器介紹與配置(理論+實(shí)踐)一

再次回到web服務(wù)器日志文件中查看訪問日志文件

[root@web ~]# cd /etc/httpd/logs/             //查看日志文件
[root@web logs]# vim access_log               //此時(shí)是179代理服務(wù)器訪問的

透明代理實(shí)驗(yàn)搭建

實(shí)驗(yàn)環(huán)境

quid服務(wù)器IP地址:ens33:192.168.80.184
                 ens36:192.168.10.1 (僅主機(jī)模式)
web服務(wù)器IP地址: 192.168.80.151
clientIP地址:192.168.10.10  (僅主機(jī)模式)

在squid服務(wù)器上添加一塊網(wǎng)卡,并配置IP地址

squid代理服務(wù)器介紹與配置(理論+實(shí)踐)一

[root@squid ~]# cd /etc/sysconfig/network-scripts/
[root@squid network-scripts]# cp -p ifcfg-ens33 ifcfg-ens36
[root@squid network-scripts]# vim ifcfg-ens36              //修改ens36IP信息
刪除uuid修改33為36
BOOTPROTO=static                                 //設(shè)置靜態(tài)                     
IPADDR=192.168.10.1
NETMASK=255.255.255.0  
[root@squid network-scripts]# service network restart           //重啟網(wǎng)絡(luò)服務(wù)
[root@squid network-scripts]# vim /etc/sysctl.conf             //開啟路由轉(zhuǎn)發(fā)
net.ipv4.ip_forward=1
[root@squid network-scripts]# sysctl -p                         //加載

在web服務(wù)器上指定靜態(tài)路由

[root@web ~]# route add -net 192.168.10.0/24 gw 192.168.80.184  ##添加靜態(tài)路由

在squid服務(wù)器上設(shè)置透明代理

root@squid network-scripts]# vim /etc/squid.conf           //設(shè)置配置文件
http_port 192.168.10.1:3128 transparent                  //設(shè)置透明代理
cache_effective_user squid
cache_effective_group squid
[root@squid network-scripts]# service squid stop            //關(guān)閉開啟squid服務(wù)
[root@squid network-scripts]# service squid start
[root@squid network-scripts]# iptables -F                    //清空表緩存
[root@squid network-scripts]# iptables -t nat -F
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128                 //定義規(guī)則入口ens36,80端口重定向到3128
[root@squid network-scripts]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128            //https443端口
[root@squid network-scripts]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT      //允許3128端口訪問

client客戶端測試

squid代理服務(wù)器介紹與配置(理論+實(shí)踐)一

[root@web ~]# cd /var/log/httpd/
[root@web httpd]# vim access_log   ##查看訪問日志信息,此時(shí)訪問地址變?yōu)?84訪問web服務(wù)
向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI