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

squid反向代理

squid反向代理

squid反向代理

過程其實和前面的正向代理沒有什麽太大區別,唯一的區別是配置文件中一個地方需要改動一下。需要把:

http_port 3128

改為:

http_port 80 accel vhost vport

然後再增加你要代理的後端真實服務器信息:

cache_peer 123.125.119.147 parent 80 0 originserver name=a cache_peer 61.135.169.125 parent 80 0 originserver name=b cache_peer_domain a www.qq.com cache_peer_domain b www.baidu.com

因為咱們之前沒有配置網站信息,所以就拿qq.com和baidu.com來做個例子吧。其中cache_peer為配置後端的服務器ip以及端口,name後邊為要配置的域名,這裏和後面的cache_peer_domain相對應。實際的應用中,ip大多為內外ip,而域名也許會有多個,如果是squid要代理一臺web上的所有域名,那麽就寫成這樣:

cache_peer 192.168.10.111 80 0 originserver

後面連cache_peer_domain 也省了。

反向代理主要用於緩存靜態項,因為諸多靜態項目尤其是圖片、流媒體等比較耗費帶寬,在中國,聯通網訪問電信的資源本例就慢,如果再去訪問大流量的圖片、流媒體那更會慢了,所以如果在聯通網配置一個squid反向代理,讓聯通客戶端直接訪問這個聯通squid,而這些靜態項已經被緩存在了squid上,這樣就大大加快了訪問速度。也許你聽說過CDN, 其實它的設計原理就是這樣的思路。好了,我們再測一測反向代理吧。

因為修改了配置文件,所以需要重啟一下squid:

[[email protected] ~]# /etc/init.d/squid restart [[email protected] ~]# curl -xlocalhost:80 http://www.baidu.com/ [[email protected] ~]# curl -xlocalhost:80 http://www.qq.com/ [[email protected] ~]# curl -xlocalhost:80 http://www.sina.com/

你會發現,baidu.com和qq.com都能正常訪問,然而sina.com訪問503了,這是因為並沒有加sina.com的相關設置。

還有一個知識點,需要介紹給你:

[[email protected] ~]# squid -h Usage: squid [-cdhvzCFNRVYX] [-s | -l facility] [-f config-file] [-[au] port] [-k signal] -a port Specify HTTP port number (default: 3128). -d level Write debugging to stderr also. -f file Use given config-file instead of /etc/squid/squid.conf -h Print help message. -k reconfigure|rotate|shutdown|interrupt|kill|debug|check|parse Parse configuration file, then send signal to running copy (except -k parse) and exit. -s | -l facility Enable logging to syslog. -u port Specify ICP port number (default: 3130), disable with 0. -v Print version. -z Create swap directories -C Do not catch fatal signals. -D OBSOLETE. Scheduled for removal. -F Don‘t serve any requests until store is rebuilt. -N No daemon mode. -R Do not set REUSEADDR on port. -S Double-check swap during rebuild. -X Force full debugging. -Y Only return UDP_HIT or UDP_MISS_NOFETCH during fast reload.

上面把squid命令所用到的選項全部打印出來了,但最常用的除了 squid -k check 外,還有一個那就是 squid -k reconfigure 它們倆都可以簡寫:

[[email protected] ~]# squid -kche [[email protected] ~]# squid -krec

其中第二條命令表示重新加載配置文件,如果我們更改了配置文件後,不需要重啟squid服務,直接使用該命令重新加載配置即可。


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

squid反向代理