如何从文档根而不是使用Nginx的子目录显示自定义404?

发布于 2025-01-25 15:45:20 字数 1034 浏览 2 评论 0原文

我有以下nginx.conf部分:

# valid url
location /foo/bar/ {
    proxy_pass http://my_server/foo/bar/;
}


# redirect base url to index.html
location = / {
    root   /usr/share/nginx/html;
    index  index.html;
}


# anything not listed above to 404
location / {
    return 404;
}

error_page 404 404.html;
error_page 500 502 503 504 50x.html;

当我启动nginx服务器并请求一条路由时,例如:https://my_server/blah/code>,我被正确地重定向到<<代码> https://my_server/404.html页面我可以看到我的自定义404。但是,如果我用子目录向URL提出另一个请求:https:https://my_server/blah/blah/blah/blah/blah/blah/blah/blah/blah/blah/blah/ baz,我的nginx服务器尝试查找https://my_server/blah/404.html

my_server-reverse_proxy-1 | 2022/05/02 23:05:35 [error] 7#7: *1 open() "/usr/share/nginx/html/blah/404.html" failed (2: No such file or directory), client: 11.11.111.111, server: my_server.com, request: "GET /blah/404.html HTTP/1.1", host: "my_server.com"

如何正确地将我的Nginx服务器配置为处理404s的不存在的子目录URL?

I have the following nginx.conf section:

# valid url
location /foo/bar/ {
    proxy_pass http://my_server/foo/bar/;
}


# redirect base url to index.html
location = / {
    root   /usr/share/nginx/html;
    index  index.html;
}


# anything not listed above to 404
location / {
    return 404;
}

error_page 404 404.html;
error_page 500 502 503 504 50x.html;

When I launch my nginx server and request a route like: https://my_server/blah/, i am correctly redirected to https://my_server/404.html page where I can see my custom 404. However, if I make a another request to a url with a subdirectory: https://my_server/blah/baz, my nginx server tries to find https://my_server/blah/404.html

my_server-reverse_proxy-1 | 2022/05/02 23:05:35 [error] 7#7: *1 open() "/usr/share/nginx/html/blah/404.html" failed (2: No such file or directory), client: 11.11.111.111, server: my_server.com, request: "GET /blah/404.html HTTP/1.1", host: "my_server.com"

How do I properly configure my NGINX server to handle 404s for non existent sub directory urls?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

↙温凉少女 2025-02-01 15:45:20

要从文档根加载错误文档,它们的相对URL应以/

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

开始#Error_page“ rel =” error_page的nofollow noreferrer“> nginx文档用领先的斜杠显示此代码。包括通常的做法。

To load the error documents from the document root, the relative URL for them should start with a /

error_page 404 /404.html;
error_page 500 502 503 504 /50x.html;

The nginx documentation for error_page shows this code with the leading slash. Including it is the usual practice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文