天天看點

代理伺服器

代理伺服器proxy

應用軟體包:squid-2.6.STABLE6-3.el5

主程式:/usr/sbin/squid.conf

配置目錄:/etc/squid

主配置檔案:/etc/squid/squid.conf

端口:tcp 3128

日志檔案:/var/log/squid/access.log

其中代理伺服器有兩塊網卡一個是内網IP,一個是外網IP

安裝squid

使用yum 安裝squid

yum -y install squid

[root@localhost ~]# cd /etc/squid/  切換到配置目錄

[root@localhost squid]# vim squid.conf  編輯主配置檔案

配置檔案中大部分是注釋行,以下部分是未注釋行

acl all src 0.0.0.0/0.0.0.0

acl manager proto cache_object

acl localhost src 127.0.0.1/255.255.255.255

acl to_localhost dst 127.0.0.0/8

acl SSL_ports port 443

acl Safe_ports port 80 # http

acl Safe_ports port 21 # ftp

acl Safe_ports port 443 # https

acl Safe_ports port 70 # gopher

acl Safe_ports port 210 # wais

acl Safe_ports port 1025-65535 # unregistered ports

acl Safe_ports port 280 # http-mgmt

acl Safe_ports port 488 # gss-http

acl Safe_ports port 591 # filemaker

acl Safe_ports port 777 # multiling http

acl CONNECT method CONNECT

http_access allow manager localhost

http_access deny manager

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow localhost

http_access deny all

icp_access allow all

http_port 3128

hierarchy_stoplist cgi-bin ?

access_log /var/log/squid/access.log squid

acl QUERY urlpath_regex cgi-bin \?

cache deny QUERY

refresh_pattern ^ftp: 1440 20% 10080

refresh_pattern ^gopher: 1440 0% 1440

refresh_pattern . 0 20% 4320

acl apache rep_header Server ^Apache

broken_vary_encoding allow apache

coredump_dir /var/spool/squid

我們需要便捷的配置項包括

http_portt 内網IP:3128  預設監聽的網卡和端口

cache_mem 64MB  占據記憶體空間大小,通常是實體記憶體的1/4-1/3

reply_body_max_size 10240000  allow all    (位元組)設定或限制檔案下載下傳的最大大小

access_log /var/log/squid/access.log squid  通路日志檔案路徑

minimum_object_size 0 KB  緩存對象最小值

maximum_object_size 4096 KB  緩存對象最大值

cache_swap_low 90  緩存目錄大小超過限定的95%,則清空老資料,清除值90%不再清理

cache_swap_high 95  和cache_swap_low同時使用

visible_hostname proxy.zsl.com  代理伺服器要求的主機名(寫自己的主機名)

cache_dir ufs /var/spool/squid 100 16 256  緩存目錄(ufs是緩存格式 100 緩存目錄配置設定磁盤空間(MB) 16 緩存空間的一級子目錄個數 256 緩存空間的二級子目錄個數)

更改預設通路控制http_assess deny all 為 allow all

[root@localhost squid]# squid -k parse  檢查配置檔案是否有錯誤

[root@localhost squid]# squid -z  初始化(建立)squid緩存目錄

[root@localhost squid]# service squid start

因為我們是兩塊網卡,是以讓proxy伺服器開啟路由

[root@localhost squid] vim /etc/sysctl.conf

    設定net.ipv4.ip_forward = 1

[root@localhost squid]# service squid reload  重新加載squid服務

關于proxy的通路控制(acl)

常用的acl清單類型

src源位址  dst目标位址  arp比對源MAC位址  port端口類型  time基于時間的控制

srcdomain源域 detdomain目标域  maxconn同一用戶端的最大連接配接請求 

url_regex限制協定 urlpath_regex 

acl的使用

acl NAME 類型 位址

NAME是自己設定的

比如

acl GOD src 192.168.2.0/24

http_assess deny GOD 拒絕GOD,GOD就是源位址是192.168.2.0/24的使用者

透明代理(做透明代理時客戶機需要将網關設定為代理伺服器)

所謂的透明代理,對于使用者來講不需要設定代理,就是說他們意識不到代理伺服器的存在,隻需要将網關指向代理伺服器即可。

我們需要以下操作

修改主配置檔案squid.conf,讓其支援透明代理,并重新加載該配置

[root@localhost squid]# vim squid.conf

僅需修改一行

http_port 内網IP:3128 transparent

添加iptables規則

iptables -t nat -A PREROUTING -i eth1(内網網卡) -s 192.168.1.0/24 -p tcp --dport 80 -j REDIRECT --to-ports 3128

舉個例子

192.168.1.20和代理伺服器的内部網卡(eth1)相連,他通路www.baidu.com 時,到防火牆的路由前,源位址變為代理伺服器位址,端口由80變為本機的3128,添加此行是為了讓用戶端不在浏覽器設定中指定代理的情況iptables幫客戶機聯系到代理伺服器

(生産環境下需要在内網中部署一台DNS伺服器,且DNS伺服器可以連接配接到網際網路,客戶機DNS指向内網DNS伺服器)

設定SNAT

iptables -t nat -A POSTROUTING -s 192.168.1.0/24 -j SNAT --to-source IP(代理伺服器外網卡)

反相代理

作用:為内部伺服器提供緩存,提高外網通路網站的速度。

修改主配置檔案squid.conf,讓其支援反相代理

編輯

http_port 外網IP:80 vhost

添加以下幾行

cache_peer 内網IP1 parent 80 0 originserver weight=1 max-conn=10

cache_peer 内網IP2 parent 80 0 originserver weight=2 max-conn=20

cache_peer 内網IP3 parent 80 0 originserver weight=3 max-conn=30

cache_peer WEB伺服器ip 伺服器類型 http端口 icp端口 可選項

可選項包括 (originserver這個伺服器是個真實的server weight權重 max-conn同時相應多少請求)

*基于反向代理實作虛拟主機映射

*cache_peer 内網IP4 parent 80 0 originserver name=zsl

*cache_peer 内網IP5 parent 80 0 originserver name=zzu

*cache_peer_domain zsl www.zsl.com

繼續閱讀