背景:linux
使用squid代理配置二級squid代理(父代理)模式,訪問外部網絡服務器獲取數據c++
網絡構架:centos
根據客戶需求及雲服務商構架環境的因素,部署結構以下圖所示:瀏覽器
環境需求:緩存
服務器數量: 4臺 bash
系統:centos 6.7服務器
軟件:squid-3.0網絡
網絡: A、B兩臺服務器能訪問外部網絡,C1與C2不容許訪問外網,只能訪問A、Basync
防火牆:均開啓容許8080端口訪問tcp
部署:
一、A B C1 C2均部署squid-3.0服務,部署方式以下:
一、安裝編譯環境
yum -y install gcc make patch gcc-c++ gcc-g77 flex bison autoconf automake openssl*
二、下載squid-3.0 軟件
cd /usr/local/src/ wget http://www.squid-cache.org/Versions/v3/3.0/squid-3.0.STABLE18.tar.gz
三、解壓並安裝
tar zxvf squid-3.0.STABLE18.tar.gz cd squid-3.0.STABLE18 ./configure --prefix=/usr/local/squid-3.0 --enable-auth=basic --enable-basic-auth-helpers=NCSA --sysconfdir=/usr/local/squid-3.0/etc --bindir=/usr/local/squid-3.0/bin --sbindir=/usr/local/squid-3.0/sbin --mandir=/usr/local/squid-3.0/share/man --enable-gnuregex --enable-carp --enable-async-io=8080 --enable-removal-policies=heap,lru --enable-icmp --enable-delay-pools --enable-useragent-log --enable-referer-log --enable-kill-parent-hack --enable-snmp --enable-arp-acl --enable-htcp --enable-cache-digests --enable-default-err-language=Simplify_Chinese --enable-err-languages="Simplify_Chinese" --enable-poll --enable-linux-netfilter --disable-ident-lookups --enable-underscores --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-external-acl-helpers="ip_user" --enable-x-accelerator-vary --enable-ssl
#若是須要支持訪問https,則編譯時候須要添加 --enable-ssl
#還須要執行這個 yum -y install openssl* 不然會報錯
#structs.h:1161: 錯誤:ISO C++ 不容許聲明無類型的‘SSL_SESSION’
#從新解壓,編譯執行安裝
cd /home/lvnian/tools rm -rf squid-3.0.STABLE20 tar xf squid-3.0.STABLE20.tar.gz make make install useradd squid -s /sbin/nologin mkdir -p /var/spool/squid mkdir -p /var/log/squid chown nobody:nobody -R /var/log/squid/ chown nobody:nobody -R /var/spool/squid/ touch /usr/local/squid-3.0/var/logs/cache.log touch /usr/local/squid-3.0/var/logs/store.log chown nobody:nobody -R /usr/local/squid-3.0/var/logs mkdir -p /vdb/squid/logs mkdir -p /vdb/squid/spool/squid touch /vdb/squid/logs/squidaccess.log touch /vdb/squid/logs/cache.log touch /vdb/squid/logs/store.log chown nobody:nobody -R /vdb/squid
四、備份默認配置
cd /usr/local/squid-3.0/etc/ cp squid.conf squid.conf-default > squid.conf
五、配置修改
5.一、A與B squid.conf 配置
cat >> squid.conf <<eof acl download urlpath_regex -i \.mp3 \.avi \.rmvb \.rm \.ra \.ram \.mpe \.smi \.rar acl protos proto HTTP FTP acl methods method GET POST PUT HEAD acl IMpports port 80 acl IMpports port 443 acl IMpports port 8080 acl CONNECT method CONNECT http_access allow IMpports http_access allow protos http_access allow methods http_access allow CONNECT IMpports http_access deny CONNECT !IMpports http_access allow all http_port 8080 hierarchy_stoplist cgi-bin ? logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh access_log /vdb/squid/logs/squidaccess.log combined cache_log /vdb/squid/logs/cache.log cache_store_log /vdb/squid/logs/store.log refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern . 0 20% 4320 coredump_dir /vdb/squid/spool/squid pid_filename /var/run/squid.pid cache_dir ufs /vdb/squid/spool/squid 256 8 16 cache_mem 256 MB visible_hostname Test_squidproxy eof
5.二、C1與C2 squid.conf配置
cat >> squid.conf <<eof #cache_peer 120.24.x.x parent 8080 0 login=用戶名:密碼 default #父代理是認證模式 cache_peer A-IP parent 8080 0 proxy-only no-query defaule #父代理是無認證訪問模式,default設置默認使用代理 cache_peer B-IP parent 8080 0 proxy-only no-query #均衡分佈鏈接 never_direct allow all #可使用cache_peer指令添加Squid將要求內容的父代理。此外,您能夠分別使用always_direct或never_direct控制是直接仍是間接獲取內容 acl protos proto HTTP FTP acl methods method GET POST PUT HEAD acl IMpports port 80 acl IMpports port 443 acl IMpports port 8080 acl CONNECT method CONNECT http_access allow IMpports http_access allow protos http_access allow methods http_access allow CONNECT IMpports http_access deny CONNECT !IMpports http_access allow all http_port 8080 hierarchy_stoplist cgi-bin ? logformat combined %>a %1tr %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh access_log /var/log/squid/access.log combined refresh_pattern ^ftp: 1440 20% 10080 refresh_pattern ^gopher: 1440 0% 1440 refresh_pattern . 0 20% 4320 coredump_dir /var/spool/squid pid_filename /var/run/squid.pid cache_dir ufs /var/spool/squid 256 8 16 cache_mem 64 MB visible_hostname longtianlu_test_squidproxy eof
六、啓動方式:
6.1 A 與B服務啓動方式
./squid -z #初始化緩存目錄 ./squid -N -d1 #顯示啓動過程,觀察是否有報錯信息 /usr/local/squid-3.0/sbin/squid -s #後臺啓動 #加入開機自啓動 echo "/usr/local/squid-3.0/sbin/squid -s " >>/etc/rc.local
6.2 C1與C2服務啓動方式
./squid -z #初始化緩存目錄 ./squid -N -d1 #顯示啓動過程,觀察是否有報錯信息 /usr/local/squid-3.0/sbin/squid -s -D #後臺啓動並不檢測DNS,不加-D啓動則會報錯 #加入開機自啓動 echo "/usr/local/squid-3.0/sbin/squid -s -D" >>/etc/rc.local
七、測試驗證環境經過IPTABLES 將禁止C1\C2訪問外部網絡,僅容許訪問A與B
:INPUT ACCEPT [0:0] :OUTPUT ACCEPT [0:0] -A INPUT -p tcp --dport 53 -A OUTPUT -p tcp --sport 53 -A INPUT -p udp --dport 53 -A OUTPUT -p udp --sport 53 -A OUTPUT -d 127.0.0.1 -j ACCEPT -A OUTPUT -d 172.0.0.0/8 -j ACCEPT -A OUTPUT -d A-IP -j ACCEPT -A OUTPUT -d B-IP -j ACCEPT -A OUTPUT -j DROP
八、系統參數修改
net.core.somaxconn = 262144 net.ipv4.tcp_max_orphans = 3276800 net.ipv4.tcp_max_syn_backlog = 262144 net.ipv4.tcp_timestamps = 0 net.ipv4.tcp_synack_retries = 1 net.ipv4.tcp_syn_retries = 1 net.ipv4.tcp_tw_recycle = 1 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_mem = 94500000 915000000 927000000 net.ipv4.tcp_fin_timeout = 1 net.ipv4.tcp_keepalive_time = 15 net.ipv4.ip_local_port_range = 1024 65535
九、檢測驗證
一、經過netstat -nuplt查看8080端口是否啓動 二、ps -ef |grep squid 進程是否正常 三、經過終端或者瀏覽器配置http代理 C1 or C2 端口8080 測試是否能訪問外部網絡