Squid Linux 代理服務器



簡介:

Squid 是 Linux/Unix 平台下最為流行的高性能免費應用層代理服務器,它具有權限管理靈活、性能高和效率快的特點。

代理服務器可以提供文件緩存、復制和地址過濾等服務,充分利用有限的出口帶寬,加快內部主機的訪問速度。

同時可以作為一個防火牆,隔離內外網,並且能夠提供監控網絡和記錄傳輸信息的功能,加強局域網的安全性。

代理服務器工作原理:

1、客戶端 A 向代理服務器發出訪問 Internet 的請求 。
2、代理服務器接受請求后,首先與訪問控制列表中的訪問規則相匹配,如果滿足規則,則在緩存中查找是否有需要的資源信息。
3、如果緩存中存在客戶端 A 的請求信息,那么將此信息返回給客戶端 A ; 如果沒有代理服務器將代替客戶端去向 Internet 請求指定的信息。
4、Internet 上的主機將請求的信息發送到代理服務器,代理服務器會將信息存入緩存中。
5、代理服務器將 Internet 上主機的返回信息傳給客戶端 A 。
6、當客戶端 B 也請求相同的信息時。
7、代理服務器也會接受請求后,與訪問控制列表中的規則相匹配。、
8、如果滿足規則,代理服務器將會把緩存中的信息直接傳給客戶端 B 。

Squid 對硬件的要求:

1、內存短缺會嚴重影響性能。
2、硬盤空間也是一個重要因素,更多的磁盤空間意味着更多的緩存目錄和更高的命中率。
3、Squid 需要使用硬盤作為緩存 Cache ,所以對硬盤的存取速度要求較高,最好配置萬轉高速 SCSI 硬盤和磁盤陣列。

一、安裝 Squid

shell > rpm -qa squid
shell >
shell > yum -y install squid

## 這里選擇使用 yum 安裝 ,也可以選擇編譯安裝,下載地址:http://www.squid-cache.org/Versions/v3/3.4/squid-3.4.10.tar.gz

二、配置 Squid

shell > cd /etc/squid/
shell > cp squid.conf squid.conf.bak

## 首先備份一下 squid.conf 文件

shell > grep -P -v '#|^$' squid.conf

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 SSL_ports port 443
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 localnet
http_access allow localhost
http_access deny all
http_port 3128
hierarchy_stoplist cgi-bin ?
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

## 這是默認 Squid 的有效配置

shell > > squid.conf      # 清空默認配置文件
shell > vim squid.conf    # 重新寫入配置( 當然你也可以直接修改原來的配置文件 )

http_port 192.168.1.88:3128

## 指定在哪個 IP 跟 端口監聽客戶機請求,默認為所有 IP 的 3128 端口,即 http_port 3128

cache_mem 64 MB

## 內存緩存大小,指定可以使用多少物理內存作為高速緩存。如果此服務器就是專門的緩存服務器,可以指定為物理內存的一半。否則不應大於三分之一

cache_dir ufs /opt/squid_cache 4096 16 256

## 磁盤緩存區大小,ufs                為緩存的存儲類型,一般為 ufs
                   /opt/squid_cache   指定緩存存放目錄
                   4096               緩存空間最大為 4096 MB
                   16                 硬盤緩存目錄下可以創建 16 個一級子目錄,默認為 16256                每個一級子目錄下可以創建 256 個二級目錄,默認為 256 個

## 之所以創建這么多子目錄是因為:

   如果子目錄太少,則存儲在一個子目錄下的文件數目會很多很多,會導致系統尋找某一個文件的時間增加,從而影響整體性能

cache_effective_user squid

## 使用緩存的有效用戶,如果系統沒有此用戶需手動添加

cache_effective_group squid

## 使用緩存的有效組,如果系統沒有此組需手動添加

dns_nameservers 8.8.8.8

## 為了使 Squid 能解析域名需定義真實 DNS 地址

cache_access_log /var/log/squid/access.log

## 訪問日志文件路徑,記錄了用戶訪問 Internet 的詳細信息,可以查看每用戶的上網記錄

cache_log /var/log/squid/cache.log

## 緩存日志文件路徑,記錄了緩存相關的日志信息

cache_store_log /var/log/squid/store.log

## 網頁緩存日志文件路徑,記錄了網頁在緩存中調用情況

visible_hostname 192.168.1.88

## 定義運行 Squid 的主機名稱,當訪問發生錯誤時,該選項會顯示在錯誤提示網頁中

cache_mgr wangxiaoqiang888@163.com

## 設置 Squid 代理服務器管理員 E-mail 地址,訪問發生錯誤時會提示在錯誤頁面中

acl all src 0.0.0.0/0.0.0.0

## 訪問控制列表,默認拒絕所有訪問客戶機的請求

   acl 格式:acl 列表名 列表類型 [-i] 列表值

## 列表名    區分各個訪問控制列表,不能相同,盡量使用有意義的名稱,如:badurl、badip、clientip、work time 等

## 列表類型  src                     源 IP 地址( 客戶機 IP 地址 )
             dst                     目標 IP 地址 ( 服務器 IP 地址 )
             srcdomain               源名稱( 客戶機所屬的域 )
             dstdomain               目標名稱( 服務器所屬的域 )
             time                    一天中的時間和一周內的一天   
             url_regex               URL 規則表達式匹配
             urlpath_regex:URL-path  略去協議和主機名的 URL 規則表達式匹配
             proxy_auth              通過外部程序進行用戶認證
             maxcocnn                單一 IP 最大連接數
             time                    時間段:[星期][時間段]
                                     [星期]:M( Monday    ,星期一 )
                                             T( Tuesday   , 星期二 )
                                             W( Wednesday , 星期三 )
                                             H( Thursday  , 星期四 )
                                             F( Friday    ,星期五 )
                                             A( Saturday  , 星期六 )
                                             S( Sunday    , 星期日 )
                                     [時間段]:可以表示為 10:00-21:00

## -i        選項,加 -i 表示列表值的不區分大小寫

## 列表值    對應列表類型,值也不同,可以是 IP 、時間、域名等

http_access allow all

## 允許或拒絕某個訪問控制列表的 HTTP 請求,格式為:http_access [allow|deny] 列表名

三、初始化 Squid

shell > mkdir /opt/squid_cache
shell > chown -R squid.squid /opt/squid_cache/

## 創建緩存目錄並更改屬主、屬組,如果沒有 squid 用戶跟組要先創建

shell > squid -k check
2015/01/05 15:11:04| ERROR: '0.0.0.0/0.0.0.0' needs to be replaced by the term 'all'.
2015/01/05 15:11:04| SECURITY NOTICE: Overriding config setting. Using 'all' instead.
2015/01/05 15:11:04| WARNING: (B) '::/0' is a subnetwork of (A) '::/0'
2015/01/05 15:11:04| WARNING: because of this '::/0' is ignored to keep splay tree searching predictable
2015/01/05 15:11:04| WARNING: You should probably remove '::/0' from the ACL named 'all'
squid: ERROR: No running copy

## squid -k check 檢查 squid.conf 是否配置有誤,最后一行是因為沒有啟動 squid 服務
## 前面五行是因為 3.0 版本后不讓使用 all 這個列表名,默認就有一個 all 為所有
## 允許所有主機能夠使用代理就變成了 http_access allow all ( 不需要 acl 申明了 ,即刪除 acl all src 0.0.0.0/0.0.0.0 )

shell > squid -k check
squid: ERROR: No running copy

## 刪除之后正常了,下面初始化

shell > squid -z
2015/01/05 15:24:01| Creating Swap Directories
2015/01/05 15:24:01| /opt/squid_cache exists
2015/01/05 15:24:01| Making directories in /opt/squid_cache/00
2015/01/05 15:24:01| Making directories in /opt/squid_cache/01
2015/01/05 15:24:01| Making directories in /opt/squid_cache/02
2015/01/05 15:24:01| Making directories in /opt/squid_cache/03
2015/01/05 15:24:01| Making directories in /opt/squid_cache/04
2015/01/05 15:24:01| Making directories in /opt/squid_cache/05
2015/01/05 15:24:01| Making directories in /opt/squid_cache/06
2015/01/05 15:24:01| Making directories in /opt/squid_cache/07
2015/01/05 15:24:01| Making directories in /opt/squid_cache/08
2015/01/05 15:24:02| Making directories in /opt/squid_cache/09
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0A
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0B
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0C
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0D
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0E
2015/01/05 15:24:02| Making directories in /opt/squid_cache/0F

## 看到創建了好多目錄,接着看一下子目錄下的二級目錄

shell > ls /opt/squid_cache/00/
00  08  10  18  20  28  30  38  40  48  50  58  60  68  70  78  80  88  90  98  A0  A8  B0  B8  C0  C8  D0  D8  E0  E8  F0  F8
01  09  11  19  21  29  31  39  41  49  51  59  61  69  71  79  81  89  91  99  A1  A9  B1  B9  C1  C9  D1  D9  E1  E9  F1  F9
02  0A  12  1A  22  2A  32  3A  42  4A  52  5A  62  6A  72  7A  82  8A  92  9A  A2  AA  B2  BA  C2  CA  D2  DA  E2  EA  F2  FA
03  0B  13  1B  23  2B  33  3B  43  4B  53  5B  63  6B  73  7B  83  8B  93  9B  A3  AB  B3  BB  C3  CB  D3  DB  E3  EB  F3  FB
04  0C  14  1C  24  2C  34  3C  44  4C  54  5C  64  6C  74  7C  84  8C  94  9C  A4  AC  B4  BC  C4  CC  D4  DC  E4  EC  F4  FC
05  0D  15  1D  25  2D  35  3D  45  4D  55  5D  65  6D  75  7D  85  8D  95  9D  A5  AD  B5  BD  C5  CD  D5  DD  E5  ED  F5  FD
06  0E  16  1E  26  2E  36  3E  46  4E  56  5E  66  6E  76  7E  86  8E  96  9E  A6  AE  B6  BE  C6  CE  D6  DE  E6  EE  F6  FE
07  0F  17  1F  27  2F  37  3F  47  4F  57  5F  67  6F  77  7F  87  8F  97  9F  A7  AF  B7  BF  C7  CF  D7  DF  E7  EF  F7  FF

四、啟動 Squid

shell > service squid start
正在啟動 squid:....................                       [失敗]

## 啟動失敗,馬上想到看日志

shell > cat /var/log/squid/cache.log

2015/01/05 15:40:31| /opt/squid_cache/swap.state: (13) Permission denied
FATAL: commonUfsDirOpenSwapLog: Failed to open swap log.

## 發現這兩條可疑記錄,權限問題,但是 /opt/squid_cache 目錄的屬主跟屬組已經是 squid 了,那么想到可能是 SELinux 權限

shell > getenforce
Enforcing
shell > setenforce 0

## 發現 SELinux 是開啟的,那么關閉試試

shell > service squid start
正在啟動 squid:.                                          [確定]
shell > netstat -anpt | grep 3128
tcp        0      0 192.168.1.88:3128           0.0.0.0:*                   LISTEN      2564/(squid)

## 果然成功啟動了,記得修改 /etc/selinux/config 這個配置文件,並重啟服務器

五、測試 Squid

shell > curl -x 192.168.1.88:3128 www.sina.com

## 訪問 www.sina.com 然后去看看緩存目錄下有沒有緩存文件

shell > find /opt/squid_cache/ -type f
/opt/squid_cache/00/00/00000004
/opt/squid_cache/swap.state

## 其中這個 00000004 里面就是 www.sina.com 的首頁內容

shell > iptables -I INPUT 4 -i eth0 -p tcp --dport 3128 -j ACCEPT

## 插入一條防火牆規則用客戶端瀏覽器測試代理服務

## 搜狗瀏覽器 -- 工具 -- 選項 -- 高級 -- 代理設置 -- 編輯 -- 連接 -- 局域網設置 -- 填寫代理服務器 IP 端口 -- 確定

## 多次訪問網站,體驗速度

## 再次查看緩存目錄

shell > find /opt/squid_cache/ -type f
/opt/squid_cache/00/00/0000001A
/opt/squid_cache/00/00/0000008E
/opt/squid_cache/00/00/000000D6
/opt/squid_cache/00/00/0000005B
/opt/squid_cache/00/00/000000B5
/opt/squid_cache/00/00/000000F6
/opt/squid_cache/00/00/000000C7
/opt/squid_cache/00/00/0000005E
/opt/squid_cache/00/00/00000078
/opt/squid_cache/00/00/00000027
/opt/squid_cache/00/00/000000AA
/opt/squid_cache/00/00/00000065
/opt/squid_cache/00/00/000000EC
/opt/squid_cache/00/00/00000045
/opt/squid_cache/00/00/000000C4
/opt/squid_cache/00/00/00000024
/opt/squid_cache/00/00/00000080
/opt/squid_cache/00/00/0000004E
/opt/squid_cache/00/00/000000A4
/opt/squid_cache/00/00/000000DE
/opt/squid_cache/00/00/000000E2
/opt/squid_cache/00/00/00000049
/opt/squid_cache/00/00/00000070
/opt/squid_cache/00/00/000000DD
/opt/squid_cache/00/00/000000BE
.....

## 發現多了好多內容,這就是緩存下來的信息,當下次訪問相同的資源時,則代理服務器會直接把緩存中的信息返回給客戶端

六、訪問控制列表

1、禁止 192.168.1.110 使用代理上網

acl badclientip src 192.168.1.110
http_access deny badclientip

## 順序很重要,把想拒絕的放前面,然后重新加載配置文件,當 1.110 訪問網頁時則提示拒絕訪問

2、禁止一個網段上網

acl badclientnet src 192.168.1.0/24
http_access deny badclientnet

## /24 也可以寫成 /255.255.255.0 的形式

3、禁止訪問 IP 為 61.135.169.121 的網站

acl badwebserver dst 61.135.169.121
http_access deny badwebserver

## 這里其實直接寫成 baidu.com www.baidu.com 也是可以的

4、禁止訪問域名為 www.163.com 的網站

acl badwebserver dstdomain www.163.com
http_access deny badwebserver

## 用戶可以訪問 163.com mail.163.com

5、禁止用戶訪問域名中包含 163.com 的網站

acl badwebserver url_regex 163.com
http_access deny badwebserver

## 這樣就可以全面禁止所有包含 163.com 的網站

6、禁止用戶訪問域名包含 sex 的網站

acl badwebserver url_regex sex
http_access deny badwebserver

## 用戶無法訪問 www.sex.com www.abc.com/sex/index.html 等站點

7、限制 IP 為 192.168.1.110 的並發最大連接數為 10

acl clientip src 192.168.1.110
acl conn10 maxconn 10
http_access deny clientip conn10

## 通過兩條 acl 來限制最大並發連接數,注意 有時候做完就直接不能訪問網站了,是因為連接上限了,可以打開一個網頁的連接數已經超過了 10

8、禁止 1.0 網段的 IP 在 9:00-18:00 上網

acl clientnet src 192.168.1.0/24
acl worktime time MTWHF 9:00-18:00
http_access deny clientnet worktime

## 只能說很強大

9、禁止下載 *.mp3 *.mp4 *.exe *.zip *.rar 類型的文件

acl badfile urlpath_regex -i .mp3 .mp4 .exe .zip .rar
http_access deny badfile

## urlpath_regex 略去協議和主機名的 URL 規則表達式匹配方式,-i 忽略列表值的大小寫,此規則可以禁止下載這些后綴的文件


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM