Linux下Squid正向/反向代理配置

jopen 9年前發布 | 79K 次閱讀 Squid 緩存服務器

應用場景: 1、正向代理 Squid被數以百計的網絡提供商用來為他們的用戶提供最好的網頁訪問,為了提升性能和常用內容來減少帶寬squid充分利用客戶端和服務端的數據流。Squid還能路由內容請求到服務器,以各種方式構建緩存服務器多層次結構,優化網絡吞吐量 2、反向代理(網站內容加速和分發)Squid是一個支持HTTP, HTTPS, FTP的web緩存代理,它能通過緩存和反復使用頻繁訪問的網頁來減少帶寬、提高響應速度。Squid被廣泛用于訪問控制和做前端加速,它能運行在大部分 操作系統平臺上,Squid能減少服務器的壓力,提高客戶端的響應速度,其高級的內容路由配置允許建立集群和負載均衡來處理各種WEB請求,3.2及之后 的版本基本都已支持SMP和多CPU

應用場景:
1、正向代理
Squid被數以百計的網絡提供商用來為他們的用戶提供最好的網頁訪問,為了提升性能和常用內容來減少帶寬squid充分利用客戶端和服務端的數據流。Squid還能路由內容請求到服務器,以各種方式構建緩存服務器多層次結構,優化網絡吞吐量
2、反向代理(網站內容加速和分發)


一、正向代理

環境:
Squid
eth0: 10.0.2.15
eth1: 192.168.1.10

Client
eth0: 192.168.1.100


1、Squid 安裝
A、yum安裝

yum install squid -y

B、編譯安裝

groupadd squid
useradd -g squid -s /sbin/nologin squid

cd ~/downloads/
wget http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.4.tar.xz
tar -Jxf squid-3.5.4.tar.xz
cd squid-3.5.4

./configure \
--prefix=/usr/local/squid/ \
--localstatedir=/var \ #緩存目錄
--mandir=/usr/share/man \
--enable-async-io=200 \ #async模式來運行squid,線程數
--enable-icmp \
--enable-delay-pools \ #延時池,這樣能對某些特定的請求限制額定帶寬
--enable-kill-parent-hack \ #關掉suqid的時候,連同父進程一起關掉
--enable-epoll \
--enable-snmp \ #可以讓MRTG使用SNMP協議對服務器的流量狀態進行監測
--enable-cache-digests \ #啟用緩存摘要支持
--with-large-files \
--disable-arp-acl \ #禁用對客戶端的MAC地址進行管理
--disable-ident-lookups

./configure --prefix=/usr/local/squid/ --localstatedir=/var --mandir=/usr/share/man --enable-async-io=200 --enable-icmp --enable-delay-pools --enable-kill-parent-hack --enable-epoll --enable-snmp --enable-cache-digests --with-large-files --disable-arp-acl --disable-ident-lookups

make && make install


vim /etc /init.d/squid #Squid 啟動腳本

</tr> </tbody> </table>
chmod a+x /etc/init.d/squid
chkconfig --add squid #加入系統服務
chkconfig squid on

滾動日志(日志切割):
vim /etc/cron.d/squid_cutlog

#!/bin/bash
# Squid server script
# chkconfig: 2345 86 17
# description: Squid prxoy server

squid_prefix='/usr/local/squid'
squid_pid='/var/run/squid.pid'

start()  {
if [ -e $squid_pid ]; then 
echo "squid already start, nothing "
else 
$squid_prefix/sbin/squid -k parse >> /dev/null 2>&1         #check config_file 
if [ $? -ne 0 ]; then 
    echo "squid config_file is Error "
else 
    echo "Squid is starting..."
    $squid_prefix/sbin/squid -s   
    fi 
    fi
}


stop()  {
if [ ! -e $squid_pid ]; then
echo "squid already start stop, nothing "
else 
    echo "Squid is stoping..."
$squid_prefix/sbin/squid -k shutdown 
    fi
}

status() {
if [ -e $squid_pid ]; then 
echo "squid  (pid  `cat $squid_pid`) is running..."
else 
    echo "Squid is stop"
    fi
}


case $1 in 
start)
  start
              ;;
stop)
          stop 
  ;;
restart)      
          stop 
  sleep 40  #shutdown_lifetime is 30(default)
  start
  ;;
status)
          status
              ;;    
reload)
          echo "Squid is reloading"
  $squid_prefix/sbin/squid -k reconfigure
  ;;
*)
          echo "Usage: $0 {start|stop|restart|status|reload}"
  ;;
esac

</tr> </tbody> </table>



2、配置Squid

vim /etc/squid/squid.conf #編輯squid主配置文件

0 3 * * *  /usr/local/squid/sbin/squid -k rotate           #每天3點切割一次

</tr> </tbody> </table>

chown -R squid:squid /var/cache/squid #緩沖目標權限
/usr/local/squid/sbin/squid -z #創建緩沖區目錄

啟動squid
/etc/init.d/squid restart #適用yum安裝
/usr/local/squid/sbin/squid -f /usr/local/squid/etc/squid.conf


/usr/local/squid/bin/squidclient -p 3128 mgr:info #運行狀態信息
/usr/local/squid/bin/squidclient -p 3128 mgr:mem #內存情況
/usr/local/squid/bin/squidclient -p 3128 mgr:diskd #磁盤情況
/usr/local/squid/bin/squidclient -p 3128 mgr:objects
/usr/local/squid/bin/squidclient -p 3128 mgr: #查看更多。。
/usr/local/squid/bin/squidclient -p 3128 PURGE http://www.php-oa.com/static.php #強制更新某個url

/usr/local/squid/sbin/squid -krec #重新載入配置
/usr/local/squid/sbin/squid -k reconfigure|rotate|shutdown|restart|interrupt|kill|debug|check|parse(檢查語法) #用法
/usr/local/squid/sbin/squid -N -d1 # debug


3、客戶端測試

1、非上網時間訪問:
curl --proxy 192.168.1.10:3128 www.cooking.com

<p>Access control configuration prevents your request from being allowed at this time. Please contact your service provider if you feel this is incorrect.</p>


2、訪問屏蔽URL關鍵字
curl --proxy 192.168.1.10:3128 www.baidu.com

<h2>The requested URL could not be retrieved</h2>



二、透明代理(特殊的一種正向代理)

vim etc/squid.conf #配置上同,加入"intercept“

#1、規則會依次執行,先執行的先優先生效
#2、先局部后整體,一般先局部拒絕再允許,最后定義一條拒絕策略(匹配完執行)

# 訪問控制規則,更多見 http://wiki.squid-cache.org/SquidFaq/SquidAcl

acl 163mail dstdomain .163.com

# URL words
#acl Cooking1 url_regex -i cooking
acl Bad_Url url_regex -i "/usr/local/squid/etc/Bad_Url.lst"

# Local network 
acl Mynetwork src 192.168.1.0/24 
#acl Arp1 arp 01:02:03:04:05:06 

acl localnet src 10.0.0.0/8     # RFC1918 possible internal network
acl localnet src 172.16.0.0/12  # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7       # RFC 4193 local private network range
acl localnet src fe80::/10      # RFC 4291 link-local (directly plugged) machine


# No Cache Site
acl No_cache dstdomain .2345.com

# Cache delete
acl Purge method PURGE

# Specific User
acl Admin src 192.168.1.11 
acl Manage src 192.168.1.15-192.168.1.25

# Bad Client IP
acl Bad_client src "/usr/local/squid/etc/Bad_client.lst"

# Max Conn
acl Max_conn maxconn 10

# Work time 
acl Work_time time MTWHF 09:00-12:00
acl Work_time time MTWHF 13:00-17:00


cache deny No_cache
always_direct allow 163mail
http_access allow Admin Purge
http_access deny Purge
http_access allow Admin
http_access allow Manage
http_access deny Bad_client
http_access deny Bad_Url
http_access deny Max_conn Mynetwork 
http_access allow MyNetwork Work_time
http_access deny Mynetwork
#http_access allow Mynetwork
http_access allow localnet
http_access allow localhost
http_access deny all

http_port 3128
dns_nameservers 114.114.114.114 8.8.8.8      # DNS服務器,優先級比 ‘/etc/resolv.conf’ 更高

#MyNetwork
acl MyNetwork src 192.168.1.0/24  192.168.18.0/24   # Local_host network
http_access allow MyNetwork


cache_dir ufs /var/cache/squid 100 16 256        #  #100M、16個子目錄、256個子子目錄,可有多個.
maximum_object_size 4 MB           #4MB, cache的最大文件數

cache_mem 32 MB                     #對象內存緩存,一般設置成服務器內存的一半或更多
half_closed_clients off              #關閉半連接 
cache_swap_high 95               #95,達到%100時刪除內容到swap_low值
cache_swap_low 80                #90,
#maximum_object_size_in_memory  512 KB   #512KB,內存cache的最大文件大小 


cache_effective_user squid       #運行用戶
cache_effective_group squid

# log 
access_log /var/log/squid/access.log
cache_log /var/log/squid/cache.log 
pid_filename /var/run/squid.pid

visible_hostname Proxy.Srv01           #顯示名
cache_mgr chenxuwq@163.com       #管理員mail


#數據過期算法,percent為百分比,低于該值則數據不過期
#refresh_pattern  [-i]  regexp  min  percent  max [option]          
# Add any of your own refresh_pattern entries above these.
#
refresh_pattern ^ftp:           1440    20%     10080
refresh_pattern ^gopher:        1440    0%      1440
refresh_pattern -i (/cgi-bin/|\?) 0     0%      0
refresh_pattern .               0       20%     4320

</tr> </tbody> </table>


vim /etc/sysctl.conf #開啟內核端口轉發

http_port 3128 intercept

</tr> </tbody> </table>

sysctl -p

增加防火墻策略(SNAT):
iptable -A input -j ACCEPT -i lo
iptables -A INPUT -p tcp --dport 3128 -j ACCEPT
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -A PREROUTING -i eth1 -p tcp --dport 80 -j REDIRECT --to-port 3128
/etc/init.d/iptables save



參考:
http://www.squid-cache.org/Doc/config/
http://wiki.squid-cache.org/ConfigExamples
http://www.deckle.co.uk/squid-users-guide/



三、反向代理

反向代理服務器又稱為 WEB 加速服務器,它位于 WEB 服務器的前端,充當 WEB 服務器的內容緩存加速器,這里根據Squid的聯結模式展開討論:

A、單Squid服務器架構
DNS服務器將解析結果指向Squid,Squid 以輪詢方式去后臺源服務器上取到數據,最終返回給Client

環境:
DNS/Squid
eth0:192.168.1.10
eth1:192.168.18.10

webServer1
eth0:192.168.1.11

webServer2
eth0:192.168.1.15

Client
eth1:192.168.18.1


1、配置DNS服務器

反向代理結合智能DNS(view)就能實現基本的CDN框架,當不同地區的Client 訪問時,DNS服務器將解析結果指向對應地區的Squid 服務器

yum install bind #

vim /etc/named.conf #主配置文件

# Controls IP packet forwarding
net.ipv4.ip_forward = 1

# Controls source route verification
net.ipv4.conf.default.rp_filter = 0

# Do not accept source routing
net.ipv4.conf.default.accept_source_route = 0

</tr> </tbody> </table>


cd /var/named/
cp -a named.localhost test.org.zone #復制模板區域文件
cp -a named.empty 1.168.192.org.zone


vim test.org.zone #正向區域文件

options {
        listen-on port 53 { 192.168.18.10;
                            192.168.1.10; };
#       listen-on port 53 { any; };
        listen-on-v6 port 53 { ::1; };
        directory       "/var/named";
        dump-file       "/var/named/data/cache_dump.db";
        statistics-file "/var/named/data/named_stats.txt";
        memstatistics-file "/var/named/data/named_mem_stats.txt";
        allow-query     { 192.168.18.0/24;
                          192.168.1.0/24; };
#       allow-query     { any; };
        recursion yes;
        dnssec-enable yes;
        dnssec-validation yes;
        dnssec-lookaside auto;
        rrset-order { order cyclic; };

        /* Path to ISC DLV key */
        bindkeys-file "/etc/named.iscdlv.key";

        managed-keys-directory "/var/named/dynamic";
};

logging {
        channel default_debug {
                file "data/named.run";
                severity dynamic;
        };
};

zone "." IN {
        type hint;
        file "named.ca";
};

include "/etc/named.rfc1912.zones";
include "/etc/named.root.key";


vim  /etc/named.rfc1912.zones        #DNS區域定義

zone "test.org" IN {
        type master;
        file "test.org.zone";
        allow-update { none; };
};

zone "1.168.192.in-adr.arpatest" IN {
        type master;
        file "1.168.192.org.zone";
        allow-update { none; };
};

</tr> </tbody> </table>


vim 1.168.192.org.zone #反向區域文件

$TTL 3H
@       IN SOA  ns.test.org. root.test.org. (
                                        1       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
@   IN  NS      ns.test.org.

ns  IN  A       192.168.18.10
www IN  A       192.168.18.10

</tr> </tbody> </table>


/etc/init.d/named start #啟動,并加入系統服務
chkconfig named on
iptables -A INPUT -m state --state NEW -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -m state --state NEW -p udp --dport 53 -j ACCEPT
service iptables save



2、Squid 配置
反向代理的大部分配置和正向代理一樣,下面是特別配置:

$TTL 3H
@       IN SOA  ns.test.org. root.test.org. (
                                        1       ; serial
                                        1D      ; refresh
                                        1H      ; retry
                                        1W      ; expire
                                        3H )    ; minimum
@       IN NS  ns.test.org.

10      IN PTR http://www.test.org.

</tr> </tbody> </table>

3、Web源服務器設置

3.1. webServer1

yum install httpd
echo "webServer1" > /var/www/html/index.html
service httpd start
chkconfig httpd on

3.2 .webServer2配置基本類似


http://www.test.org/



B、鄰居Squid服務器架構(>=2)

DNS服務器將解析結果以輪詢方式指向多臺Squid,Squid沒有緩存時會通過ICP從兄弟Squid上取到數據,最終返回給Client

環境:
DNS/Squid1
eth0:192.168.1.10
eth1:192.168.18.10

Squid2
eth0:192.168.1.15
eth1:192.168.18.15

webServer1
eth0:192.168.1.11
eth0:192.168.18.11

webServer2
eth0:192.168.1.100
eth0:192.168.18.100

Client
eth1:192.168.18.1



http_port 80 accel vhost
icp_port 3130 #ICP查詢端口
http_port 3128 #方便squidclient管理緩存

cache_peer 192.168.1.11 parent 80 0 no-query originserver round-robin name=webServer1
cache_peer 192.168.1.100 parent 80 0 no-query originserver round-robin name=webServer2

squid1、squid2 為兄弟,當 squid1 在其緩存中沒有找到請求的資源時, 通過 ICP 查詢去其鄰居中取得緩存

cache_peer 192.168.1.10 sibling 80 3130
cache_peer 192.168.1.15 sibling 80 3130

www.test.org的請求通過輪詢方式轉發到三個父節點中的一個

cache_peer_domain webServer1 webServer2 www.test.org


iptables -A INPUT -m state --state NEW -p tcp --dport 3128 -j ACCEPT
iptables -A INPUT -m state --state NEW -p tcp --dport 3130 -j ACCEPT



四:內容過濾插件--SquidGuard

SquidGuard是 Squid的一個過濾插件,該軟件會在Squid3處理請求的時候檢查SquidGuard所管理的網站分類數據庫,并根據自定義的規則進行轉向(Redirect)攔截。
http://www.squidguard.org/

1、主程序下載

wget http://www.squidguard.org/Downloads/squidGuard-1.4.tar.gz

2、path 下載:
wget http://www.squidguard.org/Downloads/Patches/1.4/squidGuard-1.4-patch-20091015.tar.gz \
http://www.squidguard.org/Downloads/Patches/1.4/squidGuard-1.4-patch-20091015.tar.gz.md5 \
http://www.squidguard.org/Downloads/Patches/1.4/squidGuard-1.4-patch-20091019.tar.gz \
http://www.squidguard.org/Downloads/Patches/1.4/squidGuard-1.4-patch-20091019.tar.gz.md5 \
http://www.squidguard.org/Downloads/Patches/1.4/squidGuard-1.4-patch-20150201.tar.gz \
http://www.squidguard.org/Downloads/Patches/1.4/squidGuard-1.4-patch-20150201.tar.gz.md5


tar -zxf squidGuard-1.4-patch-20091015.tar.gz -C /opt/downloads/squidGuard-1.4/src/
tar -zxf squidGuard-1.4-patch-20091019.tar.gz -C /opt/downloads/squidGuard-1.4/src/
tar -zxf squidGuard-1.4-patch-20150201.tar.gz -C /opt/downloads/squidGuard-1.4/src/


./configure --prefix=/usr/local/squidGuard
make && make install

groupadd squid
useradd -g squid -s /sbin/nologin squid

wget http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.4.tar.xz
tar -Jxf squid-3.5.4.tar.xz
cd squid-3.5.4
./configure --prefix=/usr/local/squid
make && make install

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!

http_port 80 accel vhost                    #accel設置squid為加速模式,vhost實現實現反向代理
http_port 3128                                 #方便squidclient管理緩存

# 設置后臺源服務器
#cache_peer  hostname  type[parent] [sibling] [multicast]  http- port  icp-port [0]  [option]      #no-query 不使用ICP查詢源服務器   round-robin 輪 詢  weigh=N 權重
cache_peer  192.168.1.11  parent  80  0     no-query  originserver  round-robin  name=webServer1
cache_peer  192.168.1.15  parent  80  0   no-query  originserver  round-robin  name=webServer2

visible_hostname Proxy.Srv01     #顯示名
  • sesese色