Unfortunately, nginx cannot handle the include directive inside up. But you can use this script to manage your upstream servers:
somewhere in the http nginx.conf section:
include /usr/local/nginx/conf/upstream.conf
create an empty file:
touch /usr/local/nginx/conf/upstream.conf
use this script to manage upstream servers ( upstreamctl.sh ):
#!/bin/bash if [ -n "$1" -a -n "$2" ]; then action="$1"; target="$2"; else echo "Usage: $0 (add|rm) server:port" exit 0; fi; # Path to nginx binary BIN="/usr/local/nginx/sbin/nginx" # Path to upstream config file CONF="/usr/local/nginx/conf/upstream.conf" SERVERS=`cat $CONF | grep server` output="upstream cluster {" if [ $action == "add" ]; then echo -e "$output" > $CONF if $( echo $SERVERS | grep --quiet $target ); then echo "Warning: Server is already enabled." else SERVERS="$SERVERS\n\tserver $target;" fi echo -e "$SERVERS" >> $CONF echo "}" >> $CONF elif [ $action == "rm" ]; then sed -i "/$target/d" $CONF else echo "Unknown action" fi # Check changes: $BIN -t
In your case, you can run:
./upstreamctl.sh add app1:8080
and
./upstreamctl.sh rm app2:8080
Sergei Lomakov
source share