Skip to content

Instantly share code, notes, and snippets.

@jaygooby
Last active September 21, 2017 12:55
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 jaygooby/bfc63bb4deb48465caf330cf16245891 to your computer and use it in GitHub Desktop.
Save jaygooby/bfc63bb4deb48465caf330cf16245891 to your computer and use it in GitHub Desktop.
#!/bin/bash
#
# Do you need a cluster of http servers to test your nginx proxying set up?
# I did, so I wrote this. https://twitter.com/jaygooby
#
# Defaults to starting 5 servers on 127.0.0.1 ports 2000 - 2004
# You can optionally specify the starting port and the number of servers you need...
#
# START_PORT=8000 NUM_SERVERS=9 bash_http_cluster.sh
#
# Each server will only ever return the string "I am the server on port X", but that's
# by design; I don't care about the servers themselves, just how nginx communicates with them,
# because I'm testing different load balancing and proxying strategies and I needed a quick
# cluster to tear up and down.
#
# Needs work on the tear down!
# Invoke with -k to kill the cluster, but it's currently the rather ugly
# pkill -f "bash_http_cluster" && pkill -f "nc "
START_PORT=${START_PORT:-2000}
NUM_SERVERS=${NUM_SERVERS:-5}
NUM_SERVERS=$(($NUM_SERVERS - 1))
LAST_PORT=$(($START_PORT + $NUM_SERVERS))
while getopts ":k" opt; do
case $opt in
k) pkill -f "${0##*/}" && pkill -f "nc "
exit 0
;;
\?)
echo "Invalid option: -$OPTARG use -k to kill the cluster and just set START_PORT and NUM_SERVERS to configure the cluster" >&2
exit 1;
;;
esac
done
for x in $(seq $START_PORT 1 $LAST_PORT)
do
PORT=$x
RESPONSE="I am the server on port $PORT"
echo "starting 127.0.0.1:$PORT"
{ while :; do nc -l $PORT < <(echo -e "HTTP/1.1 200 OK\r\n\r\n$RESPONSE\r\n"); done } &
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment