導航:首頁 > IDC知識 > squid代理伺服器配置

squid代理伺服器配置

發布時間:2020-12-28 20:21:04

1、如何在linux上用squid搭建代理伺服器

1、首先下載:squid-3.2.9.tar.bz2
2、上傳到伺服器後解壓:
解壓: tar -vxjf squid-3.2.9.tar.bz2
解壓後生成目錄:squid-3.2.9
進入目錄翻看文檔INSTALL:
xxxx> more INSTALL
To build and install the Squid Cache, type:
% ./configure --prefix=/usr/local/squid
% make all
% make install
To run a Cache, you will need to:
1. customize the squid.conf configuration file:
% vi /usr/local/squid/etc/squid.conf
2. Initalise the cache:
% /usr/local/squid/sbin/squid -z
3. start the cache:
% /usr/local/squid/sbin/squid
If you want to use the WWW interface to the Cache Manager, copy
the cachemgr.cgi program into your httpd server's cgi-bin
directory.
3、安裝步驟:
./configure --prefix=/usr/local/squid
make all
sudo make install(因為要拷貝到系統目錄,需要root許可權,所以sudo了,你也可以root登錄執行,我是Ubuntu的系統,所以用sudo,有root許可權就行)
檢查配置文件:
sudo vi /usr/local/squid/etc/squid.conf
配置項1:
# Squid normally listens to port 3128
http_port 3128
配置項2:
acl localnet src 192.168.0.0/16
http_access allow localnet
配置項3:
# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 128
cache_mem 32 MB (這一條必須配置)
否則就會遭遇報錯: 2013/10/12 16:16:55 kid1| WARNING cache_mem is larger than total disk cache space!
安裝好了以後,系統中新建了一個用戶squid,在組中一查,發現屬於nobody組的:
cat /etc/passwd|grep squid
cat /etc/group|grep 65534
安裝squid的所在目錄是:/usr/local/squid
直接改了所屬用戶為squid:nobody
sudo chown -Rf squid:nobody /usr/local/squid
建立cache的時候,對下面目錄需要nobody用戶許可權:
sudo chown -Rf nobody /usr/local/squid/var/cache/
sudo chown -Rf nobody /usr/local/squid/var/logs/
否則會遭遇:
WARNING: Cannot write log file: /usr/local/squid/var/logs/cache.log
FATAL: Failed to make swap directory /usr/local/squid/var/cache/squid/00: (13) Permission denied
初始化squid.conf里配置的cache目錄,就是建立了一堆的目錄:
sudo /usr/local/squid/sbin/squid -z
在前台啟動squid,並輸出啟動過程
sudo /usr/local/squid/sbin/squid -N -d1
顯示ready to server reques,則啟動成功。可以鍵入ctrl+c,停止squid,並以後台運行的方式啟動。
沒有在配置文件中配置DNS,而是在 /etc/resolv.conf 中配置:
domain site
nameserver x.x.x.x
所以列印出來的日誌中就這樣的:
2013/10/12 16:42:13| Adding nameserver x.x.x.x from /etc/resolv.conf
squid從這個配置文件中讀取了dns配置來用。
啟動squid後台運行
sudo /usr/local/squid/sbin/squid -s
檢查一下進程是否存在:ps -ef|grep squid
通過squid客戶端查看squid運行狀態
/usr/local/squid/bin/squidclient -h 127.0.0.1 -p 3128 mgr:info
那台不能上網的機器配置如下:
export http_proxy=http://192.168.199.235:3128/
可以把這句寫到啟動文件中,比如什麼.profile或者.bashrc,或者/etc/profile等等。
取消:unset http_proxy
最後測試一下能不能上網了。

2、如何在linux上用squid搭建代理伺服器

安裝環境
操作系統: CentOS release 5.2 (Final)
Kernel: 2.6.18-92.el5PAE
軟體列表
squid-2.6.STABLE22.tar.gz

軟體存放位置
/data/software
安裝過程
#/usr/sbin/groupadd squid -g 610
#/usr/sbin/useradd -u 610 -g squid squid
#mkdir /srv/scache
#chmod +w /var/vcache
#chown -R squid.squid /srv/scache
#mkdir /var/log/squid
#chmod +w /var/log/squid
#chown -R squid.squid /var/log/squid
#cd /data/software/pkg
#tar zxvf ../squid-2.6.STABLE22.tar.gz
#cd squid-2.6.STABLE22
#./configure --prefix=/usr/local/squid
#make && make install
編輯Squid配置文件
#vi /usr/local/squid/etc/squid.conf
-------------------------華麗的分割線,以下都是配置文件內容----------------------------
#定義acl項名稱
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl to_localhost dst 127.0.0.0/8
acl SSL_ports port 443
acl Safe_ports port 80
acl safeprotocol protocol HTTP
acl test dstdomain .test.com
acl CONNECT method CONNECT
#定義acl規則
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access deny to_localhost
http_access allow safeprotocol Safe_ports test
http_access deny all
icp_access allow all
#squid監聽埠
http_port 80 accel defaultsite=59.151.32.58 vhost
always_direct allow all
#後端伺服器
cache_peer 10.10.10.8 parent 80 0 no-query originserver
hierarchy_stoplist cgi-bin ?
#內存cache大小
cache_mem 2048 MB
#內存cache中最大的object大小(超過這個值則不進入內存cache)
maximum_object_size_in_memory 8 KB
#內存cache的替換規則
memory_replacement_policy lru
#硬碟cache的替換規則
cache_replacement_policy lru
#磁碟cache目錄(文件類型 cache目錄路徑 cache目錄大小 二級目錄個數 每個二級目錄下的三級目錄個數)
cache_dir ufs /srv/scache 40000 16 256
#磁碟cache中最小的object的大小(低於這個值則不緩存)
minimum_object_size 0 KB
#磁碟cache中最大的object的大小(超過這個值則不緩存)
maximum_object_size 2048 KB
cache_swap_low 90
cache_swap_high 95
#定義日誌格式
logformat combined %>a %ui %un [%tl] "%rm %ru HTTP/%rv" %Hs %h" "%{User-Agent}>h" %Ss:%Sh
#記錄相關日誌
access_log /var/log/squid/access.log squid
cache_log /var/log/squid/cache.log
cache_store_log /var/log/squid/store.log
#日誌rotate(24則後綴從.0到.23)
logfile_rotate 24
emulate_httpd_log on
#如果你的URL裡面帶有?,這兩行一定要注銷掉
#acl QUERY urlpath_regex cgi-bin \?
#cache deny QUERY
#配置自動更新(關於後面的三個值請參考Squid配置手冊)
refresh_pattern -i \.jpg$ 60 80% 1440
refresh_pattern -i \.png$ 60 80% 1440
refresh_pattern -i \.gif$ 60 80% 1440
quick_abort_min 16 KB
quick_abort_max 16 KB
quick_abort_pct 95
request_header_max_size 10 KB
reply_header_max_size 10 KB
acl apache rep_header Server ^Apache
broken_vary_encoding allow apache
#相關timeout設置
forward_timeout 4 minutes
connect_timeout 3 minutes
peer_connect_timeout 30 seconds
read_timeout 15 minutes
request_timeout 1 minute
persistent_request_timeout 3 minutes
half_closed_clients off
pconn_timeout 1 minute
#cache管理員郵箱
cache_mgr [email protected]
cache_effective_user squid
cache_effective_group squid
#squid伺服器的visible_hostname,此處指定的值顯示在Squid響應的Header裡面的X-Cache項中
visible_hostname squidserver
coremp_dir /var/log/squid/cache
------------------------華麗的分割線,以上都是配置文件內容-----------------------------
初始化squid緩存目錄
#/usr/local/squid/sbin/squid -z
啟動squid
#/usr/local/squid/sbin/squid -sD
配置完成以後,最重要最重要的一點,修改Squid伺服器的hosts文件,將需要Cache的域名指向到後端的伺服器IP上
相關命令
停止squid
/usr/local/squid/sbin/squid -k shutdown
啟用新配置
/usr/local/squid/sbin/squid -k reconfig
通過crontab每小時截斷/輪循日誌
59 * * * * /usr/local/squid/sbin/squid -k rotate
查看squid運行狀況
/usr/local/squid/bin/squidclient -p 80 mgr:info
/usr/local/squid/bin/squidclient -p 80 mgr:5min
查看squid內存使用情況
/usr/local/squid/bin/squidclient -p 80 mgr:mem
查看squid磁碟使用情況
/usr/local/squid/bin/squidclient -p 80 mgr:diskd
查看squid已緩存列表(小心使用,可能會導致crash)
/usr/local/squid/bin/squidclient -p 80 mgrbjects
強制更新某個url
/usr/local/squid/bin/squidclient -p 80 -m PURGE http://img.test.com/h/a.jpg
查看squid緩存到內存cache中並返回給訪問用戶的項
#cat /var/log/squid/access.log | grep TCP_MEM_HIT
查看squid緩存到磁碟cache中並返回給訪問用戶的項
#cat /usr/local/squid/var/logs/access.log | grep TCP_HIT
查看沒被squid緩存住,直接從原始伺服器獲取並返回給訪問用戶的項
#cat /usr/local/squid/var/logs/access.log | grep TCP_MISS

3、如何用Squid Windows版架設二級代理伺服器

一、Windows版Squid的下載與安裝
下載windwosNT版本的squid
1、把squid-2.6.STABLE13-bin.zip解壓縮,把裡面的squid文件夾拷到c:/下(squid默認的是c:/squid)
2、squid/etc目錄下把
squid.conf.default拷貝一份重新命名為squid.conf
cachemgr.conf.default拷貝一份重新命名為cachemgr.conf
mime.conf.default拷貝一份重新命名為mime.conf
3.用文本編輯器打開squid.conf,需要修改的地方:
找到http_port 3128在後面增加一行
http_port 80 transparent
找到#cache_peer sib2.foo.NET sibling 3128 3130 [proxy-only]在後面增加一行
cache_peer 192.168.1.8 parent 7001 0 no-query originserver
找到# TAG: visible_hostname在後面增加一行
visible_hostname volcano(任意命名)
找到http_access deny all在其前面加#將這一行注釋掉,然後增加一行
http_access allow all
4.從命令行到c:/squid/sbin目錄下執行
squid -i(將squid服務加入到服務裡面)
squid -z
安裝完成
5.從服務里啟動squid
訪問squid伺服器:
http://192.168.1.2(你的squid伺服器IP地址)>>>指向http://192.168.1.8:7001(web伺服器地址)
如果把#http_access deny all打開把http_access allow all注釋掉,你的訪問就會被拒絕
你需要配置一下:找到下面兩行
#acl our_networks src 192.168.1.0/24 192.168.2.0/24
#http_access allow our_networks
打開注釋,修改你的內網ip(段)可以設為192.168.1.0/24一個也可以如上面的一樣設一段IP
二、squid.conf配置文件
cache_mgr [email protected] #設置管理員郵箱,無關緊要
visible_hostname ibi #設置虛擬主機名,似乎squid2.5這個版本需要
#設置這一項,2.4卻不需要
cache_peer 10.10.2.53 parent 6666 3130 login=account:passwd default no-query
#設置上級代理,其中10.10.2.53是我們校內的代理地址,6666是他的埠號,
#account,passwd則是上網帳號密碼(當然我不會把我們真實的帳號貼出來)
#hierarchy_stoplist cgi-bin ? #注釋掉這一行,不然不能訪問帶有"?"
#的url
#acl QUERY urlpath_regex cgi-bin ? #這兩行沒有具體測試,應該和cgi請求有關
#no_cache deny QUERY
acl all src 0.0.0.0/0.0.0.0
acl manager proto cache_object
acl localhost src 127.0.0.1/255.255.255.255
acl SSL_ports port 443 563
acl Safe_ports port 80 # http
acl Safe_ports port 21 # ftp
acl Safe_ports port 443 563 # https, snews
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
acl lan-a src 10.49.41.150-10.49.41.190/32 #對ip進行控制,這行定義了一個ip
#段為組lan-a
http_access allow lan-a #這里控制組lan-a的ip可以使用squid代理
acl lan-b src 10.141.96.0/24 #同樣設置了一個ip段,ip地址前三位是
#10.141.96的所有ip,其實就是我們寢室樓的ip段
http_access allow lan-b
http_access allow manager localhost
http_access deny manager
http_access deny !Safe_ports
http_access deny CONNECT !SSL_ports
http_access allow localhost
http_access deny all
icp_access allow all
never_direct allow all #這一行解決無法登陸的問題。

4、簡述Linux Squid代理伺服器的主要作用

squid is a high-performance proxy caching server for web clients, supporting FTP, gopher, and HTTP data objects. Unlike traditional caching software, squid handles all requests in a single, non-blocking, I/O-driven process.

squid keeps meta data and especially hot objects cached in RAM, caches DNS lookups, supports non-blocking DNS lookups, and implements negative caching of failed requests.

squid supports SSL, extensive access controls, and full request logging. By using the lightweight Internet Cache Protocol, squid caches can be arranged in a hierarchy or mesh for additional bandwidth savings.

squid consists of a main server program squid, a Domain Name System lookup program dnsserver, some optional programs for rewriting requests and performing authentication, and some management and client tools. When squid starts up, it spawns a configurable number of dnsserver processes, each of which can perform a single, blocking Domain Name System (DNS) lookup. This reces the amount of time the cache waits for DNS lookups.

5、如何在Linux上用Squid搭建代理伺服器

squid是所有服務裡面最簡單的我覺得
以RHEL7為例,它分成了正向代理和反向代理,正向代理里又分「標准正向代理」,「ACL訪問控制」以及「透明正向代理」。下面是標准正向代理

16.3 正向代理

16.3.1 標准正向代理

Squid服務程序軟體包在正確安裝並啟動後默認就已經可以為用戶提供標准正向代理模式服務了,而不需要單獨再去修改配置文件或者其他操作,咱們可以立即在Windows7系統的客戶端主機上面打開任意一款瀏覽器,然後點擊Internet選項標簽,如圖16-4所示:
[root@linuxprobe ~]# systemctl restart squid
[root@linuxprobe ~]# systemctl enable squid
ln -s '/usr/lib/systemd/system/squid.service' '/etc/systemd/system/multi-user.target.wants/squid.service'
用戶要想使用Squid服務程序提供的標准正向代理模式服務就必須在瀏覽器中填寫伺服器的IP地址以及埠號信息,因此咱們還需要依次點擊連接標簽後點擊區域網設置選項,如圖16-5與圖16-6所示填寫伺服器信息後保存退出配置向導。

用戶只需要在瀏覽器中簡單的填寫配置信息就可以開始享用Squid服務程序提供的代理服務了,此時作為一個網卡為僅主機模式(Hostonly)的虛擬機,開始也奇跡般的能夠上網瀏覽了,這一切都是托代理伺服器轉發的功勞哦~

如此公開而沒有密碼驗證的代理服務終歸覺得不放心,萬一有其他人也來「蹭網」咱們的代理服務怎麼辦呢?Squid服務程序默認的會佔用3128、3401與4827等埠號,咱們可以將默認佔用的埠號修改成其他值,這樣應該能起到一定的保護作用吧~同學們都知道在Linux系統配置服務程序就是在修改該服務的配置文件,因此直接在/etc目錄中找到和squid服務程序同名目錄中的配置文件,把其中http_port參數後面原有3128修改為10000,這樣即是將Squid服務程序的代理服務埠修改成了新值,當然最後不要忘記再重啟下服務程序哦~:
[root@linuxprobe ~]# vim /etc/squid/squid.conf
………………省略部分輸出信息………………
45 #
46 # INSERT YOUR OWN RULE(S) HERE TO ALLOW ACCESS FROM YOUR CLIENTS
47 #
48
49 # Example rule allowing access from your local networks.
50 # Adapt localnet in the ACL section to list your (internal) IP networks
51 # from where browsing should be allowed
52 http_access allow localnet
53 http_access allow localhost
54
55 # And finally deny all other access to this proxy
56 http_access deny all
57
58 # Squid normally listens to port 3128
59 http_port 10000
60
http_port 10000
………………省略部分輸出信息………………
[root@linuxprobe ~]# systemctl restart squid
[root@linuxprobe ~]# systemctl enable squid
ln -s '/usr/lib/systemd/system/squid.service' '/etc/systemd/system/multi-user.target.wants/squid.service'

同學們有沒有突然覺得這一幕似曾相識?在前面的第十章10.5.3小節咱們學習過基於埠號來部署httpd服務程序的虛擬主機功能,當時在編輯完配置文件後重啟服務程序時被直接提示報錯了,雖然現在重啟服務程序並沒有直接報錯,但其實客戶並不能使用代理服務呢,SElinux安全子系統認為Squid服務程序使用3128埠號是理所應當的,默認策略規則中也是允許的,但現在卻在嘗試使用新的10000埠號,這是原本並不屬於Squid服務程序應該使用的系統資源,因此咱們需要手動把新的埠號添加到squid服務程序在SElinux域的允許列表中即可:
[root@linuxprobe ~]# semanage port -l | grep -w -i squid_port_t
squid_port_t tcp 3128, 3401, 4827
squid_port_t udp 3401, 4827
[root@linuxprobe ~]# semanage port -a -t squid_port_t -p tcp 10000
[root@linuxprobe ~]# semanage port -l | grep -w -i squid_port_t
squid_port_t tcp 10000, 3128, 3401, 4827
squid_port_t udp 3401, 4827

更多的圖文信息以及其他的代理方式你可以看下http://www.linuxprobe.com/chapter-16.html#161這篇,講的非常詳細,相信能解決你的問題

6、如何在linux上用squid搭建代理伺服器

安裝步驟:
./configure --prefix=/usr/local/squid
make all
sudo make install(要拷貝系統目錄需要root許可權所sudoroot登錄執行我Ubuntu系統所用sudoroot許可權行)
檢查配置文件:
sudo vi /usr/local/squid/etc/squid.conf
配置項1:
# Squid normally listens to port 3128
http_port 3128
配置項2:
acl localnet src 192.168.0.0/16
http_access allow localnet
配置項3:
# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 128
cache_mem 32 MB (條必須配置)
否則遭遇報錯: 2013/10/12 16:16:55 kid1| WARNING cache_mem is larger than total disk cache space!
安裝系統新建用戶squid組查發現屬於nobody組:
cat /etc/passwd|grep s

7、不能通過squid代理伺服器運行JAVA程序

從你配置文件看你配的是正常
我的意見是你應該配成反向代理.
你可以網路一下squid 反向代理參考一下.

8、linux下用squid實現代理伺服器的問題。

完全可以
在伺服器上設置FQDN,squid要求使用FQDN。例如server.linux.com,使用hostname命令即可:
hostname server.linux.com
在/etc/hosts文件中寫入上述FQDN的解析條目:
192.168.2.161 server.linux.com
然後運行命令squid -z完成初始化
修改/etc/squid/squid.conf配置文件。該文件絕大部分的配置都不需要修改,只要加入你的訪問控制即可。

可以加入兩行:
acl mynet src 192.168.2.0/255.255.255.0
http_access allow mynet

最後客戶端在瀏覽器上設置代理地址就可以了

9、如何在Linux上用Squid搭建代理伺服器

安裝步驟:
./configure --prefix=/usr/local/squid
make all
sudo make install(因為要拷貝到系統目錄,需要root許可權,所以了,你也可以root登錄執行,我是Ubuntu的系統,所以用sudo,有root許可權就行)
檢查配置文件:
sudo vi /usr/local/squid/etc/squid.conf
配置項1:
# Squid normally listens to port 3128
http_port 3128
配置項2:
acl localnet src 192.168.0.0/16
http_access allow localnet
配置項3:
# Uncomment and adjust the following to add a disk cache directory.
cache_dir ufs /usr/local/squid/var/cache/squid 100 16 128
cache_mem 32 MB (這一條必須配置)
否則你就會遭遇報錯: 2013/10/12 16:16:55 kid1| WARNING cache_mem is larger than total disk cache space!
安裝好了以後,系統中新建了一個用戶squid,在組中一查,發現屬於nobody組的:
cat /etc/passwd|grep squid
cat /etc/group|grep 65534
安裝squid的所在目錄是:/usr/local/squid
我閑得沒事干,直接改了所屬用戶為squid:nobody
sudo chown -Rf squid:nobody /usr/local/squid
建立cache的時候,對下面目錄需要nobody用戶許可權,這個是網上沒有說的很清楚的地方,折騰了我半天:
sudo chown -Rf nobody /usr/local/squid/var/cache/
sudo chown -Rf nobody /usr/local/squid/var/logs/

與squid代理伺服器配置相關的知識