Nginx:将错误页面设置为静态文件

Nginx: setting the error page to a static file

我有以下 nginx 配置:

server {
  listen          80;
  server_name     default;

  location / {
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080/;
  }

}

http://127.0.0.1:8080/ 的内部服务没有响应 nginx return 一个 502 Bad Gatway 错误.

我的问题:如何将 nginx 配置为 returning 静态 html 文件,例如/var/www/error.html,什么时候出现这样的错误?


我试过的

接受提示from here我试过这个:

server {
  listen          80;
  server_name     default;

  location / {
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080/;
  }

  error_page 404 502 /error.html;

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

}

如果服务(在端口 8080 上)关闭,它工作正常,但是当服务启动时,我尝试访问 url /error.html nginx 匹配最后一个位置(return给我静态文件),我想避免它。

好吧,如果你真的不想公开错误页面,你可以使用命名位置。

server {
  listen          80;
  server_name     default;

  location / {
    proxy_set_header Host $host;
    proxy_pass http://127.0.0.1:8080/;
    # probably you also need this
    # proxy_intercept_errors on;
  }

  error_page 404 502 @error;

  location @error {
    root /var/www;
    try_files /error.html /error.html;
  }
}