代理服務器

代理服務器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_port內網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 拒絕GODGOD就是源地址是192.168.2.0/24的用戶

 

透明代理(做透明代理時客戶機需要將網關設置爲代理服務器)

所謂的透明代理對於用戶來講不需要設置代理,就是說他們意識不到代理服務器的存在,只需要將網關指向代理服務器即可。

我們需要以下操作

修改主配置文件squid.conf,讓其支持透明代理,並重新加載該配置

[root@localhost squid]# vim squid.conf

僅需修改一行

http_port 內網IP3128 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代理服務器外網卡)

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

 

反相代理

作用:爲內部服務器提供緩存,提高外網訪問網站的速度

修改主配置文件squid.conf,讓其支持反相代理

[root@localhost squid]# vim 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

*cache_peer_domain zzu www.zzu.com

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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章