IT系おじさんのチラシの裏
2018年10月~
当サイトの記事にはアフィリエイト広告のリンクが含まれる場合があります

nginxで長いサーバー名を使う時はserver_names_hashを設定する

以前、「Windows+nginx+PHPの環境でVirtualHostの設定をする」の記事を投稿してからだいぶ日が空いてしまいましたが、最近またWebサーバーにnginxを入れて環境構築しています。

以前、テスト用にVirtualHostを構築した際には問題なかったのですが、今回本格的にLAN内のWebサーバーをnginxへ切り替えようとしたら、サーバー名の長さで躓いたので(忘れっぽい自分のための)備忘録です。

多数のサーバー、もしくは長いサーバー名を使う場合は設定変更が必要

例えば、以下のようにnginx.confでサーバーを定義した場合、

server {
	listen       80;
	server_name  www.example.com.toolonghostname;
	root c:/www/example/;
}

↓こんなエラーが表示されます。

nginx: [emerg] could not build server_names_hash, you should increase server_names_hash_bucket_size: 32

サーバー名ハッシュを構築できないよ!server_names_hash_bucket_sizeを32より大きくしないとダメだよ!(意訳)だそうです。

…………いやそんなこと言われてもキミ、nginx.confの中にserver_names_hash_bucket_sizeなんて記述ないやん…と混乱したのですが、単に追加すれば良いだけみたい。

server_names_hash_bucket_sizeの設定

↓こんな感じ。

http {
	server_names_hash_bucket_size 64;

	#略

	server {
		listen       80;
		server_name  www.example.com.toolonghostname;
		root c:/www/example/;
	}
}

設定値の詳細についてはnginxのオンラインマニュアルの「Server names」にありました。

If a large number of server names are defined, or unusually long server names are defined, tuning the server_names_hash_max_size and server_names_hash_bucket_size directives at the http level may become necessary. The default value of the server_names_hash_bucket_size directive may be equal to 32, or 64, or another value, depending on CPU cache line size. If the default value is 32 and server name is defined as “too.long.server.name.example.org” then nginx will fail to start and display the error message:

多数のサーバー名が定義されていたり、異常に長いサーバー名が定義されている場合は、http レベルで server_names_hash_max_size および server_names_hash_bucket_size ディレクティブを調整してね(意訳)

ということらしい。

server_names_hash_bucket_size のデフォルト値は「CPU cache line size」に応じて32とか64に設定されている、とのことですが、CPUキャッシュラインサイズってなんだろ…。まさかCPUのL1キャッシュだとかL2キャッシュとか、たまに聞くアレ? そんなレベルまで最適化してるの?! さすが高速Webサーバーといったところでしょうか。

それはともかく、うちのしょっぼいWebサーバー(手のひらサイズのNUCです)ではデフォルト値が32だったため、明示的にserver_names_hash_bucket_size 64を追加したら、あっさり起動するようになりました。

まとめ

  • nginxのVirtualHostを設定していたら、「server_names_hash_bucket_sizeが足りないよ!」的なエラーが出てnginxを起動できなくなった。
  • nginxのマニュアルによればserver_names_hash_bucket_sizeのデフォルトサイズは環境によって異なり、うちではたまたま(?)32だったらしい。
  • nginx.confを編集し、http の設定の中に server_names_hash_bucket_size 64 を追記したらnginxが起動するようになった。

めでたし、めでたし。

ちなみに先述のマニュアルによると、

could not build the server_names_hash,
you should increase either server_names_hash_max_size: 512
or server_names_hash_bucket_size: 32

みたいなエラーの場合もあり、そのときはまず最初に server_names_hash_max_size を調整してね、とのことです。

関連記事

WindowsにMariaDBをインストールする

Googleをはじめ、Facebookやwikipedia等、大規模なWebサイトでもMariaDBが採用されており、CentOS、DebianなどのLinuxディストリビューションや有名な開発環境で

コメント

新しいコメントを投稿する

[新規投稿]
 
TOP