Can the "enable" directive be used in the top nginx block?

My site uses two application servers, namely app1 and app2, so in the configuration I have something like this:

upstream cluster { server app1:8080; server app2:8080; } 

Since every time I update the code, I need to restart both server processes and I want the service to be unperturbed, I will do the following manually:

  • Comment app1 in the top block to change it to:

     upstream cluster { #server app1:8080; server app2:8080; } 
  • Run nginx -s reload

  • Update the code in app1 and restart the server program, and then uncomment app1 in the top block

  • Take steps 1-3 for app2

I want to write a script to save this tedious job, so I hope to do the following:

  • You have a folder called "available" that contains app1.conf and app2.conf in the form as

     server app1:8080; 
  • You have another folder called "enabled" which contains indirect links app1.conf and app2.conf

  • Change the overlying cluster to

     upstream cluster { include /usr/local/nginx/conf/enabled/*; } 

    Therefore, every time I need to disconnect any application server, I can simply delete the corresponding soft link from the "included" folder, and later it can be restored by running ln -s

However, this approach did not help, since I received an error message from nginx:

[emerg]: the "include" directive is not allowed here ....

Is it possible that include cannot be placed in the top block? and I would suggest that I am not alone in this scenario, shutting down and allowing the server from time to time, how do other people usually deal with it?

+7
include nginx
source share
2 answers

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 
+10
source share

Just put it here if we can help others:

The Include directive can be used in the upstream block in later versions of nginx.

Example:

/etc/nginx/upstream.conf

 server ip:port; server ip:port; 

/etc/nginx/conf.d/default.conf

 upstream cluster { include /etc/nginx/upstream.conf; } server { listen 80; server_name localhost; location / { proxy_pass http://cluster; } error_page 500 502 503 504 /50x.html; location = /50x.html { root /usr/share/nginx/html; } } 
+3
source share

All Articles