1. 程式人生 > >squid正向代理

squid正向代理

squid正向代理

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

技術分享

此時,訪問百度就可以看到抓到很多包,說明確實走的是我們的代理服務器。

同時在緩存目錄下產生了很多文件:

技術分享

使用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


本文出自 “12350027” 博客,謝絕轉載!

squid正向代理