Skip to content

Instantly share code, notes, and snippets.

@ion1
Created February 21, 2014 06:14
Show Gist options
  • Save ion1/9129651 to your computer and use it in GitHub Desktop.
Save ion1/9129651 to your computer and use it in GitHub Desktop.
Configure my Traffic Server instance
#!/bin/sh
set -eu
# Configure my Traffic Server instance.
export LC_ALL=C
cd /etc/trafficserver
# Other portability issues would just raise runtime errors; this one would
# break config files.
if ! printf 'foo\n' | grep -Eq '^\<(f{1})(o+\>)$'; then
>&2 printf '%s\n' 'ERROR: grep -E does not seem to do the expected thing'
exit 1
fi
replace_line() {
local file pat line
file="$1"; shift
pat="$1"; shift
line="$1"; shift
(
grep -Ev "$pat" "$file" || :
printf '%s\n' "$line"
) | >/dev/null tee "$file.new"
chown trafficserver:trafficserver "$file.new"
chmod 644 "$file.new"
mv -f "$file.new" "$file"
}
service trafficserver stop
sleep 2 # cringe
replace_line cacheurl.config '/DEBIAN-MIRROR\.CACHEURL/' \
'http://ftp\.[0-9a-zA-Z-]+\.debian\.org/debian/(.*) http://DEBIAN-MIRROR.CACHEURL/$1'
replace_line cacheurl.config '/UBUNTU-MIRROR\.CACHEURL/' \
'http://(?:[0-9a-zA-Z-]+\.)?archive\.ubuntu\.com/ubuntu/(.*) http://UBUNTU-MIRROR.CACHEURL/$1'
replace_line plugin.config '^cacheurl\.so\>' \
'cacheurl.so /etc/trafficserver/cacheurl.config'
replace_line storage.config '^/var/cache/trafficserver\>' \
'/var/cache/trafficserver 2G'
replace_line logs_xml.config '^<LogFormat><Name = "squid_censored"/>' \
'<LogFormat><Name = "squid_censored"/><Format = "%<cqtq> %<ttms> %<chi> %<crc>/%<pssc> %<psql> %<cqhm> - - %<phr>/- %<psct> %<xid>"/></LogFormat>'
replace_line logs_xml.config '^<LogObject><Filename = "squid_censored"/>' \
'<LogObject><Filename = "squid_censored"/><Format = "squid_censored"/></LogObject>'
service trafficserver start
sleep 2 # cringe
# The via settings have four values
# 0 - Do not modify / set this via header
# 1 - Update the via, with normal verbosity
# 2 - Update the via, with higher verbosity
# 3 - Update the via, with highest verbosity
traffic_line -s proxy.config.http.insert_response_via_str -v 2
traffic_line -s proxy.config.http.insert_squid_x_forwarded_for -v 0
# when_to_revalidate has 5 options:
# 0 - default. use cache directives or heuristic
# 1 - stale if heuristic
# 2 - always stale (always revalidate)
# 3 - never stale
# 4 - always revalidate if request is conditional, else default is used
traffic_line -s proxy.config.http.cache.when_to_revalidate -v 4
# required headers: three options:
# 0 - No required headers to make document cachable
# 1 - "Last-Modified:", "Expires:", or "Cache-Control: max-age" required
# 2 - explicit lifetime required, "Expires:" or "Cache-Control: max-age"
traffic_line -s proxy.config.http.cache.required_headers -v 0
# enable the cache to read from an object while it is being added to the cache
traffic_line -s proxy.config.cache.enable_read_while_writer -v 1
traffic_line -s proxy.config.http.background_fill_active_timeout -v 0
traffic_line -s proxy.config.http.background_fill_completed_threshold -v 0
traffic_line -s proxy.config.log.logfile_perm -v 'rw-------'
traffic_line -s proxy.config.log.custom_logs_enabled -v 1
traffic_line -s proxy.config.log.squid_log_enabled -v 0
traffic_line -s proxy.config.reverse_proxy.enabled -v 0
# To enable forward proxy, you must turn off remap_required
traffic_line -s proxy.config.url_remap.remap_required -v 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment