squid配置正反代理

squid配置php

1. 什麼是squidcss

squid能夠作代理也能夠作緩存html

squid緩存不只能夠節省寶貴的帶寬資源,也能夠大大下降服務器的I/O.  linux

squid不只能夠作正向代理,又能夠作反向代理。 web

正向代理,squid後面是客戶端,客戶端上網要經過Squid去上;反向代理,squid後面是服務器,服務器返回給用戶數據須要走squid. vim

正向代理用在企業的辦公環境中,員工上網須要經過squid代理來上網,這樣能夠節省網絡帶寬資源。而反向代理用來搭建網站靜態項(圖片、html、流媒體、js、css等)的緩存服務器,它用於網站架構中。 緩存


2. 搭建squid正向代理服務器

官方網站爲 http://www.squid-cache.org/  網絡

yum install -y squid架構

squid -v  查看版本以及編譯參數

> /etc/squid/squid.conf

vim /etc/squid/squid.conf

加入以下配置

http_port 3128

acl manager proto cache_object

acl localhost src 127.0.0.1/32 ::1

acl to_localhost dst 127.0.0.0/8 0.0.0.0/32 ::1

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 SSL_ports port 443

acl Safe_ports port 80 8080         # http

acl Safe_ports port 21          # ftp

acl Safe_ports port 443         # https

acl CONNECT method CONNECT

http_access allow manager localhost

http_access deny manager

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow localnet

http_access allow localhost

http_access allow all

cache_dir aufs /data/cache 1024 16 256

cache_mem 128 MB

hierarchy_stoplist cgi-bin ?

coredump_dir /var/spool/squid

refresh_pattern ^ftp:           1440    20%     10080

refresh_pattern ^gopher:        1440    0%      1440

refresh_pattern -i (/cgi-bin/|\?) 0     0%      0

refresh_pattern \.(jpg|png|gif|mp3|xml) 1440    50%     2880    ignore-reload

refresh_pattern .               0       20%     4320

### 到此結束,acl是訪問控制


mkdir  /data/cache  #建立緩存目錄

chown -R squid:squid /data/cache  #更改權限

squid -z  #初始化緩存目錄,會在緩存目錄下生成不少子目錄,該步驟能夠省略

/etc/init.d/squid start 

squid  -kcheck #能夠檢測配置文件是否有錯

squid -k rec #能夠從新加載配置

service squid restart #重啓squid服務


設置一下正向代理,Internet選項--連接--局域網設置--高級,輸入代理ip地址和端口

測試:

tcpdump -nn port 3128 and host 192.168.11.13

或者

curl -xlocalhost:3128 www.qq.com -I          -x指的是代理服務器

查看緩存文件 find /data/cache/ -type f


訪問圖片,測試緩存: curl -xlocalhost:3128   -I 'http://www.bier.com/bbs/static/p_w_picpath/common/logo.png'




設置白名單,或者說只想代理某幾個域名  vim /etc/squid/squid.conf

acl http proto HTTP 

acl good_domain dstdomain .lishiming.net .aminglinux.com 

http_access allow http good_domain 

http_access deny http !good_domain 


測 curl -xlocalhost:3128 www.google.com.hk -I  #403

測 curl -xlocalhost:3128 www.bier.com/bbs/forum.php  -I #200


vim /etc/squid/squid.conf  #設置域名黑名單

acl http proto HTTP 

acl bad_domain dstdomain .baidu.com

http_access deny http !bad_domain 


日誌 ls /var/log/squid/cache.log



3. 搭建squid反向代理

vim /etc/squid/squid.conf  #以下變動

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 

以前增長的域名白/黑名單相關配置去掉

若是是squid要代理一臺web上的全部域名,那麼就寫成這樣: cache_peer 192.168.10.111 80 0 originserver  #只須要這一行,cache_peer_domain  均可以省掉

/etc/init.d/squid restart 

測試 www.baidu.com www.qq.com  www.bier.net


curl -x127.0.0.1:80 www.qq.com -I

curl -x127.0.0.1:80 -I 'https://www.baidu.com/img/bd_logo1.png'

相關文章
相關標籤/搜索