Squid簡介:linux
Squid是一個高性能的代理緩存服務器,主要提供緩存加速和應用層過濾控制的功能。Squid支持FTP、gopher、HTTPS和HTTP協議。和通常的代理緩存軟件不一樣,Squid用一個單獨的、非模塊化的、I/O驅動的進程來處理全部的客戶端請求。
代理的工做機制:
當客戶機經過代理來請求web頁面時,指定的代理服務器會先檢查本身的緩存,若是緩存中已經有客戶機須要訪問的頁面,則直接將緩存中的頁面內容反饋給客戶機;若是緩存中沒有客戶機須要訪問的頁面,則由代理服務器向Internet發送訪問請求,當得到返回的Web頁面之後,將頁面數據保存到緩存中併發送給客戶機。c++
傳統代理:使用傳統代理的特色在於,客戶機的相關程序(如IE瀏覽器、qq聊天軟件等)必須指定代理服務器的地址、端口等基本信息。web
-正則表達式
透明代理:客戶機不須要指定代理服務器的地址和端口,而是經過默認路由、防火牆策略將Web訪問重定向給代理服務器處理。
vim
百度網盤連接:https://pan.baidu.com/s/198CbzyEFmh9MyVCdn1GKzA 密碼:9h6a瀏覽器
yum install gcc gcc-c++ make -y
tar zxvf squid-3.4.6.tar.gz -C /opt
cd /opt/squid-3.4.6/
./configure --prefix=/usr/local/squid \ --sysconfdir=/etc \ --enable-arp-acl \ --enable-linux-netfilter \ --enable-linux-tproxy \ --enable-async-io=100 \ --enable-err-language="Simplify_Chinese" \ --enable-underscore \ --enable-poll \ --enable-gnuregex
詳解以下:
./configure --prefix=/usr/local/squid \ //編譯安裝時目錄設置爲/usr/local/squid
--sysconfdir=/etc //單獨將配置文件修改到此目錄下
--enable-arp-acl //能夠在規則中設置直接經過客戶端MAC進行管理,防止客戶端使用IP欺騙
--enable-linux-netfilter //使用內核過濾
--enable-linux-tproxy //支持透明模式
--enable-async-io=100 //異步I/O,提高存儲性能
--enable-err-language="Simplify_Chinese" //錯誤信息的顯示語言
--enable-underscore //容許URL中有下劃線
--enable-poll //使用Poll() 模式,提高性能
--enable-gnuregex // 使用GUN正則表達式緩存
make && make install
ln -s /usr/local/squid/sbin/* /usr/local/sbin/ useradd -M -s /sbin/nologin squid chown -R squid.squid /usr/local/squid/var/
vim /etc/squid.conf
找到下面這行,進行添加:bash
#And finally deny all other access to this proxy http_access allow all #只添加這行,表明容許全部 http_access deny all
找到下面這行,進行添加: 服務器
#Squid normally listens to port 3128 http_port 3128 cache_effective_user squid #添加這行 表明指定程序用戶 cache_effective_group squid #添加這行 表明指定帳號基本組 #包含了程序運行時的內存,寄存器狀態,堆棧指針,也能夠認爲是當時程序的狀態時生成一個儲存文件 coredump_dir /usr/local/squid/var/cache/squid
vim /etc/init.d/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 -anpt | 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未啓動" fi ;; restart) $0 stop &> /dev/null echo "正在關閉 squid..." $0 start &> /dev/null echo "正在啓動 squid..." ;; reload) $CMD -k reconfigure ;; check) $CMD -k parse ;; *) echo "請輸入正確參數{start|stop|reload|restart|check|status}" ;; esac
編輯完腳本後保存退出,並賦予執行權限:
[root@localhost squid-3.4.6]# cd /etc/init.d/ [root@localhost init.d]# ls functions netconsole network README squid [root@localhost init.d]# chmod +x squid [root@localhost init.d]# chkconfig --add squid [root@localhost init.d]# chkconfig --level 35 squid on
service squid restart
squid的命令: [root@localhost ~]# squid -k parse #檢查配置文件語法,只要不出現error報錯,就沒有問題 [root@localhost ~]# squid -z #初始化緩存目錄 [root@localhost ~]# squid #啓動服務
1.一臺Centos 7 做爲Squid服務器,ip:192.168.233.128
2.一臺Centos 7 做爲提供web服務的http服務器,ip:192.168.233.6
3.一臺Windows 做爲客戶端 ,ip:192.168.233.200
vim /etc/squid.conf
找到下面的端口號,插入如下的內容
http_port 3128 cache_mem 64 MB #指定緩存功能所使用的內存空間大小,便於保持訪問頻繁的web對象,容量最好爲4的倍數,單位爲MB,建議設爲物理 內存的1/4 reply_body_max_size 10 MB #容許用戶下載的最大文件大小,以字節爲單位。默認設置爲0表示不進行設置。 maximum_object_size 4096 KB #容許保存到緩存空間的最大對象大小,以KB爲單位,超過大小限制的文件將不被緩存,而是直接轉發給用戶 修改完成後保存退出
[root@localhost ~]# iptables -F [root@localhost ~]# setenforce 0 [root@localhost ~]# iptables -I INPUT -p tcp --dport 3128 -j ACCEPT
[root@localhost ~]# service squid restart
關閉防火牆,搭建並開啓web服務:
[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 [root@localhost ~]# yum install httpd -y [root@localhost ~]# systemctl start httpd.service
在Windows 系統中開啓瀏覽器
Internet選項--》鏈接--》局域網設置—— ip:squid服務器地址 端口:3128
設置完成後,在地址欄輸入訪問web服務器的地址
1.一臺Centos 7 做爲Squid服務器,設置透明代理,
做爲網卡,設置兩個網關地址:
(1)內網 ens33:192.168.130.1
(2) 外網 ens36:12.0.0.1
2.一臺Centos 7 做爲內網端,ip:192.168.130.130【由於實驗,因此只使用一臺做爲內網端】
3.一臺Windows 做爲外網端,ip:12.0.0.12原理圖以下:
做爲網卡,設置兩個網關地址: (1)內網 ens33:192.168.130.1 (2) 外網 ens36:12.0.0.1
由於squid服務器是做爲網關的,因此須要開啓路由功能。
啓用路由功能:
進行修改配置主配置文件:[root@localhost ~]# vim /etc/squid.conf http_port 192.168.130.1:3128 transparent #修改 設置透明代理模式
修改完成後保存退出,並重啓squid服務:[root@localhost ~]# service squid restart設置防火牆的規則:
[root@localhost ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.130.0/24 -p tcp --dport 80 -j REDIRECT --to 3128 [root@localhost ~]# iptables -t nat -I PREROUTING -i ens33 -s 192.168.130.0/24 -p tcp --dport 443 -j REDIRECT --to 3128 [root@localhost ~]# iptables -I INPUT -p tcp --dport 3218 -j ACCEPT
設置web服務地址IP
關閉防火牆,搭建並開啓web服務:[root@localhost ~]# systemctl stop firewalld.service [root@localhost ~]# setenforce 0 [root@localhost ~]# yum install httpd -y [root@localhost ~]# systemctl start httpd.service
[root@localhost ~]# tail /var/log/httpd/access_log