小型企業的通常互聯網寬帶接入比較差的。公共上網帶寬也不很充足的,再加上有一些下載或都視頻流,對於web瀏覽將有嚴重的影響的。可能聽過各類網絡慢的聲音。html
在以上的基本目標下,準備使用squid作透明代理,這樣在用戶不用改變任何的配置狀況下,將有必定的網絡瀏覽速度的提高的。nginx
基本系統:web
Centos 6.7 內核:2.6.32.547vim
雙網卡bash
iptables 1.4.7服務器
squid 3.5.15網絡
開始安裝配置app
yum install -y gcc make
軟件存放路徑:/usr/local/src/dom
安裝路徑:/usr/local/squid/
tcp
cd /usr/local/src/ wget tar -zxvf squid-3.5.15.tar.gz cd squid-3.5.15 ./configure --prefix=/usr/local/squid / --enable-arp-acl / --enable-ssl / --with-aufs-threads=64 / --enable-ltdl-convenience #參數說明請參考 ./configure --help make && make install 基本安裝完成,未有錯誤。如遇到錯誤請自行找度媽與谷爸吧!
以上安裝完成之後的,軟件安裝位置:/usr/local/squid 下,目錄結構以下:
[root@nginx-master squid]# ll
total 24 drwxr-xr-x. 2 root root 4096 Feb 26 16:49 bin drwxr-xr-x. 2 root root 4096 Mar 29 15:25 etc #配置文件 drwxr-xr-x. 2 root root 4096 Feb 26 16:49 libexec drwxr-xr-x. 2 root root 4096 Feb 26 16:49 sbin #squid 執行程序 drwxr-xr-x. 5 root root 4096 Feb 26 16:49 share #文檔相關 drwxrwxrwx. 5 squid squid 4096 Feb 26 16:49 var 目錄cache與logs
編輯配置文件
cd /etc vim squid.conf # # Recommended minimum configuration: # #squid run user cache_effective_user squid # 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.8.0/24 # 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 acl game dstdomain games.sina.com.cn acl sport url_regex -i ccc.x.jd.com #------ 47jp acl jp src 192.168.47.188 acl jp src 192.168.47.187 acl jp src 192.168.47.32 acl jp src 192.168.47.153 acl jp src 192.168.47.148 acl jp src 192.168.47.147 acl jp src 192.168.47.105 acl jp src 192.168.47.130 acl jp src 192.168.47.230 acl jp src 192.168.47.103 acl jp src 192.168.47.150 acl jp src 192.168.47.127 acl jp src 192.168.47.123 acl jp src 192.168.47.158 #------- 4-5man acl man src 192.168.4.97 acl man src 192.168.4.99 acl man src 192.168.4.96 acl man src 192.168.4.101 acl man src 192.168.5.2 acl man src 192.168.5.3 acl man src 192.168.5.8 acl man src 192.168.5.20 acl man src 192.168.4.13 # # Recommended minimum Access Permission configuration: # # Deny requests to certain unsafe ports http_access deny !Safe_ports http_access deny game sport # Deny CONNECT to other than secure SSL ports http_access deny CONNECT !SSL_ports # Only allow cachemgr access from localhost http_access allow man 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 # And finally deny all other access to this proxy http_access deny all # Squid normally listens to port 3128 http_port 192.168.10.2:3128 transparent #默認配置爲:http_port 3128 此時爲代理模式 http_port 3129 #系統日誌有報錯:增長此參數 # Hostname visible_hostname ancc # 不加此參數時,系統啓動時會有警告 # Uncomment and adjust the following to add a disk cache directory. #cache_dir ufs /usr/local/squid/var/cache/squid 100 16 256 cache_dir ufs /home/data/cache 4096 16 256 #修改存儲目錄之後,要給目錄增長相應權限 cache_dir ufs /home/data/cache0 4096 16 256 cache_dir ufs /home/data/cache1 4096 16 256 cache_dir ufs /home/data/cache2 4096 16 256 access_log /home/data/log/access.log cache_store_log /home/data/log/store.log cache_log /home/data/log/cache.log cache_effective_user squid cache_effective_group squid # Leave coredumps in the first cache dir coredump_dir /home/data/cache #coredump_dir /usr/local/squid/var/cache/squid #manager mail cache_mgr xiexiangrong@ancc.com cache_mem 800 MB #20160322 #cache_swap_high 100% #cache_swap_low 80% maximum_object_size 1024 KB # # 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
以上是個人用的配置文件,詳細的配置參數可參考以下網址:http://zyan.cc/book/squid/index.html
上面的配置中:我修改了cache目錄與log的存儲位置的到/home/data目錄下,由於是測試了因此沒有詳細的規劃硬盤的使用空間。若是實施時,須要進一步的規劃磁盤空間。
配置文件完成,保存退出
cd /usr/local/squid/sbin/ ./squid -z #初始化cache目錄 ./squid #啓動程序 ps -axu | grep squid #查看服務是否正常啓動 netstat -nat | grep 3128 #查看端口是否被監聽
此配置爲透明代理,須要開啓路由轉發與iptables NAT和端口轉發
開啓轉發(永久的方法)
vim /etc/sysctl.conf # Controls IP packet forwarding net.ipv4.ip_forward = 1 # 0 修改成1 保存退出 #需重啓系統 [root@nginx-master sbin]# sysctl -p net.ipv4.ip_forward = 1 #修改生效 net.ipv4.conf.default.rp_filter = 1 net.ipv4.conf.default.accept_source_route = 0 kernel.sysrq = 0 kernel.core_uses_pid = 1
iptables配置
iptables -t nat -A PREROUTING -i eth0 -p tcp --dport 80 -j REDIRECT --to-ports 3128 iptables -t nat -A POSTROUTING -s 192.168.0.0/16 -j SNAT --to-source 192.168.8.10
iptables 根據你的實際IP來配置的。
實驗遇到的QA:
Q:配置時,把好多功能都配置完了,啓動時各類報錯。
A:實際配置當中的,一個功能一個功能實際並測試,這樣比較好查找故障的。
Q:squid啓動時,提示cache 與 log 目錄拒絕訪問
A:查看配置文件內的cache_effective_user squid 而後對目錄進行用戶與權限的配置
Q:配置完代理服務器後,固然想測試一下服務器的性能的。
A:請參考:http://www.oschina.net/question/12_6110 我使用的siege,其它沒有測試
測試結果以下:
squid
Transactions: 2000 hits
Availability: 100.00 %
Elapsed time: 9.30 secs
Data transferred: 6.89 MB
Response time: 0.04 secs
Transaction rate: 215.05 trans/sec
Throughput: 0.74 MB/sec
Concurrency: 7.70
Successful transactions: 0
Failed transactions: 0
Longest transaction: 0.54
Shortest transaction: 0.00
沒有SQUID
Lifting the server siege... done.
Transactions: 956 hits
Availability: 99.17 %
Elapsed time: 396.96 secs
Data transferred: 109.31 MB
Response time: 26.62 secs
Transaction rate: 2.41 trans/sec
Throughput: 0.28 MB/sec
Concurrency: 64.10
Successful transactions: 956
Failed transactions: 8
Longest transaction: 78.13
Shortest transaction: 2.62
有寫的錯誤的地方,喜歡你們批評指正的。
努力寫好每一篇文章,拒絕當轉發黨。嚴格測試每篇文章。