Squid 是一款缓存代理服务器软件,广泛用于网站的负载均衡架构中,常见的缓存服务器还有varnish、ATS等。

正向代理服务器可满足内网仅有一台服务器可以上网,而要供内网所有机器上网的需求,也可以用于爬虫的代理访问。在实践中我将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,说明正向代理服务器搭建成功。

Squid 搭建正向代理服务器的更多相关文章

  1. 使用Squid搭建HTTPS代理服务器

    由于经常去的一些国外网站如Google.Blogspot.Wordpress被"出现了技术问题",访问不了,于是我在自己的DigitalOcean云主机上搭建了一个 Squid代理 ...

  2. Nginx搭建正向代理服务器

    配置如下: server { listen 8888;                                                   #监听端口 location / { res ...

  3. centos7.3使用squid搭建代理服务器

    centos7.3使用squid搭建代理服务器 1 安装 yum install squid 2 编辑 vi /etc/squid/squid.conf 3 设置 最底部增加 如下http_acces ...

  4. centos7.6_x86_64使用Squid搭建代理服务器让windows上网

    centos7.6_x86_64使用Squid搭建代理服务器让windows上网 windows机器很多站点访问受限,可以在没有限制外网的机器上面搭建代理服务器,其它电脑可以配置代理通过这台不受限制的 ...

  5. Nginx搭建反向代理服务器

    [大型网站技术实践]初级篇:借助Nginx搭建反向代理服务器   一.反向代理:Web服务器的“经纪人” 1.1 反向代理初印象 反向代理(Reverse Proxy)方式是指以代理服务器来接受int ...

  6. Squid实现正向代理及访问控制--技术流ken

    Squid及正向代理简介 Squid cache(简称为Squid)是一个流行的自由软件,它符合GNU通用公共许可证.Squid作为网页服务器的前置cache服务器,可以代理用户向web服务器请求数据 ...

  7. 在Centos7下搭建Socks5代理服务器

    在Centos7下搭建Socks5代理服务器 http://blog.51cto.com/quliren/2052776   采用socks协议的代理服务器就是SOCKS服务器,是一种通用的代理服务器 ...

  8. linux_UBUNTU 12.04 上使用 SQUID 架设HTTP正向代理服务器

    配置普通HTTP正向代理 安装   1 sudo apt-get install squid squid-common 配置 squid3   1 sudo vim /etc/squid3/squid ...

  9. squid搭建http/https代理服务器

    前言:笔者使用的长城宽带,访问国外网站,比如mysql,nginx等站点的速度.......,你懂得,于是想到使用腾讯云主机搭建squid代理服务器,这里搭建的是一般代理服务器,squid代理服务器分 ...

随机推荐

  1. tensorflow serving 中 No module named tensorflow_serving.apis,找不到predict_pb2问题

    最近在学习tensorflow serving,但是运行官网例子,不使用bazel时,发现运行mnist_client.py的时候出错, 在api文件中也没找到predict_pb2,因此,后面在网上 ...

  2. vue组件知识点

    1.组件的定义 const component = { props: { //外部父组件约束子组件的 里面不要修改 可以通过触发事件来修改 active: Boolean, propOne: Stri ...

  3. 移动端web页面滚动不流畅,卡顿闪烁解决方案

    移动端web页面滚动不流畅,卡顿闪烁解决方案   1.ios端的-webkit-overflow-scrolling属性可控制页面滚动效果,设置如下实现惯性滚动和弹性效果: -webkit-overf ...

  4. 如何解决Android Studio解决DDMS真机/模拟器无法查看data目录问题

    android app开发中,文件.SharedPreference或数据库默认保存在/data文件夹下,有时需要查看该文件夹下数据文件是否创建成功时,发现竟然打不开data目录: 具体解决方式如下: ...

  5. MySql数据类型范围

    [MySql数据类型范围] 参考:http://blog.sina.com.cn/s/blog_4f925fc30102edg8.html

  6. 针对SO交期回写的工厂日历功能调整

    针对所有SO回写的交期,在最终写入SAP系统时,如果交期落在周日的,则自动往后延迟一天到周一,前期已经开发的长节假日UI维护的功能不变(按照UI的开始和结束时间跳过此段时间不规划). 当加1天时落在国 ...

  7. day28 网络协议OSI 及socket模块

    反射都是操作对象中的__dict__的内容 class Student: def study(self): print("正在学习....") stu = Student() if ...

  8. appium多机并行测试

    在实际应用中需要对多个机型并行测试,节省时间 多机测试的思路 启动多个appium server与多台机器交互(android和ios均可)   注意:一定要使用node安装appium的命令行,使用 ...

  9. uWSGI和WSGI区别

    uWSGI是一个Web服务器,它实现了WSGI协议.uwsgi.http等协议.Nginx中HttpUwsgiModule的作用是与uWSGI服务器进行交换.WSGI是一种Web服务器网关接口.它是一 ...

  10. Java 中的 static 使用

    Java语言基础--static 0.目录 8.static 8.1 Java 中的 static 使用之静态变量 8.2 Java 中的 static 使用之静态方法 8.3 Java 中的 sta ...