Centos 搭建 Apache Trafficserver 缓存服务器
常见的缓存服务器有squid、Varnish以及我们今天要讲的Apache Trafficeserver,本文将带你一起安装和配置缓存服务器实现对源站的资源进行加速,
Trafficserver官网:http://trafficserver.apache.org/
配置设备简介
2台设备:
一台阿里云青岛服务器(源站 LNMP环境)
一台深圳服务器(ATS)
安装ATS
参见官网文档:https://docs.trafficserver.apache.org/en/latest/getting-started/index.en.html#installation
1.Centos安装ats
wget https://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-5.noarch.rpm
sudo rpm -Uvh epel-release-7*.rpm
yum -y install trafficserver
2.配置文件在/etc/trafficserver
目录下
cd /etc/trafficserver/
[root@iZ94dhr5x5iZ trafficserver]# ls
body_factory hosting.config_1 logs_xml.config_1 proxy.pac_1 splitdns.config trafficserver-release
cache.config icp.config parent.config records.config splitdns.config_1 update.config
cache.config_1 icp.config_1 parent.config_1 records.config_1 ssl_multicert.config update.config_1
cluster.config ip_allow.config plugin.config remap.config ssl_multicert.config_1 vaddrs.config
cluster.config_1 ip_allow.config_1 plugin.config_1 remap.config_1 stats.config.xml vaddrs.config_1
congestion.config log_hosts.config prefetch.config snapshots stats.config.xml_1 volume.config
congestion.config_1 log_hosts.config_1 prefetch.config_1 socks.config storage.config volume.config_1
hosting.config logs_xml.config proxy.pac socks.config_1 storage.config_1
3.核心配置 /etc/trafficserver/record.config
CONFIG proxy.config.http.server_ports STRING 80 #反向代理监听端口
CONFIG proxy.config.cache.ram_cache.size INT 512M #内存缓存
4.回源配置 /etc/trafficserver/remap.config
访问的URL 回源地址
map http://www.v5linux.com/ http://www.fangwenjun.com/
5.缓存目录及大小 /etc/trafficserver/storage.config
/var/cache/trafficserver 256M
#或使用裸盘
#/dev/sdb
6.配置完后开启ATS
/etc/init.d/trafficserver start
chkconfig trafficserver on
7.防火墙
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
7.访问网站
8.解决跨域问题
解决方案
在源站nginx中加入响应头
然后再次查看响应头
其他参数
record.config常用参数:
#用于标识名称,Cluster模式下同一集群必须保持一致才能建立集群
traffic_line -s proxy.config.proxy_name -v test
#配置cluster 模式
– 1 纯cluster 模式,有7层代理功能和集群配置同步
– 2 仅做配置管理
– 3 单机模式
traffic_line -s proxy.local.cluster.type -v 1
#自动配置线程数,多少个CPU配置多少个线程,可调整
CONFIG proxy.config.exec_thread.autoconfig INT 1 #开启
CONFIG proxy.config.exec_thread.autoconfig.scale FLOAT 1.500000 #CPU核数与TS线程数量比例
CONFIG proxy.config.exec_thread.limit INT 2 #每个核创建的线程数
CONFIG proxy.config.accept_threads INT 1 #运行单独线处理请求
#监听8080、80端口,8080 一般用于管理端口,80为服务端口
traffic_line -s proxy.config.http.server_ports -v 8080 80
#使用ssd做冷热缓存
LOCAL proxy.config.cache.ssd.storage STRING /dev/sdb
#Cache内存大小,-1 为不限制,具体大小结合业务调整,内存命中越高磁盘IO越小
traffic_line -s proxy.config.cache.ram_cache.size -v 25769803776
#Cache内存淘汰算法,采用CLFUS,LRU 模式有问题
traffic_line -s proxy.config.cache.ram_cache.algorithm -v 1
#传输超时时间,默认900s,传输大文件容易触发超时,改成不限制
traffic_line -s proxy.config.http.transaction_active_timeout_in -v 0
#negative TTL 功能开启
traffic_line -s proxy.config.http.negative_caching_enabled -v 1
#忽略判断Accept头,默认会根据Accept头做多副本缓存
traffic_line -s proxy.config.http.cache.ignore_accept_mismatch -v 1
#忽略判断Accept-Language头,默认会根据Accept-Language头做多副本缓存
traffic_line -s proxy.config.http.cache.ignore_accept_language_mismatch -v 1
#忽略判断Accept-Charset头,默认会根据Accept-Charset头做多副本缓存
traffic_line -s proxy.config.http.cache.ignore_accept_charset_mismatch -v 1
#忽略client max-age
traffic_line -s proxy.config.http.cache.ignore_client_cc_max_age -v 1
#忽略HTTP认证头,默认带Authentication是不做缓存的
traffic_line -s proxy.config.http.cache.ignore_authentication -v 1
#开启vary 功能
traffic_line -s proxy.config.http.cache.enable_default_vary_headers -v 1
#开启回源合并
traffic_line -s proxy.config.cache.enable_read_while_writer -v 1
#平均object 大小,结合业务配置
traffic_line -s proxy.config.cache.min_average_object_size -v 16384
#开启HTTP UI
traffic_line -s proxy.config.http_ui_enabled -v 3
隐藏版本号
traffic_line -s proxy.config.http.response_server_str -v HFS/1.0.
traffic_line -s proxy.config.http.response_via_str -v HFS
然后reload配置文件
traffic_line -x
缓存设置
设置cache.config
dest_domain=www.fangwenjun.com suffix=gif revalidate=6h
dest_domain=www.fangwenjun.com suffix=jpg revalidate=6h
dest_domain=www.fangwenjun.com suffix=jpeg revalidate=6h
dest_domain=www.fangwenjun.com suffix=png revalidate=6h
dest_domain=www.fangwenjun.com suffix=bmp revalidate=6h
dest_domain=www.fangwenjun.com suffix=swf revalidate=6h
dest_domain=www.fangwenjun.com suffix=ico revalidate=6h
dest_domain=www.fangwenjun.com suffix=js revalidate=6h
dest_domain=www.fangwenjun.com suffix=css revalidate=6h
dest_domain=www.fangwenjun.com revalidate=12h