You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Nginx配置:用户访问错误子目录时返回404而非403的方法

解决Nginx访问错误子目录返回404而非403的配置方法

要让Nginx在用户访问错误子目录时返回404错误而非403,只需调整error_page指令,将403错误映射到404页面并强制返回404状态码,具体配置如下:

关键修改点

添加或调整error_page规则,把403错误和404错误统一指向你的404页面,同时指定返回404状态码:

error_page 403 404 =404 /404.html;

这里的=404是核心,它会强制Nginx向客户端返回404的HTTP状态码,而不是默认的重定向状态码,确保浏览器识别为404错误。

调整后的完整配置

server {                                                                                                                                                                               
    listen 80;                                                                                                                                                                     
    index index.html;                                                                                                                                                              
    server_name localhost;

    location / {                                                                                                                                                                   
            #add_header "Access-Control-Allow-Origin" *;                                                                                                                           
            proxy_set_header X-Forwarded-Host $host;                                                                                                                               
            proxy_set_header X-Forwarded-Server $host;                                                                                                                             
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;                                                                                                           
            proxy_set_header Host $http_host;                                                                                                                                      
            proxy_pass  http://10.10.66.66:8080;
    }                                                                                                                                                                              

    # 映射403、404错误到404.html,返回404状态码
    error_page 403 404 =404 /404.html;                                                                                                                                                     
    location = /404.html {
        root /path/to/your/static/files; # 替换为404.html所在的实际目录
    }                                                                                                                                                                              

    error_page 500 502 503 504 /50x.html;                                                                                                                                          
    location = /50x.html {
        root /path/to/your/static/files; # 替换为50x.html所在的实际目录
    }                                                                                                                                                                              
}

注意事项

  1. 确保location = /404.html中配置了正确的root路径,指向存放404.html的目录,否则Nginx无法找到错误页面,会返回默认错误。
  2. 如果你原本的404.html是由后端服务提供的,可以在location = /404.html内配置proxy_pass指向后端地址,替代root指令。
  3. 若你的场景是Nginx静态目录无索引文件导致的403,确认autoindex off;(默认已开启),避免目录列表功能触发403错误。

内容的提问来源于stack exchange,提问作者Ali farahzadi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.04 05:35:30