我正在尝试编写一个函数,在将文件移动到/etc/nginx/site-available
之前对文件执行完整性检查。
位于我的主目录中并定期修改。
在这些文件中进行的唯一修改是添加server_name
。
他们看起来像:
server {
listen 80;
server_name domain.com;
server_name www.domain.com;
server_name mynsite1.com;
server_name www.mysite1.com;
server_name mysite2.com;
server_name www.mysite2.com;
server_name mysite3.com;
server_name www.mysite3.com;
server_name mysite4.com;
server_name www.mysite4.com;
access_log /var/log/nginx/domain.com-access.log main;
error_log /var/log/nginx/domain.com-error.log warn;
root /var/www/docroot;
index index.php index.html index.htm;
location / {
try_files $uri /app_dev.php;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location ~ \.php$ {
fastcgi_split_path_info ^(.+\.php)(/.+)$;
# NOTE: You should have "cgi.fix_pathinfo = 0;" in php.ini
fastcgi_pass 127.0.0.1:9000;
fastcgi_index index.php;
fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
include fastcgi_params;
}
# deny access to .htaccess files, if Apache's document root
# concurs with nginx's one
#
location ~ /\.ht {
deny all;
}
}
这是我现在的功能:
verify_nginx()
{
if [ ! -s "$file1" ]; then
echo "-> File \"$file1\" is empty or do not exist" | ts
exit 1
elif [ ! -s "$file2" ]; then
echo "-> File \"$file2\" is empty or do not exist" | ts
exit 1
fi
}
我还想在函数中添加nginx -t -c /homedir/file1
,但是我收到以下错误:
nginx: [emerg] "server" directive is not allowed here in /homedir/file:1 nginx: configuration file /homedir/file test failed
确实nginx -c
期待nginx.conf
在我的homedir中不包含我的文件。
我可以将文件放在/etc/nginx/site-available
中nginx.conf
中,但我想在将文件移动到正确的位置之前执行完整性检查。
我的问题:
/etc/nginx/site-available
测试位于nginx
以外的其他地方的配置文件?nginx
个文件执行哪种健全性检查?答案 0 :(得分:0)
您尝试进行完整性检查的文件不是nginx配置文件,因此(可以理解)nginx -t
表示它们无效。 -c
标记需要" an alternative configuration file",而不是单个server
块。 server
阻止http
阻止。
如果你想运行nginx -t
,你需要传递一个正确的配置文件,这将包含你试图修改的文件。正如Etan Reisner建议的那样,您可以简单地编写一个包含您的文件的虚拟nginx.conf
,这样的东西可能会起作用(我不在安装了nginx的机器上,因此您可能需要添加一些更多存根指令):
http {
include path/to/files/*;
}
然后你可以运行nginx -t -c dummy_nginx.conf
。
这有一个问题;你可能仍然有任何数量的错误只有在你的真实配置文件加载时才会显示。
相反,您只需在reload
之前调用nginx -t
,就可以在更改之前验证您的真实配置文件。如果你愿意,可以将它包装在一个bash函数中:
safe_reload() {
nginx -t &&
service nginx reload # only runs if nginx -t succeeds
}
您还应该拥有某种备份或还原机制。这可能就像将旧配置文件复制到并行*.bak
文件一样简单,但使用像Mercurial或Git这样的VCS更令人愉快。您检查成功的配置的每次迭代,然后如果出现任何问题,您可以轻松恢复到以前已知的良好配置。