Apache Traffic Server,ats

零.前言

1.官网 http://trafficserver.apache.org/

2.国内社区 https://blog.zymlinux.net

3.简洁明了的配置:http://blog.csdn.net/tiantiandjava/article/details/50327855

一.ats安装

1.安装依赖包

#yum install pkgconfig libtool gcc make openssl tcl expat pcre pcre-devel libcap flex hwloc lua curses curl tcl-devel

  



2.下载安装包

下载trafficserver-5.3.2

#wget http://apache.fayea.com/trafficserver/trafficserver-5.3.2.tar.bz2

  



3.安装ats

#tar xvf trafficserver-5.3.2.tar.bz2
#cd trafficserver-5.3.2
#./configure --prefix=/usr/local/ats
#make && make install

  

二.配置

1.ats的配置文件都在安装目录下的etc/trafficserver,此处为/usr/local/ats/etc/trafficserver

2.重要配置文件:

(1).records.config 主要配置文件,配置ats的各项参数

安装完后该项保持默认配置

(2).remap.config 配置访问域名和源

如,在该文件最后增加

map http://domain.com http://113.7.1.126:8111/



前者为绑定域名,后者为源

(3).storage.config 配置磁盘缓存

在其最后增加

/ats_cache 20G



该配置是将磁盘缓存缓存在/ats_cache目录下,大小为20G

(4).cache.config 缓存配置

该文件决定了ats对缓存的配置

url_regex=192.168.2.105:8112/news.php\?id=.* ttl-in-cache=2h cache-responses-to-cookies=4

  

cache-responses-to-cookies取值介绍

0 不缓存任何带cookie的源服务器返回结果

1 缓存任何结果

2 只缓存图片类型的结果

3 缓存除了text类型的所有结果

4 缓存所有的非text类型的返回结果,以及不带 Set-Cookie 头或带 Cache-Control: public的text类型的源服务器返回结果

以上url正则表示缓存源网址为192.168.2.105:8112/news.php?id=任意结果的相应数据,缓存两小时,前提是该网址不带有Set-Cookie

dest_ip=127.0.0.1 suffix=gif ttl-in-cache=60d
dest_ip=127.0.0.1 suffix=jpg ttl-in-cache=60d
dest_ip=127.0.0.1 suffix=jpeg ttl-in-cache=60d
dest_ip=127.0.0.1 suffix=png ttl-in-cache=60d
dest_ip=127.0.0.1 suffix=bmp ttl-in-cache=60d
dest_ip=127.0.0.1 suffix=swf ttl-in-cache=60d
dest_ip=127.0.0.1 suffix=ico ttl-in-cache=60d
dest_ip=127.0.0.1 suffix=js ttl-in-cache=60d
dest_ip=127.0.0.1 suffix=css ttl-in-cache=60d

以上将标示缓存127.0.0.1的静态文件,缓存60天

(5).logs_xml.config 配置日志

三.常用配置

1.配置http ui

(1).在recoreds.config最后增加

CONFIG proxy.config.http_ui_enabled INT 3
CONFIG proxy.config.http.enable_http_info INT 1

  



(2).在remap.config中增加

map http://localhost/cache-internal/ http://{cache-internal}
map http://localhost/cache/ http://{cache} @action=allow @src_ip=127.0.0.1
map http://localhost/stat/ http://{stat} @action=allow @src_ip=127.0.0.1
map http://localhost/test/ http://{test} @action=allow @src_ip=127.0.0.1
map http://localhost/hostdb/ http://{hostdb} @action=allow @src_ip=127.0.0.1
map http://localhost/net/ http://{net} @action=allow @src_ip=127.0.0.1
map http://localhost/http/ http://{http} @action=allow @src_ip=127.0.0.1

  

(3).通过以上配置,即可通过http协议来访问http ui

2.配置日志

(1).ats默认的日志是squid格式并不符合要求

(2).配置自定义日志

a.在logs_xml.config中增加

<LogFormat>
<Name = "access"/>
<Format = "%<chi> %<cqtq> %<ttms> %<{X-Forwarded-For}cqh> %<crc>/%<pssc> %<pscl> %<cqhm> %<cquuc> %<cqhv> %<phr>/%<pqsi> %<psct> \"%<{Referer}cqh>\" \"%<{User-Agent}cqh>\" %<shn> %<sscl>"/>
</LogFormat>

<LogObject>
<Format = "access"/>
<Filename = "access"/>
</LogObject>

  

*format每项代表的意思,可以通过 https://docs.trafficserver.apache.org/en/5.3.x/admin/working-log-files.en.html 来查看

b.修改或增加records.config中的如下配置

CONFIG proxy.config.log.squid_log_enabled INT 0
CONFIG proxy.config.log.custom_logs_enabled INT 1

  

c.重载配置,后面会介绍



3.配置via标示,来快速确认文件缓存状态

a.

CONFIG proxy.config.http.insert_response_via_str INT 2

  

b.然后查看头部,可以看到

Via:http/1.1 localhost.localdomain (ApacheTrafficServer/5.3.2 [cRs f ])

c.查看[cRs f ],可以通过网址

http://trafficserver.apache.org/tools/via#cSsSfD

来查看

也可以通过命令来解释,后面会介绍



四.常用命令

1.命令通常在安装目录下的bin目录下,本文为/usr/local/ats/bin/



2./usr/local/ats/bin/trafficserver start #启动

/usr/local/ats/bin/trafficserver stop #停止

/usr/local/ats/bin/trafficserver restart #停止

3.重载配置

/usr/local/ats/bin/traffic_ctl config reload

4.查看squid日志

/usr/local/ats/bin/traffic_logcat filename #查看squid日志

5./usr/local/ats/bin/traffic_logstats #查看状态

6./usr/local/ats/bin/traffic_via [cRs f ] #查看via头状态