当nginx的重新启动或配置测试突然发生错误时,应采取以下应对措施

只需一個選項:這份資料的目的是什麼?

    • nginxの再起動を行う際にエラーが発生した。

 

    別環境でも発生したことがあるので、対応内容をここに整理する。

现象可以用中文用以下方式表达:

    • nginxの再起動(configチェック含む)でエラーが発生した。

 

    原因と対処について、ここに整理する。

解决方案

事件

    nginxの再起動やコンフィグテストを行うとエラーが発生する。

造成这种情况的原因是什么。

    • エラーメッセージより、/var/tmp/nginx配下のディレクトリが存在しないことが原因。

なんらかの原因で削除されてしまう為、ある時起動などできなくなる

■Ansible Playbookのエラーメッセージ
TASK [xxx : nginx設定ファイルの文法チェック] **********************************************************
fatal: [xxx]: FAILED! => {"changed": true, "cmd": ["/usr/local/nginx/sbin/nginx", "-t", "-c", "/etc/nginx/nginx.conf"], "delta": "0:00:00.126446", "end": "2020-04-22 12:09:35.651695", "msg": "non-zero return code", "rc": 1, "start": "2020-04-22 12:09:35.525249", "stderr": "nginx: the configuration file /etc/nginx/nginx.conf syntax is ok\nnginx: [emerg] mkdir() \"/var/tmp/nginx/client/\" failed (2: No such file or directory)\nnginx: configuration file /etc/nginx/nginx.conf test failed", "stderr_lines": ["nginx: the configuration file /etc/nginx/nginx.conf syntax is ok", "nginx: [emerg] mkdir() \"/var/tmp/nginx/client/\" failed (2: No such file or directory)", "nginx: configuration file /etc/nginx/nginx.conf test failed"], "stdout": "", "stdout_lines": []}
    to retry, use: --limit @/home/ansible/xxx/xxx.retry

解决办法

    手動で必要なディレクトリを作成する。
/var/tmpへ移動する
# cd /var/tmp

nginxディレクトリが存在しない場合、作成する
# mkdir nginx

nginxディレクトリへ移動する
# cd nginx

client / fcgi / proxyディレクトリが存在しない場合、作成する
# mkdir client fcgi proxy

fcgi / proxyディレクトリのパーミッションを変更する
# chmod 700 fcgi/ proxy/

client / fcgi / proxyディレクトリのオーナーを変更する
# chown nginx:root fcgi/ proxy/ client/

以下のような設定になっていれば完了、コンフィグテストを際実行する
[root@xxx nginx]# ls -ltr
合計 12
drwx------ 2 nginx root 4096  4月 22 12:12 2020 proxy
drwx------ 2 nginx root 4096  4月 22 12:12 2020 fcgi
drwxr-xr-x 2 nginx root 4096  4月 22 12:12 2020 client
[root@xxx nginx]#