MeWrite Docs

nginx: [emerg] unknown directive

Nginx設定ファイルの構文エラー

概要

nginx: [emerg] unknown directive は、Nginxの設定ファイルに構文エラーがある場合に発生するエラーです。設定のテストや再起動時に検出されます。

エラーメッセージ

nginx: [emerg] unknown directive "sever" in /etc/nginx/nginx.conf:10
nginx: [emerg] unexpected "}" in /etc/nginx/nginx.conf:25
nginx: [emerg] invalid parameter "on" in /etc/nginx/nginx.conf:15
nginx: [emerg] "location" directive is not allowed here in /etc/nginx/nginx.conf:30

原因と解決策

1. ディレクティブ名のタイプミス

1
2
3
4
5
6
7
8
9
# NG
sever {  # server のタイプミス
    listen 80;
}

# OK
server {
    listen 80;
}

2. セミコロンの欠落

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
# NG
server {
    listen 80      # セミコロンがない
    server_name example.com;
}

# OK
server {
    listen 80;
    server_name example.com;
}

3. 括弧の不一致

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# NG
server {
    listen 80;
    location / {
        root /var/www/html;
    # } が足りない
}

# OK
server {
    listen 80;
    location / {
        root /var/www/html;
    }
}

4. ディレクティブの配置ミス

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
# NG: locationはhttpブロック直下には置けない
http {
    location / {
        root /var/www/html;
    }
}

# OK: serverブロック内に配置
http {
    server {
        location / {
            root /var/www/html;
        }
    }
}

5. モジュールが必要なディレクティブ

1
2
3
4
5
# NG: ngx_http_rewrite_moduleがない場合
rewrite ^/old$ /new permanent;

# モジュールがロードされているか確認
# nginx -V 2>&1 | grep -o with-http_rewrite_module

6. バージョン非対応のディレクティブ

1
2
3
4
5
# Nginx 1.19以降のみ
ssl_reject_handshake on;

# バージョン確認
# nginx -v

設定テスト方法

1
2
3
4
5
6
7
8
# 設定ファイルの構文テスト
nginx -t

# 詳細な設定確認
nginx -T

# 特定の設定ファイルをテスト
nginx -t -c /etc/nginx/nginx.conf

よくある間違い

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
# NG: クォートの不一致
root "/var/www/html;

# OK
root "/var/www/html";

# NG: パスの末尾スラッシュ
location /api/ {
    proxy_pass http://backend;  # /api/foo → http://backend/foo
}

# location /api {
    proxy_pass http://backend/;  # /api/foo → http://backend//foo
}

設定例(正しい構造)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# nginx.conf
user nginx;
worker_processes auto;

events {
    worker_connections 1024;
}

http {
    include /etc/nginx/mime.types;
    default_type application/octet-stream;

    server {
        listen 80;
        server_name example.com;

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

        location /api {
            proxy_pass http://localhost:3000;
        }
    }
}

関連エラー

関連エラー

Nginx の他のエラー

最終更新: 2025-12-17