0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Docker + Nginx でのエラー「"server" directive is not allowed here」の解決メモ

Posted at

エラー内容

Dockerを使ってNginxコンテナを起動(docker container run)しようとした際に、以下のようなエラーメッセージが表示されてコンテナが起動しない、という状況に遭遇しました。

[emerg] "server" directive is not allowed here

エラーの原因

Nginx設定ファイルの構造ミス

Nginx設定ファイル「nginx.conf」では、特定のWebサイト(バーチャルホスト)の設定を記述する server { ... } ブロックは、必ず http { ... } ブロックの中に記述しなければならないようです。

修正前のファイル

server{
    Listen 80;

    location / {
        root /var/www;
        index index.html index.htm;
        }
}

修正後のファイル

  • server ブロックの配置: server { ... } ブロック全体を、http { ... } ブロックの内側に移動させます。
  • events ブロックの追加: Nginxの設定には、http ブロックとは別に events { ... } ブロックが必須です。
events {
    worker_connections 768; # 最小限の値 (適当な値)
}

    http {
        server {
            listen 80; # 小文字にし、セミコロンを追加
            
            location / {
                root /var/www;
                index index.html index.htm;
                }
        }
    }

イメージの再ビルドとコンテナの実行

修正したファイルを保存し、イメージの再ビルドとコンテナを実行します。

docker image build . -t my-custom-nginx:latest
docker container run -p 8080:80 my-custom-nginx:latest

以下のように表示され、Nginxが正常に起動しました🎉

/docker-entrypoint.sh: Configuration complete; ready for start up
172.17.0.1 - - [06/Apr/2025:06:50:42 +0000] "GET / HTTP/1.1" 200 138 "-" "..."
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?