Nginx。如何在获取时设置自定义500错误页:内部重定向到时重写或内部重定向循环

Nginx。如何在获取时设置自定义500错误页:内部重定向到时重写或内部重定向循环,nginx,custom-error-pages,http-status-code-500,Nginx,Custom Error Pages,Http Status Code 500,示例nginx config/etc/nginx/conf.d/errorpagestest.conf server { listen 80; server_name errorstage.test; error_log /var/log/nginx/errorstage.log warn; access_log /var/log/nginx/errorstage-access.log custom; root /var/www/errorpagestest;

示例nginx config/etc/nginx/conf.d/errorpagestest.conf

server {
   listen 80;
   server_name errorstage.test;
   error_log /var/log/nginx/errorstage.log warn;
   access_log /var/log/nginx/errorstage-access.log custom;

   root /var/www/errorpagestest;
   try_files $uri $uri/ /dir/index.html;

   error_page 403 /403.html;
   error_page 404 /404.html;
   error_page 500 /500.html;
   error_page 502 /502.html;
   error_page 503 /503.html;
   error_page 504 /504.html;
        }
案例1:如果删除/注释行“try_files$uri$uri//dir/index.html;”并尝试获取任何不存在的文件,我将获得加载自定义404错误页面文件/var/www/errorpagestest/404.html

案例2:如果文件/var/www/errorpagestest/dir/index.html存在,当我尝试获取任何不存在的文件时,我会获取load file/var/www/errorpagestest/dir/index.html

案例3:如果我试图在/var/www/errorpagestest/dir/index.html文件不存在时获取任何不存在的文件,我会得到nginx 500错误页面,记录在/var/log/nginx/errorstage.log:[error]21996#21996:*401019重写或内部重定向循环,同时内部重定向到“/dir/index.html”,服务器:errorstage.test,请求:“GET/anyfile HTTP/1.1”,主机:“errorstage.test”


但我需要在案例3中获得自定义的500错误页面。它是如何解决的?

Nginx似乎一旦达到其重定向周期限制,它也会停止处理
错误页面
重定向

最简单的解决方案是在第一次尝试后,如果
/dir/index.html
不存在,则显式触发500响应

例如:

try_files $uri $uri/ /dir/index.html =500;

似乎一旦Nginx达到其重定向周期限制,它也会停止处理
error\u页面
重定向

最简单的解决方案是在第一次尝试后,如果
/dir/index.html
不存在,则显式触发500响应

例如:

try_files $uri $uri/ /dir/index.html =500;