本实例是squid和apache在同一台机器上,squid做前端反向代理。port为80,apache作为后端web,port为81

serverip:172.16.8.102

1.首先介绍下版本号选择,在进行測试之前一定要选定一个合适的squid版本号,在此推荐2.7。她和2.6功能相似但更好的支持http1.1,也有3.0以上版本号的不少特性。

2.squid2.7安装

cd /usr/local/src

tar -zxvf squid-2.7.STABLE9.tar.gz

cd squid-2.7.STABLE9

./configure -prefix=/usr/local/squid2.7 -enable-xmalloc-statistics --enable-async-io=320 --with-maxfd=65536 -enable-useragent-log -enable-referer-log -enable-epoll -disable-poll -enable-large-cache-files -disable-internal-dns -enable-linux-netfilter -enable-truncate
-enable-x-accelerator-vary -enable-follow-x-forwarded-for -with-large-files -with-pthreads -enable-storeio="aufs,coss,diskd,ufs" -enable-kill-parent-hack -enable-gnuregex -enable-cache-digests -enable-delay-pools -enable-stacktraces -enable-default-err-language=Simplify_Chinese
-enable-err-languages="Simplify_Chinese English" --enable-auth="basic" --enable-basic-auth-helpers="NCSA" --enable-snmp

make && make install

3.创建suqid用户

useradd squid

chown -R squid.squid squid

设置suqid安装文件文件夹的属性,否则squid可能会无法启动

4.创建缓文件夹

cd /data

mkdir -p squid/cache

chown -R squid.squid squid

5.创建日志文件夹

cd /var/log

mkdir squid

chown -R squid.squid squid

设置suqid日志的属性,否则squid可能会无法启动

5.配置squid.conf

cd /usr/local/squid2.7

vim squid.conf

acl all src all

acl manager proto cache_object

acl localhost src 127.0.0.1/32

acl to_localhost dst 127.0.0.0/8 0.0.0.0/32

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 # http

acl Safe_ports port 81
# http

acl Safe_ports port 3128 # http

acl Safe_ports port 8080 # 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

http_access allow manager localhost localnet

http_access deny !Safe_ports

http_access deny CONNECT !SSL_ports

http_access allow all

icp_access allow localnet

icp_access deny all

http_port 80 accel vhost vport

cache_peer 127.0.0.1 parent 81 0 no-query originserver name=test

cache_peer_access test allow all

hierarchy_stoplist cgi-bin ?

cache_mem 1024 MB

maximum_object_size_in_memory 6 MB

memory_replacement_policy lru

cache_replacement_policy lru

cache_dir ufs /data/squid/cache 1024 16 256

maximum_object_size 6 MB

cache_swap_low 90

cache_swap_high 95

access_log /var/log/squid/access.log

cache_log /var/log/squid/cache.log 

refresh_pattern ^ftp: 144020%10080

refresh_pattern ^gopher: 14400%1440

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

refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js|aspx) 1440    50%     2880    ignore-reload

refresh_pattern . 020%4320

acl shoutcast rep_header X-HTTP09-First-Line ^ICY.[0-9]

cache_vary on

acl apache rep_header Server ^Apache

broken_vary_encoding allow all

cache_effective_user squid

cache_effective_group squid

visible_hostname 172.16.8.102 

icp_port 0

reload_into_ims on

coredump_dir /usr/local/squid2.7/var/cache

所更改的參数解释:

(1)acl Safe_ports port 81 # http

     acl Safe_ports port 3128 # http

     acl Safe_ports port 8080 # http

此处定义能够訪问的端口,因为http_access deny !Safe_ports,仅仅要不是在Safe_ports中出现的端口都会被限制,这个能够依据实际情况而定.

(2)http_access allow all

在此我定义的是全部的ip都能够訪问squid,这也是为了方便我在測试环境中使用,假设是线上应用请制定对应的訪问限制。

(3)http_port 80 accel vhost vport

定义訪问squid的port。

假设不加accel vhost vport说明你的squid默认做为一个缓存服务器。这个时候假设client有请求发到了squid。squid起到的是路由功能,把请求转发出去。被真正的web server接收,web server返回响应。当squid接收到响应后,依据响应头决定是否缓存。此时的squid,仅仅是一个cache server。

假设加上accel vhost vport说明你的squidsquid就从一个缓存(cache server)变成了一个web server, 这个时候squid在80port监听请求。同一时候和web server的请求port(vhost vport)绑定。这个时候请求到了squid,squid是不用转发请求的。而是直接要么从缓存中拿数据要么向绑定的port直接请求数据。另外绑定port另一个优点,能够充分利用http 响应头中的到期时间头和etag头。

cache_peer 127.0.0.1 parent 81 0 no-query originserver name=test

反向代理81port。81port为apache;no-query不做查询,直接获取数据;orginserver 代表是源服务器;name定义反向代理的名字。能够对acl控制

(4)cache_mem 1024 MB

设置所用内存的大小

maximum_object_size_in_memory 6 MB

设置缓存对象所占用的最大内存

memory_replacement_policy lru

cache_replacement_policy lru

替换机制

cache_dir ufs /data/squid/cache 1024 16 256

缓存文件夹的大小。应该不低于cache_mem

maximum_object_size 6 MB

最大的单个缓存对象

(5)access_log /var/log/squid/access.log

cache_log /var/log/squid/cache.log

设置squid的日志文件夹。注意日志权限,否则有可能导致squid无法启动

(6)refresh_pattern \.(jpg|png|gif|mp3|xml|html|htm|css|js|aspx) 1440    50%     2880    ignore-reload

设置jpg等后缀格式的文件在cache中停留的时间

(7)cache_vary on

假设你发现squid缓存命中率非常低。即使调整refresh_pattern。maximum_object_size_in_memory,加大内存都没用。利用cachemgr.cgi统计工具中的In-Memory and In-Transit Objects,发现HTML/js/css not_in_memory,而jpg/png等图片都缓存了,则可能是有因为这个參数off导致。

这是由于apache在 response header 中返回了一个vary:Accept-encoding ,则squid在存储缓存文件时须要将“浏览器”request header 信息中的Accept-encoding字段的值(gzip。deflate之类)作为缓存key的一部分,因此对于不同的Accept-encoding字段值。都须要保存不同的文件。(IE与firefox的请求头的Accept-encoding字段值中就有一个空格的区别下次

请求到squid的时候,须要先找到一个缓存文件的索引文件,依据索引文件里的不同的Accep-encoding值再去找对应的缓存文件。 cache vary off,那么经过gzip压缩后含有vary头的。都不会被cache了,所以和上述缓存策略没什么影响,而jpg本来是被压缩过,不含vary,自然会被cache了。

(8)cache_effective_user squid

cache_effective_group squid

设置squid的用户和组

(9)icp_port 0

禁用icp邻居,假设你想使用squid集群能够更改这个參数

(10)reload_into_ims on

开启这个全局參数。可将client发来的no-cache转化为If-Modified-Since去处理

这个參数的设置,能够參考此博客http://blog.sina.com.cn/s/blog_56d8ea9001018xev.html

(11)hierarchy_stoplist cgi-bin ?

此为默认參数。不论什么包括问号或cgi-bin字符串的请求匹配该列表,变成不可层叠。

Squid内在的将每一个client请求标记为层叠或不可层叠。不可层叠的请求看起来不会导致cache命中。比如。POST请求的响应差点儿从不会被cache。在squid能简单的连接到原始server时,转发不可cache目标的请求到邻居cache。纯粹是浪费资源。

某些区分层叠和不可层叠请求的规则,在squid里难于编码。比如。POST和PUT方式总是不可层叠的。

然而,hierarchy_stoplist指令同意你定制这样的算法。它包括一个字符串列表。当在URI里发现它们时。squid将请求标记为不可层叠。

更改完配置文件后,能够进行初始化缓存文件夹和启动squid了

/usr/local/squid2.7/sbin/squid -z

/usr/local/squid2.7/sbin/squid

lsof -i:80

COMMAND  PID  USER   FD   TYPE  DEVICE SIZE/OFF NODE NAME

squid   1399 squid   17u  IPv4 9965038      0t0  TCP *:http (LISTEN)

说明启动成功,假设发现启动没成功,则检查配置文件

6.cachemgr.cgi统计工具

vim /usr/local/squid2.7/etc/cachemgr.conf

localhost:80

80port为squid的http_portport

cd /var/www/html

mkdir squid/cgi-bin

cp /usr/local/squid2.7/libexec/cachemgr.cgi /var/www/html/squid/cgi-bin

在apache中设置对应的訪问

vim /etc/httpd/conf.d/squid.conf

ScriptAlias /squid/cgi-bin/cachemgr.cgi /usr/local/squid2.7/libexec/cachemgr.cgi



# Only allow access from localhost by default

<Location /squid/cgi-bin/cachemgr.cgi>

 order allow,deny

# allow from localhost.localdomain

 allow from all

 # Add additional allowed hosts as needed

 # allow from .example.com

</Location>

service httpd restart使配置文件生效。

因为Apache使用的是81port,我们直接用81port訪问就可以

http://172.16.8.102:81/squid/cgi-bin/cachemgr.cgi

因为我们没有设置usernamepassword直接訪问就可以,可是应用到线上则必须设置。

7.apache配置

网站的訪问配置我在直接用的是我们一个測试网站,在这不做过多介绍。但在此要介绍下apache的mod_expoires模块。此模块能够降低10%左右的反复请求,让反复的用户对指定的页面请求结果都CACHE在本地,根本不向server发出请求。

检查apache按安装有mod_expires模块,因此我们仅仅须要在/etc/httpd/conf.d/mod_expires.conf中进行配置就可以。

vim /etc/httpd/conf.d/mod_expires.conf

<IfModule mod_expires.c>

ExpiresActive On

ExpiresDefault "access plus 12 hours"

ExpiresByType text/html "access plus 3 days"

ExpiresByType text/plain "access plus 3 days"

ExpiresByType text/css  "access plus 7 days"

ExpiresByType image/gif "access plus 30 days"

ExpiresByType image/png "access plus 30 days"

ExpiresByType image/jpeg "access plus 30 days"

ExpiresByType image/jpg "access plus 30 days"

ExpiresByType image/x-icon "access plus 30 days"

ExpiresByType video/x-flv  "access plus 30 days"

ExpiresByType application/x-shockwave-flash "access plus 30 days"

</IfModule>

当中对全部文件能够缓存的文件都默认设置为12小时,对text/image/video等类型的文件又一次设置成对应的缓存时间。

设置完毕后service httpd restart 就可以。

最后我们訪问測试。然后查看缓存命中了。

watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveWFuZ2dkMTk4Nw==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast" alt="">

另,在apache前端加squid后。我的负载可以达到4000。可是squid消耗的cpu也有点高啊。

[root@localhost webbench-1.5]# webbench -c 4000 -t 30 http://172.16.8.102/Login.php

Webbench - Simple Web Benchmark 1.5

Copyright (c) Radim Kolar 1997-2004, GPL Open Source Software.



Benchmarking: GET http://172.16.8.102/Login.php

4000 clients, running 30 sec.



Speed=685846 pages/min, 4664574 bytes/sec.

Requests: 342923 susceed, 0 failed.

squid+apache实现缓存加速的更多相关文章

  1. 谈谈varnish,squid,apache,nginx缓存的对比

    总是有人在问cache用什么,有varnish,squid,apache,nginx这几种,到底是我们用什么架构cache. 1.从这些功能上.varnish和squid是专业的cache服务,而ap ...

  2. Linux实战教学笔记43:squid代理与缓存实践(二)

    第6章 squid代理模式案例 6.1 squid传统正向代理生产使用案例 6.1.1 squid传统正向代理两种方案 (1)普通代理服务器 作为代理服务器,这是SQUID的最基本功能:通过在squi ...

  3. Linux实战教学笔记42:squid代理与缓存实践(一)

    第1章 Squid介绍 1.1 缓存服务器介绍 缓存服务器(英文意思cache server),即用来存储(介质为内存及硬盘)用户访问的网页,图片,文件等等信息的专用服务器.这种服务器不仅可以使用户可 ...

  4. Linux实战教学笔记36:PHP服务缓存加速深度优化实践

    一,PHP缓存加速器介绍与环境准备 1.1 PHP缓存加速器介绍 1.1.1 操作码介绍及缓存原理 当客户端请求一个PHP程序时,服务器的PHP引擎会解析该PHP程序,并将其编译为特定的操作码(Ope ...

  5. PHP7安装Memcache+Memcached缓存加速WordPress教程

    PHP7安装Memcache+Memcached缓存加速WordPress教程 2016年1月19日 6,691 Views 生活方式 PHP7最显著的变化就是性能的极大提升,已接近Facebook开 ...

  6. PHP WEB 引擎缓存加速优化

    PHP 缓存加速器介绍 操作码缓存 请求一个 PHP 程序时,PHP 引擎会解析程序,并且将编译码作为特定操作码.这是要执行的代 码的一种二进制表示形式.随后,此操作码有 PHP 引擎执行并丢弃.操作 ...

  7. squid代理与缓存(下)

    squid代理与缓存(下) 6. squid代理模式案例 6.1 squid传统正向代理生产使用案例 6.1.1 squid传统正向代理两种方案 (1)普通代理服务器 作为代理服务器,这是SQUID的 ...

  8. squid代理与缓存(上)

    squid代理与缓存(上) 1. Squid介绍 1.1 缓存服务器介绍 缓存服务器(英文意思cache server),即用来存储(介质为内存及硬盘)用户访问的网页,图片,文件等等信息的专用服务器. ...

  9. 使用tmpfs作为缓存加速缓存的文件目录

    使用tmpfs作为缓存加速缓存的文件目录 [root@web02 ~]# mount -t tmpfs tmpfs /dev/shm -o size=256m[root@web02 ~]# mount ...

随机推荐

  1. Flask web开发 简单介绍

    Flask是一个基于python的轻量级web框架.当安装好后Flask后 (pip install flask),就可以开始使用了. 一.最简单的例子 1.新建目录,作为web应用的目录,如: mk ...

  2. 基于visual Studio2013解决C语言竞赛题之0413同构数

       题目 解决代码及点评 该题目与水仙花数类似,只是条件不同,循环还是一样的 /***************************************************** ...

  3. socket通信技术介绍

    [-] 网络中进程之间怎样通信 什么是Socket socket一词的起源 socket的基本操作 socket函数 bind函数 网络字节序与主机字节序 listenconnect函数 accept ...

  4. NGUI出现Shader wants normals, but the mesh UIAtlas doesn&#39;t have them

    NGUI出现Shader wants normals, but the mesh UIAtlas doesn't have them,没有网格法线,打开UI Root上 UIPanel组建上的 Nor ...

  5. Triangle---minimum path sum

    动态规划 class Solution: # @param triangle, a list of lists of integers # @return an integer def minimum ...

  6. SGU 415. Necessary Coins ( 背包dp )

    题意大概是:给出N个硬币, 面值为a_i, 问要凑成X元哪些硬币是不可或缺的.1 ≤ N ≤ 200, 1 ≤ x ≤ 10^4 直接枚举, 然后就是01背包了. 为了不让复杂度多乘个N, 我们就从左 ...

  7. centos安装python gcc sqlite

    终端中输入命令:yum install gcc -y yum install python -y yum install sqlite -y

  8. wince下GetManifestResourceStream得到的Stream是null的解决

    问题的引入 在编程过程中遇到下面这样一个问题: 有这样一个方法: public static AlphaImage CreateFromResource(string imageResourceNam ...

  9. Bertelsmann Asia Investments, 简称BAI

    聚焦龙宇:贝塔斯曼的中国风险投资之路 _财经_腾讯网 贝塔斯曼亚洲投资基金(Bertelsmann Asia Investments, 简称BAI )

  10. linux下shutdown无法关闭tomcat进程的解决方式

    1.问题 笔者在linux下发现使用tomcat6.0.41自带的./shutdown.sh常常无法停止进程,导致各种问题的发生,令笔者相当反感! 2.解决方式一: 查找到全部的tomcat进程 $ ...