ATS配置及缓存规则Apache Traffic Server详细教程

Apache Traffic Server 怎么配置,Apache Traffic Server 怎么安装,ATS缓存规则,ATS配置文件,ATS缓存方法,之前写了一篇 OneClickCDN 一键ATS自建CDN节点脚本图文教程 用来加速你的网站,今天另外写一篇关于ATS缓存规则配置的问题,如果不修改配置文件的话,在登陆注册都会出现一系列的问题,比如登陆状态不刷新,登陆出现问题等诸多问题。

OneClickCDN

ATS管理命令

1.启动
trafficserver start 
2.关闭
trafficserver stop
3.重启
trafficserver restart 
4.重载配置文件(很多情况下好像都没有生效)
traffic_ctl config reload
5.监控ats的状况 类似于top命令
traffic_top
6.清理所有缓存
traffic_server -Cclear

修改配置文件

cache.config 【缓存类型配置】路径 cd /etc/trafficserver/vim cache.config

url_regex=.* suffix=xml  ttl-in-cache=5d
url_regex=.* suffix=ts  ttl-in-cache=5d
url_regex=.* suffix=jpeg  ttl-in-cache=5d
url_regex=.* suffix=mp4  ttl-in-cache=5d
url_regex=.* suffix=zip  ttl-in-cache=5d
url_regex=.* suffix=gif  ttl-in-cache=5d
url_regex=.* suffix=ppt  ttl-in-cache=5d
url_regex=.* suffix=jpg  ttl-in-cache=5d
url_regex=.* suffix=swf  ttl-in-cache=5d
url_regex=.* scheme=http  ttl-in-cache=0
url_regex=.* scheme=https  ttl-in-cache=0
url_regex=.* scheme=m3u8  ttl-in-cache=5d
url_regex=.* scheme=js  ttl-in-cache=5d
url_regex=.* scheme=css  ttl-in-cache=5d
url_regex=.* suffix=webp  ttl-in-cache=5d
url_regex=.* suffix=php action=never-cache
url_regex=.* suffix=php  ttl-in-cache=0
url_regex=.* suffix=html action=never-cache

record.config 配置文件 路径 cd /etc/trafficserver/

#保持默认
CONFIG proxy.config.exec_thread.autoconfig INT 1
CONFIG proxy.config.exec_thread.autoconfig.scale FLOAT 1.500000
CONFIG proxy.config.exec_thread.limit INT 2
CONFIG proxy.config.accept_threads INT 1
CONFIG proxy.config.task_threads INT 2
CONFIG proxy.config.cache.threads_per_disk INT 8
CONFIG proxy.config.exec_thread.affinity INT 1

#这个地方代表开放的端口 这里是开发80作为web端口 443作为ssl端口 可以根据自己需求更改
CONFIG proxy.config.http.server_ports STRING 80 443:ssl

#VIA 啥的 这个地方可以修改默认的server 
CONFIG proxy.config.http.insert_request_via_str INT 0
CONFIG proxy.config.http.insert_response_via_str INT 2
#这个地方修改默认的server还有via头 待会下面放效果图
CONFIG proxy.config.http.response_server_str STRING chasel_cdn/1.0
CONFIG proxy.config.http.response_via_str STRING chasel_cdn

#这个地方还是默认
CONFIG proxy.config.http.parent_proxy_routing_enable INT 0
CONFIG proxy.config.http.parent_proxy.retry_time INT 300
CONFIG proxy.config.http.parent_proxy.connect_attempts_timeout INT 30
CONFIG proxy.config.http.forward.proxy_auth_to_parent INT 0
CONFIG proxy.config.http.uncacheable_requests_bypass_parent INT 1

#链接超时啥的 
CONFIG proxy.config.http.keep_alive_no_activity_timeout_in INT 60
CONFIG proxy.config.http.keep_alive_no_activity_timeout_out INT 60
CONFIG proxy.config.http.transaction_no_activity_timeout_in INT 20
CONFIG proxy.config.http.transaction_no_activity_timeout_out INT 20
CONFIG proxy.config.http.transaction_active_timeout_in INT 900
CONFIG proxy.config.http.transaction_active_timeout_out INT 0
CONFIG proxy.config.http.accept_no_activity_timeout INT 60
CONFIG proxy.config.net.default_inactivity_timeout INT 86400

##############################################################################
# Origin server connect attempts. Docs:
#    https://docs.trafficserver.apache.org/records.config#origin-server-connect-attempts
##############################################################################
CONFIG proxy.config.http.connect_attempts_max_retries INT 3
CONFIG proxy.config.http.connect_attempts_max_retries_dead_server INT 1
CONFIG proxy.config.http.connect_attempts_rr_retries INT 3
CONFIG proxy.config.http.connect_attempts_timeout INT 30
CONFIG proxy.config.http.post_connect_attempts_timeout INT 1800
CONFIG proxy.config.http.down_server.cache_time INT 60
CONFIG proxy.config.http.down_server.abort_threshold INT 10
##############################################################################
# Negative response caching, for redirects and errors. Docs:
#    https://docs.trafficserver.apache.org/records.config#negative-response-caching
##############################################################################
CONFIG proxy.config.http.negative_caching_enabled INT 1
CONFIG proxy.config.http.negative_caching_lifetime INT 10
##############################################################################
# Proxy users variables. Docs:
#    https://docs.trafficserver.apache.org/records.config#proxy-user-variables
##############################################################################
CONFIG proxy.config.http.insert_client_ip INT 1
CONFIG proxy.config.http.insert_squid_x_forwarded_for INT 1
##############################################################################
# Security. Docs:
#    https://docs.trafficserver.apache.org/records.config#security
##############################################################################
CONFIG proxy.config.http.push_method_enabled INT 0
##############################################################################
# Enable / disable HTTP caching. Useful for testing, but also as an
# overridable (per remap) config
##############################################################################
CONFIG proxy.config.http.cache.http INT 1
##############################################################################
# Cache control. Docs:
#    https://docs.trafficserver.apache.org/records.config#cache-control
#    https://docs.trafficserver.apache.org/en/latest/admin-guide/files/cache.config.en.html
##############################################################################
CONFIG proxy.config.http.cache.ignore_client_cc_max_age INT 1
CONFIG proxy.config.http.normalize_ae_gzip INT 1
CONFIG proxy.config.http.cache.cache_responses_to_cookies INT 1
CONFIG proxy.config.http.cache.cache_urls_that_look_dynamic INT 1
    # https://docs.trafficserver.apache.org/records.config#proxy-config-http-cache-when-to-revalidate
CONFIG proxy.config.http.cache.when_to_revalidate INT 0
    # https://docs.trafficserver.apache.org/records.config#proxy-config-http-cache-required-headers
CONFIG proxy.config.http.cache.required_headers INT 0
##############################################################################
# Heuristic cache expiration. Docs:
#    https://docs.trafficserver.apache.org/records.config#heuristic-expiration
##############################################################################
CONFIG proxy.config.http.cache.heuristic_min_lifetime INT 3600
CONFIG proxy.config.http.cache.heuristic_max_lifetime INT 86400
CONFIG proxy.config.http.cache.heuristic_lm_factor FLOAT 0.100000
##############################################################################
# Network. Docs:
#    https://docs.trafficserver.apache.org/records.config#network
##############################################################################
CONFIG proxy.config.net.connections_throttle INT 80000
CONFIG proxy.config.net.max_connections_in INT 80000
CONFIG proxy.config.net.max_connections_active_in INT 30000
##############################################################################
# RAM and disk cache configurations. Docs:
#    https://docs.trafficserver.apache.org/records.config#ram-cache
#    https://docs.trafficserver.apache.org/en/latest/admin-guide/files/storage.config.en.html
##############################################################################

#这个地方定义内存缓存空间 我只有2G内存 所以就设置20M了 内存肯定是越大越好来
CONFIG proxy.config.cache.ram_cache.size INT 20M
CONFIG proxy.config.cache.ram_cache_cutoff INT 4194304
    # https://docs.trafficserver.apache.org/records.config#proxy-config-cache-limits-http-max-alts
CONFIG proxy.config.cache.limits.http.max_alts INT 5
    # https://docs.trafficserver.apache.org/records.config#proxy-config-cache-max-doc-size
CONFIG proxy.config.cache.max_doc_size INT 0
CONFIG proxy.config.cache.min_average_object_size INT 8000

#日志 这里修改成3了 待会为了格式化日志
CONFIG proxy.config.log.logging_enabled INT 3 
CONFIG proxy.config.log.max_space_mb_for_logs INT 5000
CONFIG proxy.config.log.max_space_mb_headroom INT 1000
CONFIG proxy.config.log.rolling_enabled INT 1
CONFIG proxy.config.log.rolling_interval_sec INT 86400
CONFIG proxy.config.log.rolling_size_mb INT 100
CONFIG proxy.config.log.auto_delete_rolled_files INT 1
CONFIG proxy.config.log.periodic_tasks_interval INT 5
##############################################################################
# These settings control remapping, and if the proxy allows (open) forward proxy or not. Docs:
#    https://docs.trafficserver.apache.org/records.config#url-remap-rules
#    https://docs.trafficserver.apache.org/en/latest/admin-guide/files/remap.config.en.html
##############################################################################
CONFIG proxy.config.url_remap.remap_required INT 1
    # https://docs.trafficserver.apache.org/records.config#proxy-config-url-remap-pristine-host-hdr
CONFIG proxy.config.url_remap.pristine_host_hdr INT 1
    # https://docs.trafficserver.apache.org/records.config#reverse-proxy
CONFIG proxy.config.reverse_proxy.enabled INT 1
##############################################################################
# SSL Termination. Docs:
#    https://docs.trafficserver.apache.org/records.config#client-related-configuration
#    https://docs.trafficserver.apache.org/en/latest/admin-guide/files/ssl_multicert.config.en.html
##############################################################################
CONFIG proxy.config.ssl.client.verify.server INT 0
CONFIG proxy.config.ssl.client.CA.cert.filename STRING NULL
CONFIG proxy.config.ssl.server.cipher_suite STRING ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:DHE-RSA-AES256-GCM-SHA384:DHE-DSS-AES256-GCM-SHA384:DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:ECDHE-ECDSA-AES256-SHA384:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA:ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES128-SHA:DHE-RSA-AES256-SHA256:DHE-DSS-AES256-SHA256:DHE-RSA-AES128-SHA256:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA:DHE-DSS-AES256-SHA:DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA:AES256-GCM-SHA384:AES128-GCM-SHA256:AES256-SHA256:AES128-SHA256:AES256-SHA:AES128-SHA:DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA
##############################################################################
# Debugging. Docs:
#    https://docs.trafficserver.apache.org/records.config#diagnostic-logging-configuration
##############################################################################
CONFIG proxy.config.diags.debug.enabled INT 0
CONFIG proxy.config.diags.debug.tags STRING http.*|dns.*
# ToDo: Undocumented
CONFIG proxy.config.dump_mem_info_frequency INT 0
CONFIG proxy.config.http.slow.log.threshold INT 0

CONFIG proxy.config.http_ui_enabled INT 3
CONFIG proxy.config.http.enable_http_info INT 1
CONFIG proxy.config.http.server_max_connections INT 0
CONFIG proxy.config.http.origin_max_connections INT 0
CONFIG proxy.config.http.cache.ignore_accept_mismatch INT 1
CONFIG proxy.config.http.cache.ignore_accept_language_mismatch INT 1
CONFIG proxy.config.http.cache.ignore_accept_encoding_mismatch INT 1
CONFIG proxy.config.http.cache.ignore_accept_charset_mismatch INT 1
CONFIG proxy.config.http.insert_squid_x_forwarded_for INT 1
CONFIG proxy.config.http.anonymize_insert_client_ip INT 1
CONFIG proxy.config.log.logfile_dir STRING /var/log/trafficserver

#定义证书的位置配合 ssl_multicert.config  使用 不加证书可以注释掉这几行
CONFIG proxy.config.ssl.server.ticket_key.filename STRING NULL
CONFIG proxy.config.ssl.server.cert.path STRING /etc/trafficserver/certs
CONFIG proxy.config.ssl.server.private_key.path STRING /etc/trafficserver/certs

logging.config 配置文件 路径 cd /etc/trafficserver/

vim logging.config 
增加 
log.ascii {
  Format = squid,
  Filename = 'squid',
  RollingEnabled = 1,
  RollingIntervalSec = 86400,
  RollingOffsetHr = 0
}

storage.config 配置文件 路径 cd /etc/trafficserver/

这里是指定ats文件缓存的位置 我这里设置200M缓存 正式环境越多越好

/home/ats/cache  200M 

remap.conf 配置文件 路径 cd /etc/trafficserver/ 这个是配置反向代理的

vim remap.conf
增加下面的内容
map http://test.com http://127.0.0.1:81

保存配置文件,使用命令 trafficserver restart 重启ATS

有些站点是需要跨域的 所以设置下跨域 默认是使用插件设置跨域的

首先增加一个配置文件在 /etc/trafficserver/下面

vim add_header.config 
填入下面的内容
add-header Access-Control-Allow-Origin *
#这样添加 如果源站也有跨域属性的话 会出现 Access-Control-Allow-Origin *,* 这种问题 为了避免
所以增加一步

然后编辑plugin.config
vim plugin.config
增加下面一行
header_rewrite.so add_header.config

然后重载配置文件 清除浏览器缓存 清除ats缓存
就可以看到这个跨域加上了

如果源站也有 跨域的 header 会出现的问题

所以继续修改 add_header.config

vim add_header.config 
将 add_header改为下面这样 但是我就是简单测试了下 正式环境 建议仔细测试
rm-header Access-Control-Allow-Origin *
add-header Access-Control-Allow-Origin *

以上ATS配置及缓存规则仅用于参考,推荐直接使用 OneClickCDN (你只需要参考文章的缓存规则即可)一键ATS自建CDN节点脚本,一键设置SSL, 您可以提供您自己的SSL证书,或者也可以一键签发免费的Let’s Encrypt证书,并且开启OCSP装订。

脚本还支持随时增添新的CDN网站。您只需要根据脚本引导,输入网址和源站IP地址,即可分分钟添加CDN网址。您可以使用脚本管理CDN网站,查看网站数据,清除缓存,等等。

未经允许不得转载:VPS测评网 » ATS配置及缓存规则Apache Traffic Server详细教程
评论 (0)