Return 来自指定位置的自定义 nginx 错误
Return custom nginx error from within named location
我有一个配置,我想在其中 return 来自指定位置的自定义错误。但是,我收到的是标准的 nginx 错误页面。不过,我可以从未命名的位置获取自定义错误。
为了重现该问题,我创建了这个基于 docker 的简单设置:
Docker 文件:
FROM nginx:1.17.9
COPY default.conf /etc/nginx/conf.d/default.conf
COPY 404.html /usr/share/nginx/html/404.html
default.conf:
server {
listen 80;
server_name localhost;
#recursive_error_pages on;
location = /404.html {
root /usr/share/nginx/html;
internal;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
# used in a more complex setup, this is just a minimal config
# # See
error_page 404 = @fwd;
return 404;
}
location @fwd {
root /usr/share/nginx;
error_page 404 /404.html;
}
}
这里的 50x 错误并不有趣,我已经从 nginx docker 图像中调整了配置。
使用 404 以外的内容重定向到指定位置没有任何区别。
404.html:
<!DOCTYPE html>
<html>
<head><title>404</title></head>
<body>
my 404
</body>
</html>
构建:docker build -t mynginx .
并开始 docker run --rm -p 8080:80 --name mynginx mynginx
。
现在将浏览器指向 http://localhost:8080/index.html should return the standard welcome page, while http://localhost:8080/inexistent.html 将 return 一个内部 nginx 404 页面,而不是我配置的自定义页面。
有没有办法在指定位置实际执行此操作?
在探索问题时,我发现了 recursive_error_pages on;
——但愚蠢地留下了评论。自然没用。
取消对该指令的注释可解决问题![=11=]
我有一个配置,我想在其中 return 来自指定位置的自定义错误。但是,我收到的是标准的 nginx 错误页面。不过,我可以从未命名的位置获取自定义错误。
为了重现该问题,我创建了这个基于 docker 的简单设置:
Docker 文件:
FROM nginx:1.17.9
COPY default.conf /etc/nginx/conf.d/default.conf
COPY 404.html /usr/share/nginx/html/404.html
default.conf:
server {
listen 80;
server_name localhost;
#recursive_error_pages on;
location = /404.html {
root /usr/share/nginx/html;
internal;
}
# redirect server error pages to the static page /50x.html
#
error_page 500 502 503 504 /50x.html;
location = /50x.html {
root /usr/share/nginx/html;
}
location / {
# used in a more complex setup, this is just a minimal config
# # See
error_page 404 = @fwd;
return 404;
}
location @fwd {
root /usr/share/nginx;
error_page 404 /404.html;
}
}
这里的 50x 错误并不有趣,我已经从 nginx docker 图像中调整了配置。
使用 404 以外的内容重定向到指定位置没有任何区别。
404.html:
<!DOCTYPE html>
<html>
<head><title>404</title></head>
<body>
my 404
</body>
</html>
构建:docker build -t mynginx .
并开始 docker run --rm -p 8080:80 --name mynginx mynginx
。
现在将浏览器指向 http://localhost:8080/index.html should return the standard welcome page, while http://localhost:8080/inexistent.html 将 return 一个内部 nginx 404 页面,而不是我配置的自定义页面。
有没有办法在指定位置实际执行此操作?
在探索问题时,我发现了 recursive_error_pages on;
——但愚蠢地留下了评论。自然没用。
取消对该指令的注释可解决问题![=11=]