可以“包括”指令在nginx的上游块中使用? [英] Can "include" directive be used within upstream block of nginx?
问题描述
我的网站使用两个app服务器,即app1和app2,所以在配置中我有这样的东西:
My site uses two app server, 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 undisturbed, I will follow these steps manually:
-
评论
app1
在上游块内,以便将其修改为:
Comment
app1
within upstream block so to modify it into:
upstream cluster {
#server app1:8080;
server app2:8080;
}
运行 nginx -s reload
更新代码 app1
并重新启动服务器程序,然后取消注释<上游块中的code> app1
Update code at app1
and restart the server program, and then uncomment app1
in upstream block
执行 app2的步骤1-3
我希望编写一个脚本来完成这项繁琐的工作,所以我希望做的是这个:
I wish to write a script to spare this tedious work, so what I hope to do is this:
-
有一个名为available的文件夹,其中包含
app1.conf
和app2.conf
格式为
server app1:8080;
让另一个名为enabled的文件夹包含 app1.conf
和 app2.conf
修改上游集群进入
upstream cluster {
include /usr/local/nginx/conf/enabled/*;
}
所以每次我需要禁用任何app服务器时我都可以删除相应的来自enabled文件夹的软链接,稍后可以通过运行 ln -s
So every time I need to disable any app server I can just remove the corresponding soft link from the "enabled" folder, and later can restore it by running ln -s
$ b来恢复它
$ b
然而这种方法效果不好,因为我收到了来自nginx的错误消息:
However this approach didn't work well as I got an error message from nginx saying:
[ emerg]:这里不允许使用include指令....
[emerg]: "include" directive is not allowed here in ....
是这样的 include
不能放入上游块?而且我想我在这种情况下并不孤单,有时禁用和启用服务器,其他人通常如何处理它?</ p>
Is that so include
cannot not be put into the upstream block? and I'd imagine I'm not alone in this kind of scenario, disabling and enabling server at times, how other folks normally deal with it?
推荐答案
不幸的是,nginx无法在上游内部处理include指令。
但您可以使用此脚本来管理上游服务器:
Unfortunately, nginx can't handle include directive inside upstream,. But you can use this script to manage your upstream servers:
在nginx.conf的http部分的某处:
somewhere in http section of nginx.conf:
include /usr/local/nginx/conf/upstream.conf
创建空文件:
touch /usr/local/nginx/conf/upstream.conf
使用此脚本管理上游服务器( upstreamctl.sh ):
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 may run:
./upstreamctl.sh add app1:8080
和
./upstreamctl.sh rm app2:8080
这篇关于可以“包括”指令在nginx的上游块中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!