できない.dev

nginx「403 Forbidden」の原因と直し方

403 Forbidden は、error.log の理由を読み、index ファイルを置くかパーミッションを直せば消える。
ディレクトリに index.html が無いときと、nginx のワーカーがファイルやディレクトリを読めないときに返る。

公開: 更新:

症状できない
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/docs/
403
解決後できた
curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/docs/
200

2026-11-17 に nginx:1.27 で実際に打って取った出力。検証環境の詳細

要約

nginx で静的ファイルを配信しているページを開くと、本文に 403 Forbidden とだけ書かれたページが返る。

<html>
<head><title>403 Forbidden</title></head>
<body>
<center><h1>403 Forbidden</h1></center>
<hr><center>nginx/1.31.8</center>
</body>
</html>

403 Forbidden は、nginx がそのパスの中身を返さないと決めたときの応答である。
理由はブラウザには出ず、error.log(公式の Docker イメージでは docker logs)にだけ出る。
error.log の理由を読み、index ファイルを置くかパーミッションを直せば消える。
nginx 1.31.8 の公式イメージで index とパーミッションの 403 を起こすと、理由は次の 3 種類だった。

[error] 16#16: *1 directory index of "/usr/share/nginx/html/docs/" is forbidden, client: 127.0.0.1, server: , request: "GET /docs/ HTTP/1.1", host: "127.0.0.1"
[error] 17#17: *3 open() "/usr/share/nginx/html/secret.html" failed (13: Permission denied), client: 127.0.0.1, server: , request: "GET /secret.html HTTP/1.1", host: "127.0.0.1"
[error] 21#21: *4 "/srv/private/index.html" is forbidden (13: Permission denied), client: 127.0.0.1, server: , request: "GET /private/ HTTP/1.1", host: "127.0.0.1"

1 行目は index ファイルが無いとき、2 行目と 3 行目は nginx のワーカーがファイルやディレクトリを読めないときに出る。

実行例

nginx:1.27 のコンテナで、index の無いディレクトリと権限を絞ったファイルを配信すると、4 つの URL がすべて 403 を返した。
error.log には 3 種類の理由が出ており、index の追加・index ディレクティブの指定・chmod の後は 200 に変わった。

$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/docs/
403
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/app/
403
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/secret.html
403
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/private/
403
$ grep -o '\[error\].*, client' error.log
[error] 18#18: *1 directory index of "./site/docs/" is forbidden, client
[error] 18#18: *2 directory index of "./site/app/" is forbidden, client
[error] 18#18: *3 open() "./site/secret.html" failed (13: Permission denied), client
[error] 18#18: *4 "./private/index.html" is forbidden (13: Permission denied), client
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/docs/
200
$ nginx -p . -c nginx.conf -t
nginx: the configuration file ./nginx.conf syntax is ok
nginx: configuration file ./nginx.conf test is successful
$ nginx -p . -c nginx.conf -s reload
2026/11/16 20:55:35 [notice] 28#28: signal process started
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/app/
200
$ chmod 644 site/secret.html
$ chmod 755 private
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/secret.html
200
$ curl -s -o /dev/null -w '%{http_code}\n' http://127.0.0.1/private/
200

— 2026-11-17 時点の出力

検証環境2026-11-17 検証
検証日
実行環境
nginx:1.27 Debian GNU/Linux 12 (bookworm)

この記事の「実行例」は、上記の環境で実際にコマンドを実行して得られた出力をそのまま掲載しています。 再現手順はリポジトリの検証スクリプトとして管理し、定期的に再実行して出力を更新しています。

解決策

1. index ファイルを置くか、一覧を出す

そのディレクトリに index.html を置く。
ファイルの一覧を見せるのが目的なら、その location で autoindex を有効にする(ngx_http_autoindex_module(新しいタブで開く))。

location /docs/ {
    autoindex on;
}

2. index ディレクティブに実際の名前を書く

location /app/ {
    index main.html;
}

書き換えたあとは nginx -t で設定を確かめ、nginx -s reload で読み込み直す。
どちらの location も、reload のあとは 200 が返った。

3. パーミッションを直す

chmod 644 /usr/share/nginx/html/secret.html
chmod 755 /srv/private

ファイルには読み取り権(r)が、そこまでのディレクトリすべてには実行権(x)が要る。
上の 2 つを直すと、/secret.html と /private/ も 200 になった。
どこで止まっているかは、namei -l にファイルのパスを渡すと、途中のディレクトリの権限を 1 行ずつ確かめられる。

4. deny の規則を見直す

error.log が access forbidden by rule なら、そのパスに当たる location の allow・deny を読む。
そのクライアントに見せてよいなら、規則を外すか、deny より前に allow を足して reload する。

よくある原因

  1. ディレクトリに index.html が無い。
    /docs/ のように / で終わる URL では、nginx はそのディレクトリの index ファイルを探す。
    見つからず、一覧を出す autoindex も既定の off のままだと、directory index of "..." is forbidden の 403 になる。
  2. index ファイルの名前が違う。
    index ディレクティブの既定は index.html である(ngx_http_index_module(新しいタブで開く))。
    main.html しか置いていない /app/ も、1 と同じメッセージの 403 になった。
  3. ファイルを読めない。
    ワーカーは nginx.conf の user 行の利用者で動き、公式イメージでは user nginx; だった。
    root が所有する 600 のファイルは、この利用者からは読めず open() ... failed (13: Permission denied) になる。
  4. ディレクトリに入れない。
    alias で指した /srv/private/ が 700 だと、中の index.html が 644 でも "/srv/private/index.html" is forbidden (13: Permission denied) になった。
  5. deny の規則に当たっている。
    location に deny all; や allow・deny の並びがあると、error.log は access forbidden by rule の 403 になる。

RHEL 系で SELinux が有効なときは、パーミッションが正しくても同じ (13: Permission denied) になる。
ここでは確かめていない。