Skip to content

Instantly share code, notes, and snippets.

@agustik
Last active August 29, 2015 14:08
Show Gist options
  • Save agustik/55fdd9bb6f4af076e660 to your computer and use it in GitHub Desktop.
Save agustik/55fdd9bb6f4af076e660 to your computer and use it in GitHub Desktop.
Nginx handy control script
#!/bin/bash
# variable in [] is optional
# cache
# flush [name]
# all
# size
# status [name]
# cert
# create [name]
# site
# enable
# disable
cache_path=/var/www/nginx_cache/*
action=$2
site=$3
case "$1" in
cache) #Cache
case "$2" in
flush)
if [ "${3}" == "all" ]; then
echo "flushing all cache";
$(rm -rf ${cache_path})
elif [ "${3}" == "key" ]; then
echo "Flushing key ${4}";
string=$4
if [[ "${string}" == "hash" ]]; then
md5=$5
else
md5=$(echo -n ${string} | md5sum | awk '{ print $1}' );
fi
length=$(echo ${#md5});
d1=$(echo "${length} - 1" | bc);
d2=$(echo "${length} - 3" | bc);
p1=$(echo ${md5:${d1}:1} );
p2=$(echo ${md5:${d2}:2} );
cache="${cache_path}${p1}/${p2}/${md5}"
if [ ! -f "${cache}" ]; then
echo "File not found!"
else
echo "File found";
size=$(du -h "${cache}" | awk '{print $1}');
rm -f "${cache}"
echo "Cache flushed, size: ${size}";
fi
else
echo "flushing ${3}";
$(grep -lr '$3' ${cache_path} | xargs rm)
fi
;;
size)
echo "Getting size";
size=$(du -c ${cache_path}|grep total);
size=$(echo $size | awk '{print $1 }');
maxsize=$(grep 'max_size' /etc/nginx/nginx.conf | grep -Po '([0-9]{2,4})');
size=$(expr $size / 1024);
pr=$(echo "scale=2; ${size}*100/${maxsize}" | bc);
echo "---------------------------"
echo "Current size: ${size}m"
echo "Maxsize: ${maxsize}m"
echo "Percentage: ${pr}%"
echo "---------------------------"
;;
status)
if [[ -z "${3}" ]]; then
log="/var/log/nginx/*-access.log";
echo "Getting Cache status of all logs"
else
log="/var/log/nginx/${3}-access.log";
echo "Getting status for ${3}"
fi
total_hit=0
total_miss=0
hits=$(grep "HIT" $log | grep -Po '(200 [0-9]{1,10})' | grep -Po ' [0-9]{1,10}')
miss=$(grep -P "(MISS|BYPASS|EXPIRED)" $log | grep -Po '(200 [0-9]{1,10})' | grep -Po ' [0-9]{1,10}')
for i in $hits
do
total_hit=$(echo $total_hit + $i | bc);
done
for i in $miss
do
total_miss=$(echo $total_miss + $i | bc);
done
total_hit=$(echo $total_hit / 1024 | bc);
total_miss=$(echo $total_miss / 1024 | bc);
echo "${total_hit} kbs saved";
echo "${total_miss} kbs All"
echo "---------------------------"
grep -Po '(HIT|MISS|BYPASS|EXPIRED)' ${log} | grep -Po '(HIT|MISS|BYPASS|EXPIRED)' | sort | uniq -c | sort -r
echo "---------------------------"
;;
*) echo "Missing argument, size|flush [all|name]"
;;
esac
;;
cert) echo "Cert"
if [[ -z "${2}" ]]; then
echo "Website name needed, ex: www.examle.com";
else
echo "Creating SSL request for ${2}"
$(openssl req -new -newkey rsa:4096 -nodes -keyout ./${2}.key -out ./${2}.csr)
echo "Write to files in current dir";
fi
;;
site)
if [[ -z "${2}" ]]; then
echo "Action is needed, (disable|enable)"
exit 0;
fi;
if [[ -z "${3}" ]]; then
echo "Site is needed, ex: www.example.com.conf"
exit 0;
fi
if [ $action == "enable" ]; then
if [ ! -f "/etc/nginx/conf.d/sites-available/${site}" ]; then
echo "Site not found!"
exit 0;
else
echo "Adding ${site} to nginx site-enabled"
$(ln -s /etc/nginx/conf.d/sites-available/${site} /etc/nginx/conf.d/sites-enabled/${site})
fi
fi
if [ $action == "disable" ]; then
if [ ! -f "/etc/nginx/conf.d/sites-enabled/${site}" ]; then
echo "Site not found!"
exit 0;
else
echo "Removing ${site} from nginx site-enabled"
$(rm -f /etc/nginx/conf.d/sites-enabled/${site})
fi
fi
;;
*)
echo "----------------------------------";
echo " Missing operation, cache|cert|site";
echo " [] is optional"
echo " <var> is user defined"
echo "cache"
echo " nxctl cache, shows status and size of cache, and flush cache"
echo " nxctl cache status|size|flush [<name>|all|hash [md5 [<md5hash>]|<cachekey>]] ";
echo " ex: nxctl cache flush jquery.js"
echo "cert"
echo " nxctl cert, creates CSR in current directory"
echo " ex: nxctl cert example.com"
echo "site"
echo " nxctl site enable|disable <name of configfile.conf>"
echo " ex: nxctl site enable example.com.conf";
;;
esac
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment