bash 函数对 nginx 文件执行健全性检查

标签 bash nginx

我正在尝试编写一个函数,在将文件移动到 /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 中,但我想在移动之前执行完整性检查文件到正确的位置。

我的问题:

  1. 有没有一种方法可以使用 nginx 测试位于 /etc/nginx/site-available 之外的其他地方的配置文件?
  2. 应该对 nginx 文件执行什么样的健全性检查?

最佳答案

您尝试完整性检查的文件不是 nginx 配置文件,因此(可以理解)nginx -t 表示它们无效。 -c 标志需要“an alternative configuration file”,而不是单个 server block 。 server block 位于 http block 中。

如果您想运行 nginx -t,您需要向它传递一个正确的配置文件,该文件将包含您尝试修改的文件。正如 Etan Reisner 所建议的那样,您可以简单地编写一个包含您的文件的虚拟 nginx.conf,这样的事情可能会起作用(我没有在安装了 nginx 的机器上使用 atm,因此您可能需要添加更多 stub 指令):

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。您检查每次成功的配置迭代,然后如果出现任何问题,您可以轻松恢复到之前已知的良好配置。

关于bash 函数对 nginx 文件执行健全性检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32636077/

相关文章:

linux - 转储文本文件

bash - 为什么 bash 中的 ""Here strings"会更改其后任何内容的语法颜色?

后台的 Bash 作业

redirect - Nginx:301 从外部文件重定向

ruby-on-rails - 如何在 Nginx 中启用 TLS 1.2?

linux - 用于编辑目录中所有文件的 bash 脚本

html - 从 Unix shell 在电子邮件中嵌入动态 HTML 表

linux - 使用 Ansible 安装 Nginx 到没有互联网连接的目标机器

Wordpress 多站点 Nginx 子目录安装无法再找到数据库

nginx - nginx如何限制用户在一分钟内发出的请求数?