安装
1、 依赖包
使用在线安装依赖包
yum install pkgconfig libtool gcc gcc-c++ glibc-headers make openssltcl expat pcre pcre-devel libcap flex hwloc lua curses curl curl-devel tcl-develexpat expat-devel openssl-devel zlib-devel
2、 安装
版本:trafficserver-5.3.1.tar.bz2
下载地址:http://trafficserver.apache.org/
解压后执行命令:./configure–prefix=/usr/local/ats
make&& make install
测试是否安装成功
进入安装目录下的bin,找到traffic_server,执行如下命令
./traffic_server–R 1
如果最终出现 REGRESSION_TESTDONE: PASSED 表示安装成功
3、 启动、重启与停止
在安装目录下 bin下面
执行命令 ./trafficserver start 启动
./trafficserverrestart 重启
./trafficserverstop 停止
外部环境
1、由于是反向代理模式,ATS监听80端口,需要防火墙开启80端口的权限,或者直接关闭防火墙(命令:service iptables stop)
2、需要网络通畅
反向代理配置
ATS 的主要的配置文件时records.config ,位于安装目录下etc/trafficserver下面
CONFIGproxy.config.http.cache.http INT 1 #开启http缓存代理请求
CONFIGproxy.config.reverse_proxy.enabled INT 1 #支持反向代理
CONFIGproxy.config.url_remap.remap_required INT 1 #url重映射,只有满足remap.config中的规则的请求才允许被访问
CONFIGproxy.config.url_remap.pristine_host_hdr INT 1 #
CONFIGproxy.config.http.server_ports STRING 80 #绑定反向代理端口80
配置文件remap.config,位于安装目录下 etc/trafficserver下面
添加你指定能够访问服务的域名或者URL,格式如下
map http://www.baidu.com/http://www.baidu.com/
配置完毕后需要使配置文件生效,有3种方式
1、 重启服务,如果服务已经启动,不建议使用该方法
2、 使用bin目录下的工具./traffic_line –x 命令重新应用配置文件,这是以前版本中支持的方法,当然现在也支持
3、 使用bin 目录下的工具./traffic_ctl config reload 重新加载配置文件,这是官方推荐使用的方法
日志管理
配置为写本地管道模式
1、 配置安装目录下的/etc/trafficserver/records.config文件
proxy.config.log.logging_enabledINT 3 #记录所有日志(错误日志和访问日志)
proxy.config.log.log.max_space_mb_for_logINT 2500 #日志所占硬盘最大空间,单位为M
proxy.config.log.logfile_dirSTRING var/log/trafficserver #日志存放路径,默认在安装路径下的这个文件目录中,可以指定绝对路径到其它位置
proxy.config.log.collation_modeINT 0 #禁止使用日志收集模式
proxy.config.log.custom_ogs_enableINT 1 #使用自定义日志
2、 配置安装目录下的/etc/trafficserver/logs_xml.config文件
<LogObject>
<Mode = “asci_pipe”/>
<Format = “squid”/>
<Filename = “squid_log”/>
</LogObject>
重新启动ATS服务之后,会在日志存放路径下生成一个名为 squid_log.pipe 的管道文件,然后我们就可以从管道中读取数据,继续后续操作。
允许或禁止域名访问
1、 首先按照反向代理配置中配置records.config文件
2、 在remap.config文件中添加规则(该配置文件的规则格式见附录 remap.config文件详解)
比如需要允许来自客户的请求www.baidu.com被允许访问并缓存,添加如下规则
map http://www.baidu.com/http://www.baidu.com/
要禁止域名访问,只需要从该配置文件中删除这个域名的规则
注意:一旦添加域名规则,包含这个域名的所有URL请求都能够访问源站
允许或禁止URL缓存
1、 首先需要允许该URL请求能够访问源站,及URL的域名或者URL本身被添加在remap.config文件中,如:www.aodun.com.cn/news/1/index.html具体规则格式如下:
map http://www.test.com.cn/news/1/index.htmlhttp://www.test.com.cn/news/1/index.html
2、 在cache.config文件中做如下配置
禁止缓存:
url_regex= test.com.cn/news/1/index.html action=never-cache
说明:默认被允许的请求都被缓存
允许缓存:只要不被禁止缓存被允许缓存
顺便说一下:还可以设置指定时间刷新缓存,或是永久不刷新等等,详情请参考附录cache.config文件详解
防盗链设置
首先需要在records.config配置文件中开启下面的功能
CONFIG proxy.config.http.referer_filterINT 1 #开启referer过滤
CONFIGproxy.config.http.referer_format_redirect INT 1 #运行格式化redirect-URL,使用一些格式字符,如:%r%f
该功能需要对配置文件remap.config设置规则来实现
例如:map_with_refererhttp://y.foo.bar.com/x/yy/ http://foo.bar.com/x/yy/ http://games.bar.com/new_games .*\.bar\.comwww.bar-friends.com
refererheader必须在request中而且只能为这些“.*\.bar\.com 和 www.bar-friends.com”,才会被允许访问
map_with_refererhttp://y.foo.bar.com/x/yy/ http://foo.bar.com/x/yy/ http://games.bar.com/new_games * ~.*\.evil\.com
referer header必须在request中,所有的都能访问,除了".*\.evil\.com"
map_with_refererhttp://y.foo.bar.com/x/yy/ http://foo.bar.com/x/yy/ http://games.bar.com/error ~* * ~.*\.evil\.com
refererheader是可选项,但是如果存在,只有 ".*\.evil\.com"这样的请求能被重定向到http://games.bar.com/error
更多的用法请参考附录remap.config文件详解
附录
remap.config 文件详解
URL重新映射配置文件
使用重新映射。配置允许您完成两件事:
1)重写URL(客户端)之前发送原始服务器。
2)保护代理服务器,只允许特定的请求。
用默认配置,至少一个重新映射规则是必需的。这可以简单的跟随下面的操作配置records.config:
配置proxy.config.url_remap.remap_requiredINT 0
请注意,这是反向代理的一个通用开关,开启url映射请求!
格式是:
< map_type > client-URLorigin-server-URL < tag_value > <filtering>
client-URL和origin-server-URL的格式如下
<scheme>://<host>:<port>/<path_prefix>
scheme: http, https
< tag_value >指令是可选的,可以根据<map_type >的不同而不同。
<filtering arguments> 是可选的指令,如ACL访问控制列表一样
每个重新映射规则的参数都是唯一的
6种不同类型的映射:
map
map_with_referer
map_with_recv_port
reverse_map
redirect
redirect_temporary
每一种映射类型可以用字符串前缀“regex_”指明的正则表达式规则的字符串。有关更多信息,请参见这个描述的最后部分对正则表达式的支持。
“map” 映射是最简单的。请求匹配的client-URL重写到origin-server-URL。用户代理将看到重新映射的页面的URL,但不会知道地址的更改。
“map_with_referer”是一个扩展版的“map”,可以用来激活在ApacheTraffic Server 特殊功能,称之为“deep linking protection”深度链接保护。
“map_with_recv_port”是极其像“map”,除了它使用指定的端口去替换原请求中的端口。如果出现这样的指令,“map_with_recv_port”是首先被检查映射。假如有一条匹配规则,然后选择没有评估“常规”映射规则
“reverse_map” 被发送到源服务器时会被重写本地header
“redirect” 创建一个固定的重定向信息,然后会告知浏览器URL的改变
“redirect_temporary” 和 “redirect” 类似,只是会告诉浏览器这次的重定向只是临时的。我们需要在反向代理模式下映射这个URL以便用户代理能够联系Traffic Server而不是联系源服务器
例如:下面为www.example.com设置一个反向代理,而真实的内容却在server1.example.com,规则如下
map http://www.example.com/ http://server1.example.com/
reverse_map http://server1.example.com/ http://www.example.com/
或者永久性的重定向用户的访问www.oldserver.com到www.newserver.com
redirect http://www.oldserver.com/ http://www.newserver.com
如果使用Traffic Server的特色,深度连接保护映射策略,常规格式如下
map_with_referer client-URLorigin-server-URL redirect-URL regex1 [regex2 ...]
首先要开启records.config中的
CONFIGproxy.config.http.referer_filter INT 1
为了使运行时格式化redirect0URL,需要开启
CONFIGproxy.config.http.referer_format_redirect INT 1
%r 替换源referer中的header字符串
%f 替换client-URLfrom 'map_with_referer' record
%t 替换origin-server-URLfrom 'map_with_referer' record
%o 替换request URLto origin server, which was created a the result of a mapping operation
例如:map_with_refererhttp://y.foo.bar.com/x/yy/ http://foo.bar.com/x/yy/ http://games.bar.com/new_games .*\.bar\.comwww.bar-friends.com
referer header必须在request中而且只能为这些 “.*\.bar\.com和 www.bar-friends.com”,才会被允许访问
map_with_refererhttp://y.foo.bar.com/x/yy/ http://foo.bar.com/x/yy/ http://games.bar.com/new_games * ~.*\.evil\.com
referer header必须在request中,所有的都能访问,除了".*\.evil\.com"
map_with_refererhttp://y.foo.bar.com/x/yy/ http://foo.bar.com/x/yy/ http://games.bar.com/error ~* * ~.*\.evil\.com
referer header是可选项,但是如果存在,只有 ".*\.evil\.com"这样的请求能被重定向到http://games.bar.com/error
filtering参数选项在规则末尾被指定,如下
@action=allow|deny
@src_ip=IP-address
@method=HTTP methodstring (CONNECT|DELETE|GET|HEAD|ICP_QUERY|OPTIONS|POST|PURGE|PUT|TRACE|PUSH)
@plugin=<plugin_path>
@pparam=<plugin_param>
这些参数可以叠加
例如:
maphttp://foo.cow.com/ http://bar.cow.com @src_ip=10.72.118.51-10.72.118.62@method=GET @method=DELETE @src_ip=192.168.0.1-192.168.0.254 @action=allow@method=PUT
Traffic Server 支持WebSockets,但是只能通过remap,WebSocket能够自动升级当检测到remap规则中包含"ws://scheme"
例如:
map ws://bar.com/ws://foo.com/
当一个带着恰当升级headers的请求过来时,TrafficServer 用remap规则尝试建立和维护websocket连接
过滤器
过滤器可以被创建并应用到这些 .definefilter, .activatefilter, and .deactivatefilter指令定义的块映射;
过滤器在使用之前必须用.definefilter定义,一旦定义,.activatefilter可以为接下来的每个映射激活一个过滤器直到用.deactivatefilter指令失活映射
例如: .definefilter disable_delete_purge@action=deny @method=delete @method=purge
.definefilterinternal_only @action=allow @src_ip=192.168.0.1-192.168.0.254 @src_ip=10.0.0.1-10.0.0.254
.activatefilterdisable_delete_purge
maphttp://foo.example.com/ http://bar.example.com/
.activatefilterinternal_only
maphttp://www.example.com/admin http://internal.example.com/admin
.deactivatefilterinternal_only
maphttp://www.example.com/ http://internal.example.com/
关于正则表达式
1、只有host 域可以使用,scheme,port和其他域不能使用
2、捕捉子模式最大限制为9,这意味着只能使用0-9作为占位符作为整个输入字符串
3、字符串替换的数量扩张仅限于10。
包含子目录
.include filters.config
.include one.example.com.configtwo.example.com.config
filters.config
.definefilter deny_purge@action=deny @method=purge
.definefilter allow_purge@action=allow @method=purge
one.example.com.config
.activatefilter deny_purge
map http://one.example.comhttp://origin-one.example.com
.deactivatefilter deny_purge
two.example.com.config
.activatefilter allow_purge
map http://two.example.comhttp://origin-two.example.com
.deactivatefilterdallowpurge
注意:只有当remap.config文件改变,然后执行命令(5.3.1版本中)traffic_ctl config reload 子配置文件才会被检测到
cache.config 文件详解
cache.config
使用这个文件的目的是修改指定的对象或者对象集的缓存参数
每行是这样的一对tag value集合,格式为<tag>=<value>
每行都必须包含一个主要的说明符
主要的目的说明符有
dest_domain=
dest_host=
dest_ip=
url_regex=
行可以包含任意数量的辅助说明符但辅助说明符可能不是重复的在同一行
辅助说明符有
port=
scheme=
prefix=
suffix=
method=
time=
src_ip=
每一行必须包括一个缓存指令
缓存指令有
action=never-cache
action=ignore-no-cache (client & server no cache)
action=ignore-client-no-cache (only client no cache)
action=ignore-server-no-cache (only server no cache)
pin-in-cache=<time>
revalidate=<time>
ttl-in-cache=<time> (force caching and expire after<time>)
每一行也还可以包含各种各样的调节缓存参数的微调指令
微调指令有
cache-responses-to-cookies=<value>
改变对cookies的缓存类型。这有效的覆盖了配置中的proxy.config.http.cache.cache_responses_to_cookies
使用相同的值具有相同的语义,这覆盖只发生在请求匹配
例如:使来自www.example.com的http对象在2个小时后重新验证
dest_domain=www.example.com scheme=http revalidate=2h