Traffic Server 正向代理(透明代理)
原文 http://blog.chinaunix.net/uid-10540984-id-3083216.html
解压源码包后先阅读 README 文件,里面告诉了需要那些开发包的支持。 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
- dest_domain=video.sina.com.cn scheme=http
revalidate=24h
- dest_domain=www.pptv.com scheme=http revalidate=24h
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.config map 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: command traffic_line -s proxy.config.http_ui_enabled -v 3 traffic_line -s proxy.config.http.enable_http_info -v 1
- get traffic_server aware of the change: command traffic_line -x
- testing with nc: command echo -ne "GET http://localhost/stat/ HTTP/1.0 " | nc -q 3 localhost 8080
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"
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。