squid-1正向代理安裝部署

squid簡介

Squid是一個高性能的代理緩存服務器,Squid支持FTP、gopher、HTTPS和HTTP協議,和一般的代理緩存軟件不同,Squid用一個單獨的、非模塊化的、I/O驅動的進程來處理所有的客戶端請求

web代理的工作機制

緩存網頁對象,減少重複請求

在這裏插入圖片描述
代理的基本類型

  • 傳統代理:適用於Internet,需明確指定服務器
  • 透明代理:客戶機不需要指定代理服務器的地址和端口,而是通過默認路由、防火牆策略將web訪問重定向給代理服務器處理

使用代理的好處

  • 提高web訪問速度
  • 隱藏客戶機的真實IP地址

搭建傳統代理服務器

1、環境部署
(1)環境拓撲
在這裏插入圖片描述
(2)IP地址規劃

主機名 IP地址
squid代理 192.168.7.128
Web 192.168.7.129
客戶機 192.168.7.155

2、squid安裝
(1)安裝環境包

[root@squid ~]# yum install gcc gcc-c++ -y

(2)編譯安裝squid安裝包

[root@squid squid]# tar zxvf squid-3.4.6.tar.gz -C /opt
[root@squid squid]# 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					//內核過濾
--enable-linux-tproxy						//支持透明模式
--enable-async-io=100						//io優化
--enable-err-language="Simplify_Chinese"	//設定支持語言
--enable-underscore							//支持下劃線
--enable-poll								//開啓poll功能
--enable-gnuregex							//支持正則表達式

[root@squid squid-3.4.6]# make && make install

(3)創建軟鏈接

[root@squid squid-3.4.6]# ln -s /usr/local/squid/sbin/* /usr/local/sbin/

(4)創建suid用戶,更改/usr/local/squid/var/的屬主、屬組

[root@squid ~]# useradd -M -s /sbin/nologin squid
[root@squid ~]# chown -R squid.squid /usr/local/squid/var/

(5)更改配置文件

[root@squid ~]# vim /etc/squid.conf
http_access allow all
#http_access deny all
#添加指定用戶
cache_effective_user squid		
#添加指定組
cache_effective_group squid		
#容災備份目錄(此項不需修改)
coredump_dir /usr/local/squid/var/cache/squid	

(6)檢查配置文件語法

[root@squid ~]# squid -k parse

(7)初始化緩存目錄

[root@squid ~]# squid -z

(8)啓動服務

[root@squid ~]# squid

(9)創建squid管理腳本

[root@squid ~]# cd /etc/init.d/
[root@squid init.d]# vim squid
#!/bin/bash
#chkconfig: 2345 90 25
PID="/usr/local/squid/var/run/squid.pid"
CMD="/usr/local/squid/sbin/squid"

case "$1" in
   start)
     netstat -natp | grep squid &> /dev/null
     if [ $? -eq 0 ]
     then
        echo "squid is running"
     else
        echo "正在啓動 squid..."
        $CMD
     fi
     ;;
   stop)
     $CMD -k kill &> /dev/null
     rm -rf $PID &> /dev/null
     ;;
   status)
     [ -f $PID ] &> /dev/null
        if [ $? -eq 0 ]
        then
          netstat -natp | grep squid
        else
          echo "squid is not running"
        fi
     ;;
   restart)
     $0 stop &> /dev/null
     echo "正在關閉 squid..."
     $0 start &> /dev/null
     echo "正在啓動 squid..."
     ;;
   check)
     $CMD -K parse
     ;;
   *)
     echo "用法: $0{start|stop|status|reload|check|resatrt}"
esac

[root@squid init.d]# chmod +x squid 
[root@squid init.d]# chkconfig --add squid
[root@squid init.d]# chkconfig --level 35 squid on

3、配置傳統代理服務器
(1)更改squid配置文件

[root@squid ~]# vim /etc/squid.conf
#插入以下內容
#指定緩存功能所使用的的內存空間大小
cache_mem 64 MB			
#允許用戶下載的最大文件大小
reply_body_max_size 10 MB	
#允許保存到緩存空間的最大文件大小
maximum_object_size 4096 KB	

#重啓服務
[root@squid ~]# service squid restart

(2)設置squid防火牆規則

[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

4、web服務器配置

  • 安裝Apache服務器
[root@web ~]# yum install httpd -y
[root@web ~]# systemctl start httpd

5、客戶端測試
(1)更改瀏覽器的代理服務器
在這裏插入圖片描述
(2)使用瀏覽器訪問web網頁
在這裏插入圖片描述
(3)在web服務器上打開日誌文件,查看訪問網頁的IP地址

[root@web ~]# tail -f /var/log/httpd/access_log 
192.168.7.128 - - [18/Mar/2020:11:14:33 +0800] "GET / HTTP/1.1" 200 26 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
192.168.7.128 - - [18/Mar/2020:11:14:33 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://192.168.7.129/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"

透明模式代理配置

1、IP地址規劃,在squid服務器上配置雙網卡,內置網卡ens33,外置網卡ens36

主機名 IP地址
squid代理 ens33:192.168.7.128,ens36:192.168.10.1
Web 192.168.7.129
客戶機 192.168.10.10

2、在squid上添加路由轉發功能

[root@squid ~]# vim /etc/sysctl.conf
#在最後一行添加
net.ipv4.ip_forward=1

[root@squid ~]# sysctl -p
net.ipv4.ip_forward=1

3、更改squid主配置文件,設置防火牆規則

[root@squid ~]# vim /etc/squid.conf
http_port 192.168.10.1:3128 transparent

#重啓服務
[root@squid ~]# service squid restart
[root@squid ~]# iptables -F
[root@squid ~]# iptables -t nat -F
[root@squid ~]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 80 -j REDIRECT --to 3128
[root@squid ~]# iptables -t nat -I PREROUTING -i ens36 -s 192.168.10.0/24 -p tcp --dport 443 -j REDIRECT --to 3128
root@squid ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT

4、web服務器配置
(1)安裝Apache服務器

[root@web ~]# yum install httpd -y
[root@web ~]# systemctl start httpd

(2)設置默認路由

[root@web ~]# route add -net 192.168.10.0/24 gw 192.168.7.128

5、客戶端測試
(1)不需要設置代理服務器,直接使用瀏覽器訪問web網頁
在這裏插入圖片描述
(2)在web服務器上打開日誌文件,查看訪問的IP地址

[root@web ~]# tail -f /var/log/httpd/access_log 
192.168.7.128 - - [18/Mar/2020:11:14:33 +0800] "GET / HTTP/1.1" 200 26 "-" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
192.168.7.128 - - [18/Mar/2020:11:14:33 +0800] "GET /favicon.ico HTTP/1.1" 404 209 "http://192.168.7.129/" "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36"
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章