正向代理服務器可滿足內網僅有一台服務器可以上網,而要供內網所有機器上網的需求,也可以用於爬蟲的代理訪問。在實踐中我將Squid作為爬蟲代理服務器,實現了多IP切換的功能,將在后續文章中記錄實現過程。

安裝

系統環境: CentOS 7.0
Squid版本:3.5.20

  1. 源代碼安裝

到官方網站 http://www.squid-cache.org/Versions/ 查找版本號,找到下載鏈接,以v3.5.20為例,安裝步驟如下:

1
2
3
4
5
6
cd /tmp
wget http://www.squid-cache.org/Versions/v3/3.5/squid-3.5.20.tar.gz
tar xzf squid-3.5.20.tar.gz
cd squid-3.5.20
./configure --with-MYOPTION --with-MYOPTION2 etc # 具體參數請參考官方文檔
make && make install

更多配置詳情參考: http://wiki.squid-cache.org/SquidFaq/CompilingSquid

  1. 包管理安裝

centos 用 sudo yum install squid 即可完成安裝。

配置

配置文件位置在 /etc/squid/squid.conf ,修改默認配置文件:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#
# Recommended minimum configuration:
#
 
# Example rule allowing access from your local networks.
# Adapt to list your (internal) IP networks from where browsing
# should be allowed
# 內網控制,按需修改
acl localnet src 10.0.0.0/8 # RFC1918 possible internal network
acl localnet src 172.16.0.0/12 # RFC1918 possible internal network
acl localnet src 192.168.0.0/16 # RFC1918 possible internal network
acl localnet src fc00::/7 # RFC 4193 local private network range
acl localnet src fe80::/10 # RFC 4291 link-local (directly plugged) machines
 
# 配置可訪問的端口
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
 
#
# Recommended minimum Access Permission configuration:
#
# Deny requests to certain unsafe ports
# 拒絕其他非安全端口的訪問
http_access deny !Safe_ports
 
# Deny CONNECT to other than secure SSL ports
# 拒絕443以外的端口訪問
http_access deny CONNECT !SSL_ports
 
# Only allow cachemgr access from localhost
# 允許本機訪問
http_access allow localhost manager
http_access deny manager
 
# We strongly recommend the following be uncommented to protect innocent
# web applications running on the proxy server who think the only
# one who can access services on "localhost" is a local user
#http_access deny to_localhost
 
#
# INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
#
 
# Example rule allowing access from your local networks.
# Adapt localnet in the ACL section to list your (internal) IP networks
# from where browsing should be allowed
# 允許內網
http_access allow localnet
http_access allow localhost
 
# And finally deny all other access to this proxy
# 拒絕所有
http_access deny all
 
# Squid normally listens to port 3128
# 默認對外端口為3128
http_port 3128
 
# Uncomment and adjust the following to add a disk cache directory.
# 設置緩存文件位置、cache目錄容量(單位M)、一級緩存目錄數量、二級緩存目錄數量
# 取消注釋
cache_dir ufs /var/spool/squid 100 16 256
 
# Leave coredumps in the first cache dir
coredump_dir /var/spool/squid
 
#
# Add any of your own refresh_pattern entries above these.
#
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,本文不詳細闡述具體參數的作用,如有需要可查閱相關文檔。

文檔參考資料:

  1. http://www.squid-cache.org/Doc/
  2. squid中文權威指南

運行

初次配置好或者修改緩存文件位置參數(cache_dir)之后,需要運行squid -z 初始化緩存目錄

設置開機啟動:systemctl enable squid

運行:systemctl start squid

使用

按上述設置僅支持本機或者網段為10.0.0.0/8、172.16.0.0/12、192.168.0.0/16等內網訪問,可根據實際情況增加控制參數,或者將文件中http_access deny all 改為 http_access allow all即可支持所有網段訪問。

更改瀏覽器中代理服務器設置,以火狐瀏覽器為例,填寫相應的squid服務器ip和端口號。

瀏覽器代理設置瀏覽器代理設置

訪問http://httpbin.org/ip檢測ip地址

1
2
3
{
"origin": "182.xxx.xxx.148, 139.xxx.xxx.66"
}

返回數據中有2個ip,第一個為本機的源ip,第二個為squid代理服務器ip,說明正向代理服務器搭建成功。