加油站代理服務器
前言
Squid是一個Linux系統下優秀的代理服務器軟件。Squid可以配置普通上網代理(正向代理)、反向代理、透明代理。Squid接收用戶的下載申請,並自動處理所下載的數據。當一個用戶想要下載一個主頁時,可以向 Squid 發出一個申請,要 Squid 代替其進行下載,然后 Squid 連接所申請網站並請求該主頁,接着把該主頁傳給用戶同時保留一個備份,當別的用戶申請同樣的頁面時,Squid 把保存的備份立即傳給用戶。
按照代理類型的不同,可以將 Squid 代理分為正向代理和反向代理,正向代理中,根據實現方式的不同,又可以分為普通代理和透明代理。
- 普通代理:需要客戶機在瀏覽器中指定代理服務器的地址、端口。
- 透明代理:適用於企業的網關主機(共享接入 Internet)中,客戶機不需要指定代理服務器地址、端口等信息,代理服務器需要設置防火牆策略將客戶機的 Web 訪問數據轉交給代理服務程序處理。
- 反向代理:是指以代理服務器來接受 Internet 上的連接請求,然后將請求轉發給內部網絡上的服務器,並將從服務器上得到的結果返回給 Internet 上請求連接的客戶端,此時代理服務器對外就表現為一個服務器。
環境與需求
環境
所有加油站均可以連接內網,為10.0.0.0/8網段內。
省內所有加油站通過代理訪問【https://yyt.etcsd.com/ltk】
本例:
系統:CentOS6.5。
[root@uu ~]# uname -an
Linux uu 2.6.32-431.el6.x86_64 #1 SMP Sun Nov 10 22:19:54 EST 2013 x86_64 x86_64 x86_64 GNU/Linux
配置模式為普通上網代理(正向代理)。
Squid主機IP為110.225.11.206。
系統優化
hostname zhang /etc/init.d/iptables stop chkconfig iptables off > /etc/issue cd /etc/yum.repos.d/ wget http://mirrors.neusoft.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm wget http://mirrors.163.com/.help/CentOS6-Base-163.repo sed -i 's#$releasever#6.5#g' CentOS6-Base-163.repo echo 'export LC_ALL=C'>> /etc/profile echo $LC_ALL source /etc/profile cd ~ yum install lrzsz tree lsof -y setenforce 0 getenforce echo '*/10 * * * * /usr/sbin/ntpdate pool.ntp.org >/dev/null 2>&1'>>/var/spool/cron/root chkconfig --list | awk '{print $1}' |grep -Ev "sshd|network|rsyslog|crond|sysstat"|sed -r 's#(.*)#chkconfig \1 off#g'|bash chkconfig --list |grep 3:on exit vi /etc/sysconfig/i18n --?LANG="zh_CN.UTF-8" . /etc/sysconfig/i18n
yum方法
安裝
yum安裝
注釋:版本較低,不易控制。
yum install squid -y
chkconfig --level 35 squid on
cp /etc/squid/squid.conf /etc/squid/squid.conf.bak
配置
> /etc/squid/squid.conf vim /etc/squid/squid.conf http_port 3128 cache_mem 256 MB max_filedescriptors 10240 maximum_object_size 4 MB //設置squid磁盤緩存最大文件,超過4M的文件不保存到硬盤 minimum_object_size 0 KB //設置squid磁盤緩存最小文件 maximum_object_size_in_memory 4096 KB //設置squid內存緩存最大文件,超過4M的文件不保存到內存 cache_dir ufs /var/spool/squid 1024 16 256 //定義squid的cache存放路徑 、cache目錄容量(單位M)、一級緩存目錄數量、二級緩存目錄數量 logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %<st "%{Referer}>h" "%{User-Agent}>h" %Ss:%Sh //log文件日志格式 access_log /var/log/squid/access.log combined //log文件存放路徑和日志格式 cache_log /var/log/squid/cache.log //設置緩存日志 logfile_rotate 60 //log輪循 60天 cache_swap_high 95 //cache目錄使用量大於95%時,開始清理舊的cache cache_swap_low 90 //cache目錄清理到90%時停止。 acl manager proto cache_object acl localhost src 127.0.0.1/32 ::1 acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1 acl localnet src 10.0.0.0/8 # RFC1918 possible internal network acl SSL_ports port 443 acl Safe_ports port 80 # http acl Safe_ports port 443 # https acl CONNECT method CONNECT acl http proto HTTP acl good_domain dstdomain yyt.etcsd.com http_access allow manager localhost http_access allow http good_domain http_access deny http !good_domain http_access deny !Safe_ports http_access deny CONNECT !SSL_ports http_access allow all coredump_dir /var/spool/squid 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 visible_hostname zhang cache_mgr 15666661331@163.com
初始化和啟動
killall squid squid -k parse squid -z service squid start netstat -ntpl #############################注釋:############################## squid -z #初始化緩存目錄,squid新版本3.1可以省略 /etc/init.d/squid start #啟動squid服務 squid -k check #可以檢測配置文件是否有錯;可以簡寫為-kche squid -k rec #可以重新加載配置,reconfig的簡寫; service squid restart #重啟squid服務;重啟經常性的很慢,可以先killall squid,在啟動服務; netstat -ntpl #查看端口是否打開:
在windows上設置代理
打開IE -> 工具 -> Internet選項 -> 連接 -> 局域網設置。勾選代理服務器下面的方格。設置地址為squid代理服務器IP,端口為3128。