如何更改状态代码,包括自定义错误页面

How can I change the status code including custom error page

我正在将状态代码 403 更改为 404。我只是不想让用户知道存在受限页面。这适用于以下配置:

error_page 403 =404 @404;   

location @404 {
    return 404;
}

现在,我想 return 自定义 404 错误页面。但是发送给用户的状态码应该始终是 404(当 403 发生时)。我怎样才能让两者都起作用?

编辑:

我希望……这样可以,但不行。

location @404 {
    root /usr/share/nginx/html;
    internal;
    return 404 "/custom_404.html";
}

试试这个:

error_page 404 /404.html;
location = /404.html {
        root /var/www/error/;
        internal;
}

问题是 deny 语句。该语句阻止了所有内容,包括对任何自定义错误页面的访问。解决方法是在自定义错误页面块中添加allow all来撤销继承的deny。

error_page 403 404 =404 /custom_404.html;

location = /custom_404.html {
    allow all;
    root /usr/share/nginx/html;
    internal;
}

原始来源:

https://www.cyberciti.biz/faq/unix-linux-nginx-custom-error-403-page-configuration/