如何从文档根而不是使用Nginx的子目录显示自定义404?
我有以下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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要从文档根加载错误文档,它们的相对URL应以
/
开始#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
/
The nginx documentation for error_page shows this code with the leading slash. Including it is the usual practice.