CentOS下搭建Squid代理服務器

“量子擾動超導探測器。戰爭期間用它搜索潛艇,尋找敵人的賽伯武器系統.”

“哦?海軍的玩意兒?打仗的時候用過?這麼說,烏賊能讀出你大腦芯片上儲存的東西?”她停住腳步。我覺得她藏在那兩片鏡面後面的一雙眼睛正死死盯着我。

“要說探測磁場,哪怕最低級的烏賊都比過去的磁力探測器強十億倍,就跟在體育場的一片歡呼聲中聽清誰說的一句悄悄話似的。

‍‍“squid”一般是指“量子擾動超導探測器(當然今天要講的是開源系統中的代理服務器啦)”‍‍

‍Squid代理服務器

Squid

Squid cache(簡稱爲Squid)是一個流行的自由軟件(GNU通用公共許可證)的代理服務器Web緩存服務器。Squid有廣泛的用途,從作爲網頁服務器的前置cache服務器緩存相關請求來提高Web服務器的速度,到爲一組人共享網絡資源而緩存萬維網,域名系統和其他網絡搜索,到通過過濾流量幫助網絡安全,到局域網通過代理上網。Squid主要設計用於在Linux一類系統運行。

對於Web用戶來說,Squid是一個高性能的代理緩存服務器,可以加快內部網瀏覽Internet的速度,提高客戶機的訪問命中率。Squid不僅支持HTTP協議,還支持FTP、gopher、SSL和WAIS等協議。和一般的代理緩存軟件不同,Squid用一個單獨的、非模塊化的、I/O驅動的進程來處理所有的客戶端請求。

Squid的另一個優越性在於它使用訪問控制清單(ACL)訪問權限清單(ARL)。訪問控制清單和訪問權限清單通過阻止特定的網絡連接來減少潛在的Internet非法連接,可以使用這些清單來確保內部網的主機無法訪問有威脅的或不適宜的站點。

Squid對硬件的要求是內存一定要大,不應小於128M,硬盤轉速越快越好,最好使用服務器專用SCSI硬盤,處理器要求不高,400MH以上既可。

通過模擬配置代理服務器,實現以下目標:

1.代理服務器可以將遠程的Web服務器頁面緩存在本地

2.代理服務器端口設置爲80端口

3.用戶通過訪問代理服務器即可獲得遠程Web服務器上的頁面內容

4.遠程Web服務器對客戶端用戶是透明的

5.利用緩存機制提高網站的響應速度

‍如圖,分別在web服務器、代理服務器和客戶端進行配置:‍

1

‍web服務器配置‍

1)裝包

# yum -y install httpd

#systemctl start httpd

#systemctl enable httpd

2)爲Web訪問建立測試文件

#cat /var/www/html/index.html

2

‍部署Squid代理服務器‍

1)裝包:

# yum -y install squid

2)修改配置文件:

# vim /etc/squid/squid.conf.. ..http_port 80 vhost

#設置反向代理visible_hostname www.wuke.com #設置主機名,默認沒有該語句

cache_peer 192.168.2.100 parent 80 0 originserver #定義後端真實服務器信息

cache_dir ufs /var/spool/squid 200 16 256 #硬盤緩存,緩存容量爲200M,自動創建16個一級子目錄和256個二級子目錄

http_access allow all #允許本機所有主機使用代理服務器

3)啓服務

# systemctl start squid

#systemctl enable squid

4)squid服務通過TCP 80端口監聽客戶端請求:

# netstat -anptu | grep 80

tcp 0 0 :::80 :::* LISTEN 3213/(squid)

3

‍客戶端測試‍

客戶端開啓瀏覽器訪問

# curl http://192.168.4.5 #返回的是192.168.2.100服務的頁面

 

搭建服務器:

1、yum 安裝 squid 和httpd-tools  (-y 全自動安裝)

#更新yum
yum update
 
yum install squid -y
yum install httpd-tools -y
2.生成密碼文件

mkdir /etc/squid3/
#username 是用戶名
htpasswd -cd /etc/squid3/passwords username
#提示輸入密碼,輸入即可 注意密碼不要超過 8 位
 
#測試密碼文件
/usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords
# 輸入 用戶名 密碼
username password
# 提示 OK 說明成功,ERR 是有問題,請檢查一下之前步驟
OK
# 測試完成,crtl + c 打斷
 

3.配置squid.conf文件

vi /etc/squid/squid.conf
配置文件加入:

#ncsa_auth的路徑要找到自己安裝的地方。一般在/usr/lib/squid/或者/usr/lib/squid3/視版本而定。
 
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords
acl ncsa_users  proxy_auth REQUIRED
http_access allow ncsa_users
 
#把http_access allow localnet , http_access allos localhost屏蔽掉。
 
# 設置來源IP白名單,增加的這兩行要在 http_access deny all 前面,因爲squid配置文件是從上往下讀的
 
acl localnet src 222.93.8.140
http_access allow client
# And finally deny all other access to this proxy
http_access deny all
4.修改完配置文件後啓動

# 先初始化一下
squid -z
# 啓動
systemctl start squid.service
# 停止
systemctl stop squid.service
# 重啓
#配置開機自啓動
systemctl enable squid.service
附錄:  /etc/squid/squid.conf完整代碼

#
# 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
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
 
auth_param basic program /usr/lib64/squid/basic_ncsa_auth /etc/squid3/passwords
acl ncsa_users  proxy_auth REQUIRED
http_access allow ncsa_users
 
acl localnet src 222.93.8.140
# And finally deny all other access to this proxy
http_access deny all
 
# Squid normally listens to port 3128
lhttp_port 3888
 
# Uncomment and adjust the following to add a disk cache directory.
#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
 

參考其他文檔:https://blog.csdn.net/wlwlomo/article/details/89679004

https://blog.csdn.net/Qwertyuiop2016/article/details/90183014

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章