kdnakt blog

hello there.

nginxのincludeされた設定ファイルをテストする

Netcraftの調査では、2022年2月時点で、調査対象500万超のサイトのなかで、Nginxが31%、Apacheが24%利用されているらしい。

f:id:kidani_a:20220311131326p:plain

news.netcraft.com

 

 

[シンプルな設定ファイルのテスト]

nginxコマンドは-tオプションを利用して、設定ファイルのテストを行うことができる。

www.nginx.com

 

実行環境は以下の通り。

% sw_vers           
ProductName:	macOS
ProductVersion:	12.2.1
BuildVersion:	21D62

% nginx -v
nginx version: nginx/1.21.6

 

次のようなシンプルな設定ファイルを用意する。

% cat nginx.conf 
events {}
  
http {  
  server {  
    listen 80;
    server_name localhost;  
  
    return 200 "Hello";  
  }  
}  

 

このファイルでnginxを起動してリクエストを投げると、以下のようなレスポンスを得ることができる。

% nginx -c $(pwd)/simple.conf
% curl localhost
Hello

 

設定ファイルのテストは次のように実行できる。

% nginx -t -c $(pwd)/nginx.conf
nginx: the configuration file /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/nginx.conf syntax is ok
nginx: configuration file /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/nginx.conf test is successful

 

次に、失敗するケースを見てみる。先ほどのnginx.confの1行目の}を消して、構文エラーとなるbad.confを以下のように用意する。

% cat bad.conf 
events {
  
http {  
  server {  
    listen 80;
    server_name localhost;  
  
    return 200 "Hello";  
  }  
}

 

このファイルに対してテストコマンドを実行すると、以下のように失敗する。

% nginx -t -c $(pwd)/bad.conf  
nginx: [emerg] "http" directive is not allowed here in /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/bad.conf:3
nginx: configuration file /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/bad.conf test failed

 

[includeされた設定ファイルのテスト]

Nginxは設定ファイルを入れ子構造にすることができる。

% cat including.conf 
events {}
  
http {  
  server {  
    listen 80;
    server_name localhost;  

    include conf/included.conf;
  }  
}  

% cat conf/included.conf 
location /included {
  return 200 "included";
}

 

この設定ファイルでnginxを起動すると次のようにレスポンスが返ってくる。

% nginx -c $(pwd)/including.conf
% curl http://localhost/included
included

 

このファイルをテストすると以下のようになる。

% nginx -t -c $(pwd)/including.conf
nginx: the configuration file /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/including.conf syntax is ok
nginx: configuration file /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/including.conf test is successful

 

▼includeされたファイルの構文エラーチェック

ところで、nginxコマンドの-tオプションの説明には、次のようにある。

Don’t run, just test the configuration file. NGINX checks configuration for correct syntax and then try to open files referred in configuration.

「参照されているファイルを開こうとする」とあるが、includeされたファイルの構文もテストされているだろうか、と気になったのでテストしてみた。

 

次のような設定ファイルを用意する。先ほどのincluded.confから}を削除して、構文エラーとなるように修正してある。

% cat including-broken.conf 
events {}
  
http {  
  server {  
    listen 80;
    server_name localhost;  

    include conf/included-broken.conf;
  }  
}  

% cat conf/included-broken.conf 
location /included {
  return 200 "included";

 

これをテストすると、次のように失敗する。ちゃんとincludeされたファイルを開いて、そちらの構文エラーもチェックしてくれているようだ。

% nginx -t -c $(pwd)/including-broken.conf
nginx: [emerg] unexpected end of file, expecting "}" in /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/conf/included-broken.conf:4
nginx: configuration file /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/including-broken.conf test failed

 

▼includeされたファイルの存在チェック

ちなみに、includeされたファイルがない場合もテストが失敗する。

% cat including-nonexistent.conf 
events {}
  
http {  
  server {  
    listen 80;
    server_name localhost;  

    include conf/nonexistent.conf; // このファイルは存在しない
  }  
}  

% nginx -t -c $(pwd)/including-nonexistent.conf
nginx: [emerg] open() "/Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/conf/nonexistent.conf" failed (2: No such file or directory) in /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/including-nonexistent.conf:8
nginx: configuration file /Users/kdnakt/github/kdnakt/sandbox/nginx-sandbox/including-nonexistent.conf test failed

 

[まとめ]

  • nginx -t -c 設定ファイルのパスコマンドでnginxの設定ファイルをテストできる
  • 設定ファイル内でincludeディレクティブで他の設定ファイルを読み込んでいる場合、読み込まれたファイルもテスト対象となる
  • サンプルコードは以下のリポジトリにまとめてある

github.com