原文 http://blog.chinaunix.net/uid-10540984-id-3083216.html
安装:
Ubuntu:
apt-get install -y libssl-dev tcl-dev libexpat-dev libexpat-dev libpcre3-dev libcap-dev
安装以上各个开发包。
./configure --help 查看编译的一些选项
./configure (默认安装在 /usr/local,如需修改,使用 --prefix=PREFIX;参数中还有用户和用户组选项,这是 TS 进程运行的身份,默认均为 nobody,centos 可以不作修改,其他发行版可能需要修改,如 ./configure --with-group=nogroup)
make
make install
最后安装完成后注意这个提示:
Libraries have been installed in:
/usr/local/libexec/trafficserver
提示说明自己安装的动态库文件安装在该路径下,所以编辑 /etc/ld.so.conf.d/libc.conf 配置文件,把该库文件路径加入文件中。运行 ldconfig 命令,使文件生效。
目录结构:
默认目录 内容
运行时创建的日志文件 /usr/local/var/log/trafficserver
运行时的一系列文件 /usr/local/var/trafficserver
配置文件 /usr/local/etc/trafficserver
可执行文件 /usr/local/bin
插件 /usr/local/libexec/trafficserver
初步配置(正向代理配置):
编辑全局变量配置文件,默认安装目录 /usr/local/etc/trafficserver/records.config
关闭反向代理:
- traffic_line -s proxy.config.reverse_proxy.enabled -v 0
关闭反向代理转发:
- traffic_line -s proxy.config.url_remap.remap_required -v 0
允许TS操作客户端请求的host头部:
- traffic_line -s proxy.config.url_remap.pristine_host_hdr -v 0
忽略服务器端和客户端的http头部的 no_cache 和 max_age:
- traffic_line -s proxy.config.http.cache.ignore_server_no_cache -v 1
- traffic_line -s proxy.config.http.cache.ignore_client_cc_max_age -v 1
这个非常关键,如果不设置这个,很多url都不会被缓存的
缓存所有对象,忽略头部为 Expires 和 max-age(因为几乎没有对象会有明确的截止日期和生存时间):
- traffic_line -s proxy.config.http.cache.required_headers -v 0
DEBUG模式开关(默认关闭,调试时候可以打开,海量信息,慎用):
- traffic_line -s proxy.config.diags.debug.enabled -v 0
关闭squid.log日志记录(会增长很大)
- traffic_line -s proxy.config.log.squid_log_enabled -v 0
关闭日志输出(节约日志空间):
- traffic_line -s proxy.config.log.logging_enabled -v 0
Cache-directive | 说明 |
---|---|
public | 所有内容都将被缓存 |
private | 内容只缓存到私有缓存中 |
no-cache | 所有内容都不会被缓存 |
no-store | 所有内容都不会被缓存到缓存或 Internet 临时文件中 |
must-revalidation/proxy-revalidation | 如果缓存的内容失效,请求必须发送到服务器/代理以进行重新验证 |
max-age=xxx (xxx is numeric) | 缓存的内容将在 xxx 秒后失效, 这个选项只在HTTP 1.1可用, 并如果和Last-Modified一起使用时, 优先级较高 |
cache.config配置:
我们可以针对一些php、asp、cgi等页面不缓存,编辑cache.conf文件,加入下列规则:
- url_regex=.* suffix=php action=never-cache
针对某些视频的http应答包中存在age项,而导致可以缓存,但是一旦二次访问则检查为过期的,在这里强行指定它的过期时间为25小时,这里的 dest_domain 值为cacheurl插件修改过后的静态地址的域名:
- dest_domain=video.sina.com.cn scheme=http revalidate=24h
- dest_domain=www.pptv.com scheme=http revalidate=24h
http://trafficserver.apache.org/docs/trunk/admin/configuration-files/cache.config.en.html,还有其他很强大的功能,比如指定域名,指定源地址,指定忽略no-cache等等。
cacheurl插件编译安装:
http://svn.apache.org/repos/asf/trafficserver/plugins/trunk/cacheurl/ 在官网上下载 cacheurl.c 和 Makefile 文件,然后进入文件目录编译:
make TSXS=/usr/local/bin/tsxs
make TSXS=/usr/local/bin/tsxs install
源码里有版本校验函数,只支持到2.1.6版本,所以最新3.0.2版无法支持,暂时修改源代码支持到3.0后插件能正常工作,稳定性有待测试。
编译完成后在 /usr/local/etc/trafficserver/plugin.config 文件中加入动态库 cacheurl.so
然后在插件目录 /usr/local/libexec/trafficserver/cacheurl.config,写入Rewrite url 的 perl正则即可。会在 /usr/local/var/log/trafficserver/cacheurl.log 产生Rewrite日志。
开启HTTP_UI功能:
http_ui is a cache inspector for TS internal system, a Swiss knife tool for TS, you can setup http_ui by:
- add these remap rules to remap.config, you may need to replace localhost to your own hostname
remap.configmap http://localhost/cache-internal/ http://{cache-internal}map http://localhost/cache/ http://{cache}map http://localhost/stat/ http://{stat}map http://localhost/test/ http://{test}map http://localhost/hostdb/ http://{hostdb}map http://localhost/net/ http://{net}map http://localhost/http/ http://{http}
- enable http_ui from records.config, we use command line tool here:
commandtraffic_line -s proxy.config.http_ui_enabled -v 3traffic_line -s proxy.config.http.enable_http_info -v 1
- get traffic_server aware of the change:
commandtraffic_line -x
- testing with nc:
commandecho -ne "GET http://localhost/stat/ HTTP/1.0\r\n\r\n" | nc -q 3 localhost 8080
使用 raw device 设备:
The following example will use an entire raw disk in the Linux operating system:
- #磁盘设备
- /dev/sdb
- /dev/sdc
In order to make sure traffic_server will have access to this disk you can use udev to persisently set the right permissions. The following rules are targeted for an Ubuntu system, and stored in /etc/udev/rules.d/51-cache-disk.rules:
# Assign /dev/sde and /dev/sdf to the www group
# make the assignment final, no later changes allowed to the group!
- # GROUP 为 TS运行的用户组,否则会无权限加载磁盘
- SUBSYSTEM=="block", KERNEL=="sd[bc]", GROUP:="nogroup"
然后运行 udevadm trigger 命令,重新接收内核事件,或者reboot也是可以的。