Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> 電腦軟體教學 >> 伺服器技術 >> 關於伺服器

Nginx 403 Forbidden 錯誤排查:權限缺失與索引配置缺失兩大主因

編輯:關於伺服器

403 觸發場景 (正常預期行為)

  • IP 黑名單/白名單限制 (deny/allow)
  • 目錄瀏覽關閉 (autoindex off) 且無索引檔
  • 內部網路限制 (allow 10.0.0.0/8; deny all;)

配置錯誤導致的非預期 403

原因一:檔案系統權限不足 (最常見)

Nginx 執行用戶 (如 www-data, nginx) 需要:

  • 目標檔案 **讀取權限** (644)。
  • **所有父目錄 執行權限** (755),以便遍歷進入。

範例:訪問 /usr/local/nginx/html/image.jpg 需:

chmod 755 / /usr /usr/local /usr/local/nginx /usr/local/nginx/html
chmod 644 /usr/local/nginx/html/image.jpg

原因二:index 指令缺少預設首頁檔

配置:

location / {
    root /var/www/html;
    index index.html index.htm;  # 缺少 index.php
}

若目錄下只有 index.php,Nginx 找不到匹配的 index 檔,直接回 403。

修正: index index.html index.htm index.php;

原因三:PHP/JSP/Python 等動態語言未加入 index

同理需補充:index.jsp, index.py 等。

排查檢查清單

  1. nginx -t 確認語法無誤。
  2. ls -l 檢查目標檔案與所有父目錄權限。
  3. ps aux | grep nginx 確認 worker 進程用戶。
  4. 錯誤日誌 error.log 關鍵字 Permission denied 或 directory index of ... is forbidden。
Copyright © Windows教程網 All Rights Reserved