squid正向代理

centos系统自带squid包,安装命令是:

1.安装squid

#yum install -y squid

2.修改squid配置文件

# vim /etc/squid/squid.conf (配置文件路径)

cache_dir ufs /var/spool/squid 100 16 256 (打开前面的#号)

cache_mem 128 MB

....在refresh_pattern下插入代码

refresh_pattern \.(jpg|png|gif|js|css|mp3|mp4|exe) 1440 20% 2880 ignore-reload   (ignore-reload忽略重新加载)

visible_hostname wyp

“cache_dir”这个用来指定本地磁盘上的缓存目录,后边的 1024 为大小,单位是 M,具体根据你的磁盘大小决定。

“cache_mem”它用来规定缓存占用内存的大小,即把缓存的东西存到内存里,具体也需要根据你机器的内存定,如果你的机器只是跑 Squid 服务,那么留给系统 512M 内存外,其他可以都分给 squid。

“refresh_pattern”用来匹配缓冲的对象

“visible_hostname”必须写一个名称,否则报错,或启动失败

3.配置文件保存好后,可以先检测一下是否有语法错误:

squid -kcheck     可简写为-kch

4.重启squid

squid -kreconfigur  可简写为-kre

5.在启动前还得再做一件事,就是初始化缓存目录:

mkdir /data/cache

# chown -R squid:squid /data/cache/

# squid -z

2013/06/12 16:25:14| Creating Swap Directories

2013/06/12 16:25:14| /data/cache exists

好了,初始化完成后,就可以启动 squid 了:

# /etc/init.d/squid start 启动squid)

正在启动 squid:.

测试:

1.用IE来测试,设置代理IP和端口,用网页打开网站。

2.用curl来测试,如: curl -xlocalhost:3128 -I www.baidu.com

3.用tcpdump -nn port 3128来监视端口流量

有时,我们会有这样的需求,就是想限制某些域名不能通过代理访问,或者说只想代理某几个域名,这如何做呢?在 squid.conf 中找到:

acl CONNECT method CONNECT

在其下面添加四行:

acl http proto HTTP

acl good_domain dstdomain .apelearn.com .aminglinux.com

http_access allow http good_domain    #允许访问白名单

http_access deny http !good_domain    #拒绝访问非白名单

其中我的白名单域名为 ”.apelearn.com .aminglinux.com”,这里的.表示万能匹配。前面

可以是任何字符,你只需要填写你的白名单域名即可。重启 Squid 再来测测看:

/etc/init.d/squid restart

# curl -xlocalhost:80 -I http://www.baidu.com/

访问百度已经变为 403 了。如果要设置黑名单呢?道理是一样的:

acl http proto HTTP

acl bad_domain dstdomain .sina.com .souhu.com

http_access allow http !bad_domain

http_access deny http bad_domain

重启 squid 后,测试:

# /etc/init.d/squid restart

# curl -xlocalhost:80 http://www.sina.com/ -I

# curl -xlocalhost:80 http://www.baidu.com/ -I

baidu.com 可以访问,而 sina.com 不可以访问了。

squid正向代理:

(来源51博客:http://rachy.blog.51cto.com/11428504/1905325

使用centos源中自带的squid包安装:

[[email protected] ~]# yum install -y squid

编辑squid配置文件:

[[email protected] ~]# vim /etc/squid/squid.conf

打开注释行:

cache_dir ufs /var/spool/squid 100 16 256

说明:缓存目录为/var/spool/squid,缓存大小为100MB,该目录下有16个一级子目录,每个子目录下又有256个二级子目录。

并在其下面增加一行:

cache_mem 64 MB

注意:大小不能超过上面的总大小100MB

在最后面添加要缓存的静态项:

refresh_pattern \.(jpg|png|gif|js|css|mp3|mp4) 1440  20%  2880 ignore-reload

最后面添加代理服务器要显示的主机名:

visible_hostname test.com

启动squid:

[[email protected] ~]# /etc/init.d/squid start

init_cache_dir /var/spool/squid... 正在启动 squid:.       [确定]

查看进程和端口(默认3128):

[[email protected] ~]# ps aux | grep squid

root      1738  0.0  0.1  15216  2688 ?        Ss   05:11   0:00 squid -f /etc/squid/squid.conf

squid     1740  0.1  0.4  17816  9072 ?        S    05:11   0:00 (squid) -f /etc/squid/squid.conf

squid     1742  0.0  0.0   3272   888 ?        S    05:11   0:00 (unlinkd)

root      1745  0.0  0.0   5980   744 pts/0    S+   05:12   0:00 grep squid

[[email protected] ~]# netstat -lnp | grep squid

tcp        0      0 :::3128                     :::*                        LISTEN      1740/(squid)

udp        0      0 0.0.0.0:40410               0.0.0.0:*                               1740/(squid)

udp        0      0 :::37508                    :::*                                    1740/(squid)

使用浏览器测试:

在浏览器中设置使用该代理:工具——Internet选项——连接——局域网设置——勾选“为LAN使用代理服务器”——高级——HTTP:192.168.147.139:3128——确定——确定——确定

使用浏览器访问www.baidu.com,可见能成功访问到百度。为了验证走的是我们的代理服务器,我们可以使用tcpdump工具抓个包看看

安装tcpdump抓包工具:

[[email protected] ~]# yum install -y tcpdump

squid正向代理

此时,访问百度就可以看到抓到很多包,说明确实走的是我们的代理服务器。

同时在缓存目录下产生了很多文件:

squid正向代理

使用curl测试:

[[email protected] ~]# curl -x127.0.0.1:3128 www.baidu.com -I

HTTP/1.0 200 OK

Server: bfe/1.0.8.18

Date: Fri, 10 Mar 2017 13:25:23 GMT

Content-Type: text/html

Content-Length: 277

Last-Modified: Mon, 13 Jun 2016 02:50:02 GMT

ETag: "575e1f5a-115"

Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform

Pragma: no-cache

Accept-Ranges: bytes

X-Cache: MISS from test.com

X-Cache-Lookup: MISS from test.com:3128

Via: 1.0 test.com (squid/3.1.23)

Connection: keep-alive

[[email protected] ~]# curl -x127.0.0.1:3128 www.qq.com -I

HTTP/1.0 200 OK

Server: squid/3.5.20

Date: Fri, 10 Mar 2017 13:26:13 GMT

Content-Type: text/html; charset=GB2312

Vary: Accept-Encoding

Vary: Accept-Encoding

Expires: Fri, 10 Mar 2017 13:27:13 GMT

Cache-Control: max-age=60

Vary: Accept-Encoding

Vary: Accept-Encoding

X-Cache: HIT from shanghai.qq.com

X-Cache: MISS from test.com

X-Cache-Lookup: MISS from test.com:3128

Via: 1.0 test.com (squid/3.1.23)

Connection: keep-alive

3128端口是可以改变的:只需要修改配置文件中的http_port 3128即可。

此时的代理是任何人都可以访问任何网站,并没有对访问进行控制,我们需要编辑配置文件进行访问限制,比如只允许员工访问.aminglinux.com和.baidu.com:

编辑配置文件:

[[email protected] ~]# vim /etc/squid/squid.conf

添加如下白名单good_domain配置(也可以设置黑名单bad_domain):

acl http proto HTTP

acl good_domain dstdomain .aminglinux.com .baidu.com

http_access allow good_domain

http_access deny !good_domain

检查配置是否有错:

[[email protected] ~]# squid -kcheck (或者squid -kch)

重新加载配置文件:

[[email protected] ~]# squid -kreconfig (或者squid -kre)

使用curl测试:

[[email protected] ~]# curl -x127.0.0.1:3128 www.baidu.com -I

HTTP/1.0 200 OK

Server: bfe/1.0.8.18

Date: Fri, 10 Mar 2017 13:43:57 GMT

Content-Type: text/html

Content-Length: 277

Last-Modified: Mon, 13 Jun 2016 02:50:06 GMT

ETag: "575e1f5e-115"

Cache-Control: private, no-cache, no-store, proxy-revalidate, no-transform

Pragma: no-cache

Accept-Ranges: bytes

X-Cache: MISS from test.com

X-Cache-Lookup: MISS from test.com:3128

Via: 1.0 test.com (squid/3.1.23)

Connection: keep-alive

[[email protected] ~]# curl -x127.0.0.1:3128 www.aminglinux.com -I

HTTP/1.0 301 Moved Permanently

Server: nginx

Date: Fri, 10 Mar 2017 13:45:01 GMT

Content-Type: text/html

Content-Length: 178

Location: http://www.apelearn.com/

X-Cache: MISS from test.com

X-Cache-Lookup: MISS from test.com:3128

Via: 1.0 test.com (squid/3.1.23)

Connection: keep-alive

[[email protected] ~]# curl -x127.0.0.1:3128 www.qq.com -I

HTTP/1.0 403 Forbidden

Server: squid/3.1.23

Mime-Version: 1.0

Date: Fri, 10 Mar 2017 21:45:32 GMT

Content-Type: text/html

Content-Length: 3241

X-Squid-Error: ERR_ACCESS_DENIED 0

Vary: Accept-Language

Content-Language: en

X-Cache: MISS from test.com

X-Cache-Lookup: NONE from test.com:3128

Via: 1.0 test.com (squid/3.1.23)

Connection: keep-alive