Skip to content

Instantly share code, notes, and snippets.

@mattrude
Last active February 10, 2019 20:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattrude/0cb858f0b5ca5d1677784b080b66bab5 to your computer and use it in GitHub Desktop.
Save mattrude/0cb858f0b5ca5d1677784b080b66bab5 to your computer and use it in GitHub Desktop.

start by creating the needed directorys and changing the permission to the nginx proccess (www-data on ubuntu)

mkdir -p /var/cache/nginx/sks /var/cache/nginx/temp
chown -R www-data:www-data /var/cache/nginx

Below is my config.

http {

...

    proxy_ignore_headers        Expires Cache-Control;
    proxy_cache_use_stale       error timeout updating http_500 http_502 http_503 http_504;
    proxy_cache_path            /var/cache/nginx/sks levels=1:2 keys_zone=keyserver:10m max_size=10g inactive=6h;
    proxy_temp_path             /var/cache/nginx/temp;
    
 ...
 
 }

Then in the Server secion, I have

        location /pks {
           proxy_cache                 keyserver;
           proxy_cache_use_stale       error timeout updating http_500 http_502 http_503 http_504;
           proxy_cache_valid           200 301 302 6h;
           proxy_cache_valid           404 502 503 504 1m;
           proxy_pass                  http://127.0.0.1:11371;
           add_header                  Via "1.1 keyserver.mattrude.com:11371 (nginx)";
           proxy_ignore_client_abort   on;
           client_max_body_size        8m;
       }
       
#!/bin/bash
if [ ! -f /var/lib/sks/cache.rrd ]; then
mkdir -p /var/lib/sks
rrdtool create /var/lib/sks/cache.rrd \
--start now \
--step 300 \
DS:cache:GAUGE:95040:U:U \
DS:hit:GAUGE:120:0:U \
DS:mis:GAUGE:120:0:U \
RRA:AVERAGE:0.5:1:8640 \
RRA:AVERAGE:0.5:12:9480 \
RRA:MIN:0.5:12:8640 \
RRA:MAX:0.5:12:8640
fi
CACHE=`du -bc /var/cache/nginx/sks/* |grep 'total' |awk '{ print $1 }'`
HIT=`grep "HIT$" /var/log/nginx/keyserver.log |wc -l`
MIS=`grep "MISS$" /var/log/nginx/keyserver.log |grep -v ' 404 1075 ' |wc -l`
TOT=`echo $(( $HIT + $MIS ))`
HITL=`awk -vn=$HIT "BEGIN{print(n/$TOT)}" |sed 's/0\.//g'`
MISL=`awk -vn=$MIS "BEGIN{print(n/$TOT)}" |sed 's/0\.//g'`
HITS=`echo ${HITL:0:2}`
MISS=`echo ${MISL:0:2}`
rrdtool update /var/lib/sks/cache.rrd N:${CACHE}:${HITS}:${MISS}
time_readible=$(echo $(date) | sed 's/\:/\\:/g')
hostname=`hostname -f`
mkdir -p /var/www/keyserver.mattrude.com/graphs/
for period in day week month year
do
rrdtool graph /var/www/keyserver.mattrude.com/graphs/cache-$period.png -s -1$period \
-t "Nginx Reverse Proxy Cache Size in MBytes for the last $period" -z \
-c "BACK#FFFFFF" -c "SHADEA#FFFFFF" -c "SHADEB#FFFFFF" \
-c "MGRID#AAAAAA" -c "GRID#CCCCCC" -c "ARROW#333333" \
-c "FONT#333333" -c "AXIS#333333" -c "FRAME#333333" \
-h 250 -w 700 -l 0 -a PNG -v "MegaBytes" \
DEF:cache=/var/lib/sks/cache.rrd:cache:LAST \
VDEF:mincache=cache,MINIMUM \
VDEF:maxcache=cache,MAXIMUM \
VDEF:avgcache=cache,AVERAGE \
VDEF:lstcache=cache,LAST \
VDEF:totcache=cache,TOTAL \
"COMMENT: \l" \
"COMMENT: " \
"COMMENT:Minimum " \
"COMMENT:Maximum " \
"COMMENT:Average " \
"COMMENT:Current \l" \
"COMMENT: " \
"AREA:cache#204d74:Cache " \
"LINE1:cache#337ab7" \
"GPRINT:mincache:%5.1lf %sB " \
"GPRINT:maxcache:%5.1lf %sB " \
"GPRINT:avgcache:%5.1lf %sB " \
"GPRINT:lstcache:%5.1lf %sB \l" \
"COMMENT:Server\: $hostname\u" \
"COMMENT:Built\: $time_readible\r" > /dev/null
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment